本文整理汇总了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();
}