package codesample.java_se_api.concurency.collections;

import java.io.IOException;
import java.util.concurrent.SynchronousQueue;

/**
 * SynchronousQueue provides a powerful concurrency mechanism to synchronize work in different threads.
 * In order to take() element from queue a call to put() should be active from another thread. At the same time
 * in order to put() something in a queue, a call to take() should be pending from other thread.
 */
class SynchronousQueueExample {
    public static void main(String args[]) throws InterruptedException {

        SynchronousQueue<String> str = new SynchronousQueue<>();

        Thread tr = new Thread(new MyRunnable(str));
        tr.start();
        str.put("kek");

        System.out.println("Finished");
    }

    private static class MyRunnable implements Runnable {

        final SynchronousQueue<String> queue;

        MyRunnable(SynchronousQueue<String> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            try {
                System.out.println(queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

@@ -53,36 +53,8 @@
 s;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -556,19 +556,21 @@
 String%3E 
-str
+queue
  = new S
@@ -607,16 +607,19 @@
 Thread t
+ake
 r = new 
@@ -633,22 +633,267 @@
 new 
-MyRunnable(str
+SynchronousQueueTaker(queue));%0A        taker.start();%0A%0A        System.out.println(%22Nothing in output, because taker thread has nothing to take thus it's blocked%22);%0A%0A        Thread putter = new Thread(new SynchronousQueuePutter(queue, %22Value passed to putter!%22
 ));%0A
@@ -900,17 +900,21 @@
         
-t
+putte
 r.start(
@@ -916,24 +916,25 @@
 tart();%0A
+%0A
         
 str.put(
@@ -929,24 +929,104 @@
     
-str.put(%22kek%22);%0A
+/* Wait for both threads before outputting finish*/%0A        taker.join();%0A        putter.join();
 %0A   
@@ -1062,16 +1062,17 @@
 shed%22);%0A
+%0A
     %7D%0A%0A 
@@ -1095,26 +1095,37 @@
 c class 
-MyRunnable
+SynchronousQueueTaker
  impleme
@@ -1199,18 +1199,29 @@
     
-MyRunnable
+SynchronousQueueTaker
 (Syn
@@ -1522,11 +1522,524 @@
  %7D%0A    %7D
+%0A%0A    private static class SynchronousQueuePutter implements Runnable %7B%0A%0A        final SynchronousQueue%3CString%3E queue;%0A        final String value;%0A%0A        SynchronousQueuePutter(SynchronousQueue%3CString%3E queue, String value) %7B%0A            this.queue = queue;%0A            this.value = value;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            try %7B%0A                queue.put(value);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Add better sync queue example