Java 8 Exchanger - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Exchanger class.

This code example shows how to use the following methods:exchange
    public void testSocketEndPoints() throws Exception 
    { 
        final ServerSocket server = new ServerSocket();
        server.bind(null);
         
        final Exchanger<Socket> accepted = new Exchanger<Socket>();
        new Thread(){
            public void run() 
            { 
                try  
                { 
                    accepted.exchange(server.accept());
                } 
                catch(Exception e)
                { 
                    e.printStackTrace();
                } 
            } 
        }.start();
         

6
CodeRank
This code example shows how to use the following methods:exchange
 
        if (messageExchanger != null)
        { 
            try 
            { 
                messageExchanger.exchange(message);
            } 
            catch (InterruptedException e)
            { 
                LOG.debug(e);
            } 
        } 
    } 
 
    public void waitForClose(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedExceptionAssert.assertThat("Client Socket Closed",closeLatch.await(timeoutDuration,timeoutUnit),is(true));
    } 
 
    public void waitForConnected(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedException
5
CodeRank
This code example shows how to use the following methods:
 
   static class MeetingPlace { 
      private final Exchanger<Agent> room;
 
      MeetingPlace() { 
         room = new Exchanger<>();
      } 
 
      public Agent enter(Agent visitor) throws InterruptedException {
         if (meetingsLeft.get() < 0) {
            return null; 
         } 
         Agent agent = room.exchange(visitor);
         latch.countDown();
         if (meetingsLeft.decrementAndGet() < 0) {
            return null; 
         } 
         return agent;
      } 
 
4
CodeRank
This code example shows how to use the following methods:
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 
import java.util.concurrent.Exchanger; 
 
public class BufferedFile { 
    private final Exchanger<ByteBuffer> mExchanger = new Exchanger<ByteBuffer>();
    private FileInputStream mStream;
    private ByteBuffer mWriteBuffer = ByteBuffer.allocateDirect(1024 * 1024);
    private ByteBuffer mReadBuffer = ByteBuffer.allocateDirect(1024 * 1024);
    private FileChannel mChannel;
    private boolean mFirstRun = true;
    private int mAvailable;
 
    public BufferedFile(String filename) {
        try { 
            mStream = new FileInputStream(filename);
            mChannel = mStream.getChannel();
        } catch (FileNotFoundException e) {
            throw new Error(e);
        } 
4
CodeRank
This code example shows how to use the following methods:
		// 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
CodeRank
This code example shows how to use the following methods:
 * 
 * @author Tobias Wich <tobias.wich@ecsec.de> 
 */ 
public class SwingStepResult implements StepResult { 
 
    public Exchanger<Void> syncPoint = new Exchanger<>();
    private Step step;
    private ResultStatus status;
    private Step replacement;
    private List<OutputInfoUnit> results;
 
    public SwingStepResult(Step step) {
	this(step, null);
    } 
 
    public SwingStepResult(Step step, ResultStatus status) {
	this.step = step;
	this.status = status;
    } 
 
4
CodeRank
This code example shows how to use the following methods:
        } 
 
        @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 
4
CodeRank
This code example shows how to use the following methods:
 
	public OSGIServiceLookup(BundleContext context, Class<? extends T> service)
	{ 
		this.context = context;
		this.service = service;
		this.serviceReferenceExchanger = new Exchanger<ServiceReference>();
	} 
 
	OSGIServiceLookup(BundleContext context, Class<? extends T> service, Exchanger<ServiceReference> serviceReferenceExchanger)
	{ 
		this.context = context;
		this.service = service;
		this.serviceReferenceExchanger = serviceReferenceExchanger;
	} 
 
	public T getService() 
	{ 
		ServiceReference serviceReference = context.getServiceReference(service.getName());
 
		if (serviceReference == null)
4
CodeRank
This code example shows how to use the following methods:
        super(testName);
    } 
     
    protected void setUp() throws Exception {
        this.collector = new BlockingCollector();
        this.exchanger = new Exchanger<ApplicationEvent>();
    } 
     
    public void testCollectFIFOOrder() throws Exception {
        PayloadEvent e1 = new PayloadEvent(this, new String("test"), null);
        PayloadEvent e2 = new PayloadEvent(this);
        PayloadEvent e3 = new PayloadEvent(this);
         
        this.collector.onApplicationEvent(e1);
        this.collector.onApplicationEvent(e2);
        this.collector.onApplicationEvent(e3);
         
        assertSame(e1, this.collector.pollEvent());
        assertSame(e2, this.collector.pollEvent());
        assertSame(e3, this.collector.pollEvent());
4
CodeRank
This code example shows how to use the following methods:
 
    public static void main(String[] args) {
        List<String> buffer1 = new ArrayList<>();
        List<String> buffer2 = new ArrayList<>();
 
        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();
    } 
} 
4
CodeRank
See Code Examples for Java 8 Exchanger Methods: