本文整理匯總了Java中java.util.concurrent.Exchanger類的典型用法代碼示例。如果您正苦於以下問題:Java Exchanger類的具體用法?Java Exchanger怎麽用?Java Exchanger使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Exchanger類屬於java.util.concurrent包,在下文中一共展示了Exchanger類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: init
import java.util.concurrent.Exchanger; //導入依賴的package包/類
@Override
public RpiUnit init(Object input) {
if (Objects.nonNull(executorForAgents)) {
this.agents = new ArrayList<>();
this.active = new AtomicBoolean(false);
this.commandQueue = new LinkedBlockingQueue<>();
SimpleLoggingUtil.print(getClass(), "TankRpi: INIT");
final Exchanger<GenericCommand<PlatformUnitCommandEnum>> platformExchanger = new Exchanger<>();
final Map<String, GenericMotor> enginesMap = EngineRegistry.getInstance().getByNames(CONSUMER_NAME);
this.agents.add(createAgent("platformAgent", new ClientPlatformProducer(commandQueue, platformExchanger),
new ClientPlatformConsumer(executorForAgents, platformExchanger, enginesMap)));
if (!agents.isEmpty()) {
active.set(true);
logic = initLogic();
}
}
return this;
}
示例2: main
import java.util.concurrent.Exchanger; //導入依賴的package包/類
public static void main(String[] args) {
// 創建兩個緩衝區
List<String> buffer1=new ArrayList<>();
List<String> buffer2=new ArrayList<>();
// 創建 Exchanger
Exchanger<List<String>> exchanger=new Exchanger<>();
// 創建生產者
Producer producer=new Producer(buffer1, exchanger);
// 創建消費者
Consumer consumer=new Consumer(buffer2, exchanger);
Thread threadProducer=new Thread(producer);
Thread threadConsumer=new Thread(consumer);
threadProducer.start();
threadConsumer.start();
}
示例3: main
import java.util.concurrent.Exchanger; //導入依賴的package包/類
/**
* Main method of the example
* @param args
*/
public static void main(String[] args) {
// Creates two buffers
List<String> buffer1=new ArrayList<>();
List<String> buffer2=new ArrayList<>();
// Creates the exchanger
Exchanger<List<String>> exchanger=new Exchanger<>();
// Creates the producer
Producer producer=new Producer(buffer1, exchanger);
// Creates the consumer
Consumer consumer=new Consumer(buffer2, exchanger);
// Creates and starts the threads
Thread threadProducer=new Thread(producer);
Thread threadConsumer=new Thread(consumer);
threadProducer.start();
threadConsumer.start();
}
示例4: clientClosed
import java.util.concurrent.Exchanger; //導入依賴的package包/類
@Override
protected void clientClosed(RMIConnection conn) throws IOException {
System.out.println("clientClosed, will call connectorServer.stop");
final Exchanger<Void> x = new Exchanger<Void>();
Thread t = new Thread() {
public void run() {
try {
connectorServer.stop();
} catch (Exception e) {
fail(e);
}
}
};
t.setName("connectorServer.stop");
t.start();
waitForBlock(t);
/* If this thread is synchronized on RMIServerImpl, then
* the thread that does connectorServer.stop will acquire
* the clientList lock and then block waiting for the RMIServerImpl
* lock. Our call to super.clientClosed will then deadlock because
* it needs to acquire the clientList lock.
*/
System.out.println("calling super.clientClosed");
System.out.flush();
super.clientClosed(conn);
}
示例5: testExchange
import java.util.concurrent.Exchanger; //導入依賴的package包/類
/**
* exchange exchanges objects across two threads
*/
public void testExchange() {
final Exchanger e = new Exchanger();
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertSame(one, e.exchange(two));
assertSame(two, e.exchange(one));
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertSame(two, e.exchange(one));
assertSame(one, e.exchange(two));
}});
awaitTermination(t1);
awaitTermination(t2);
}
示例6: testTimedExchange
import java.util.concurrent.Exchanger; //導入依賴的package包/類
/**
* timed exchange exchanges objects across two threads
*/
public void testTimedExchange() {
final Exchanger e = new Exchanger();
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
}});
awaitTermination(t1);
awaitTermination(t2);
}
示例7: oneRun
import java.util.concurrent.Exchanger; //導入依賴的package包/類
static void oneRun(int nthreads, int iters) throws Exception {
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
CyclicBarrier barrier = new CyclicBarrier(nthreads + 1, timer);
Exchanger<Int> l = null;
Exchanger<Int> r = new Exchanger<>();
for (int i = 0; i < nthreads; ++i) {
pool.execute(new Stage(l, r, barrier, iters));
l = r;
r = (i+2 < nthreads) ? new Exchanger<Int>() : null;
}
barrier.await();
barrier.await();
long time = timer.getTime();
if (print)
System.out.println(LoopHelpers.rightJustify(time / (iters * nthreads + iters * (nthreads-2))) + " ns per transfer");
}
示例8: main
import java.util.concurrent.Exchanger; //導入依賴的package包/類
public static void main(String[] args) throws Exception
{
if (args.length > 0)
{
size = new Integer(args[0]);
}
if (args.length > 1)
{
delay = new Integer(args[1]);
}
ExecutorService exec = Executors.newCachedThreadPool();
Exchanger<List<Fat>> xc = new Exchanger<List<Fat>>();
List<Fat> producerList = new CopyOnWriteArrayList<Fat>(), consumerList = new CopyOnWriteArrayList<Fat>();
exec.execute(new ExchangerProducer<Fat>(xc, BasicGenerator
.create(Fat.class), producerList));
exec.execute(new ExchangerConsumer<Fat>(xc, consumerList));
TimeUnit.SECONDS.sleep(delay);
exec.shutdownNow();
}
示例9: oneRun
import java.util.concurrent.Exchanger; //導入依賴的package包/類
static void oneRun(int nthreads, int iters) throws Exception {
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
CyclicBarrier barrier = new CyclicBarrier(nthreads + 1, timer);
Exchanger<Int> l = null;
Exchanger<Int> r = new Exchanger<Int>();
for (int i = 0; i < nthreads; ++i) {
pool.execute(new Stage(l, r, barrier, iters));
l = r;
r = (i+2 < nthreads) ? new Exchanger<Int>() : null;
}
barrier.await();
barrier.await();
long time = timer.getTime();
if (print)
System.out.println(LoopHelpers.rightJustify(time / (iters * nthreads + iters * (nthreads-2))) + " ns per transfer");
}
示例10: main
import java.util.concurrent.Exchanger; //導入依賴的package包/類
public static void main(String []args) {
final Exchanger <Integer>exchanger = new Exchanger<Integer>();
for(int i = 0 ; i < 10 ; i++) {
final Integer num = i;
new Thread() {
public void run() {
System.out.println("我是線程:Thread_" + this.getName() + " 我的數據是:" + num);
try {
Integer exchangeNum = exchanger.exchange(num);
Thread.sleep(1000);
System.out.println("我是線程:Thread_" + this.getName() + " 最初的數據為:" + num + " , 交換後的數據為:" + exchangeNum);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}
示例11: producerAndConsumerAndExchangerMain
import java.util.concurrent.Exchanger; //導入依賴的package包/類
/**
* http://ifeve.com/thread-synchronization-utilities-8/
*
* @see ExchangerProducer
* @see ExchangerConsumer
*/
public static void producerAndConsumerAndExchangerMain() {
List<String> buffer1 = new ArrayList<>();
List<String> buffer2 = new ArrayList<>();
Exchanger<List<String>> exchanger = new Exchanger<>();
ExchangerProducer producer = new ExchangerProducer(buffer1, exchanger);
ExchangerConsumer consumer = new ExchangerConsumer(buffer2, exchanger);
Thread producerThread = new Thread(producer);
Thread consumerThread = new Thread(consumer);
producerThread.start();
consumerThread.start();
}
示例12: testContendedPut
import java.util.concurrent.Exchanger; //導入依賴的package包/類
public void testContendedPut() throws Exception
{
final int max = 1000000;
CachedQuery orderedData[] = new CachedQuery[max];
for(int i=0;i<max;i++)
{
CachedQuery o = createCachedQuery(i);
orderedData[i] = o;
}
final CachedQuery data[] = shuffle(orderedData);
final Exchanger exchanger = new Exchanger();
final NonLruQueryIndex index = new NonLruQueryIndex();
PutRunnableWithExchange first = new PutRunnableWithExchange(0, max, data, index, exchanger);
PutRunnableWithExchange second = new PutRunnableWithExchange(0, max, data, index, exchanger);
ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
firstThread.start();
secondThread.start();
firstThread.joinWithExceptionHandling();
secondThread.joinWithExceptionHandling();
assertEquals(max, index.size());
assertEquals(max, index.getEntryCount());
first.verifyExistence();
second.verifyExistence();
}
示例13: testContendedGetIfAbsentPut
import java.util.concurrent.Exchanger; //導入依賴的package包/類
public void testContendedGetIfAbsentPut() throws Exception
{
final int max = 1000000;
String orderedData[] = new String[max];
for(int i=0;i<max;i++)
{
String o = createData(i);
orderedData[i] = o;
}
final String data[] = shuffle(orderedData);
final Exchanger exchanger = new Exchanger();
final StringIndex index = createStringPool();
GetIfAbsentPutRunnableWithExchange first = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, exchanger);
GetIfAbsentPutRunnableWithExchange second = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, exchanger);
ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
firstThread.start();
secondThread.start();
firstThread.joinWithExceptionHandling();
secondThread.joinWithExceptionHandling();
assertEquals(max, index.size());
first.verifyExistence();
second.verifyExistence();
}
示例14: testContendedGetIfAbsentPut
import java.util.concurrent.Exchanger; //導入依賴的package包/類
public void testContendedGetIfAbsentPut() throws Exception
{
final int max = 1000000;
String orderedData[] = new String[max];
for(int i=0;i<max;i++)
{
String o = createData(i);
orderedData[i] = o;
}
final String data[] = shuffle(orderedData);
final Exchanger exchanger = new Exchanger();
final ConcurrentWeakPool index = createStringPool();
GetIfAbsentPutRunnableWithExchange first = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, exchanger);
GetIfAbsentPutRunnableWithExchange second = new GetIfAbsentPutRunnableWithExchange(0, max, data, index, exchanger);
ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
firstThread.start();
secondThread.start();
firstThread.joinWithExceptionHandling();
secondThread.joinWithExceptionHandling();
assertEquals(max, index.size());
first.verifyExistence();
second.verifyExistence();
}
示例15: testContendedGetIfAbsentPut
import java.util.concurrent.Exchanger; //導入依賴的package包/類
public void testContendedGetIfAbsentPut() throws Exception
{
final int max = 1000000;
TestObject orderedData[] = new TestObject[max];
for(int i=0;i<max;i++)
{
TestObject o = new TestObject(i);
orderedData[i] = o;
}
final TestObject data[] = shuffle(orderedData);
final Exchanger exchanger = new Exchanger();
Extractor[] extractors = {new ShiftedIntExtractor(1)};
final ConcurrentFullUniqueIndex<TestObject> index = new ConcurrentFullUniqueIndex(extractors, 7);
PutIfAbsentRunnableWithExchange first = new PutIfAbsentRunnableWithExchange(0, max, data, index, exchanger);
PutIfAbsentRunnableWithExchange second = new PutIfAbsentRunnableWithExchange(0, max, data, index, exchanger);
ExceptionCatchingThread firstThread = new ExceptionCatchingThread(first);
ExceptionCatchingThread secondThread = new ExceptionCatchingThread(second);
firstThread.start();
secondThread.start();
firstThread.joinWithExceptionHandling();
secondThread.joinWithExceptionHandling();
assertEquals(max, index.size());
first.verifyExistence();
second.verifyExistence();
}