当前位置: 首页>>代码示例>>Java>>正文


Java Exchanger类代码示例

本文整理汇总了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;
}
 
开发者ID:Robo4J,项目名称:robo4j-rpi-ard-tank-example,代码行数:23,代码来源:PlatformUnit.java

示例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();
    }
 
开发者ID:firery,项目名称:java-concurrency-cheatsheet,代码行数:21,代码来源:Main.java

示例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();

}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:27,代码来源:Main.java

示例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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:27,代码来源:ConnectorStopDeadlockTest.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:ExchangerTest.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:ExchangerTest.java

示例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");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ExchangeLoops.java

示例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();
}
 
开发者ID:baohongfei,项目名称:think-in-java,代码行数:20,代码来源:ExchangerDemo.java

示例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");
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:17,代码来源:ExchangeLoops.java

示例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();
	}
}
 
开发者ID:hdcuican,项目名称:java_learn,代码行数:19,代码来源:ExchangerTest.java

示例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();
}
 
开发者ID:Saisimon,项目名称:tip,代码行数:22,代码来源:Main.java

示例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();
}
 
开发者ID:goldmansachs,项目名称:reladomo,代码行数:26,代码来源:TestConcurrentQueryIndex.java

示例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();
}
 
开发者ID:goldmansachs,项目名称:reladomo,代码行数:25,代码来源:TestConcurrentStringIndex.java

示例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();
}
 
开发者ID:goldmansachs,项目名称:reladomo,代码行数:25,代码来源:TestConcurrentWeakPool.java

示例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();
}
 
开发者ID:goldmansachs,项目名称:reladomo,代码行数:26,代码来源:TestFullUniqueIndex.java


注:本文中的java.util.concurrent.Exchanger类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。