1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package com.redisc;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.SynchronousQueue;
@Slf4j(topic = "c.Test") public class Run {
public static void main(String[] args) throws InterruptedException { SynchronousQueue<Integer> integers = new SynchronousQueue<>(); new Thread(() -> { try { log.debug("putting {}", 1); integers.put(1); log.debug("{} putted", 1);
log.debug("putting {}", 2); integers.put(2); log.debug("{} putted", 2); } catch (InterruptedException e) { e.printStackTrace(); } }, "t1").start();
Thread.sleep(1000);
new Thread(() -> { try { log.debug("taking {}", 1); integers.take(); } catch (InterruptedException e) { e.printStackTrace(); } }, "t2").start();
new Thread(() -> { try { log.debug("taking {}", 2); integers.take(); } catch (InterruptedException e) { e.printStackTrace(); } }, "t3").start();
} }
|