fork download
  1. import java.util.concurrent.LinkedBlockingQueue;
  2.  
  3. public class Main {
  4. static LinkedBlockingQueue<Message> messageQueue = new LinkedBlockingQueue<>();
  5.  
  6. enum Message { END }
  7.  
  8. static class Thread1 implements Runnable {
  9. @Override
  10. public void run() {
  11. System.out.println("From thread1");
  12. Main.messageQueue.add(Message.END);
  13. }
  14. }
  15.  
  16. static class Thread2 implements Runnable {
  17. @Override
  18. public void run() {
  19. System.out.println("From thread2");
  20. Main.messageQueue.add(Message.END);
  21. }
  22. }
  23.  
  24. public static void main(String[] args) throws java.lang.InterruptedException {
  25. (new Thread(new Thread1())).start();
  26. (new Thread(new Thread2())).start();
  27. int endedThreads = 0;
  28. while(true){
  29. Message t = messageQueue.take();
  30. if(t == Message.END) {
  31. endedThreads += 1;
  32. if(endedThreads == 2) {
  33. break;
  34. }
  35. }
  36. }
  37. System.out.println("Quit program");
  38. }
  39. }
  40.  
Success #stdin #stdout 0.06s 27936KB
stdin
Standard input is empty
stdout
From thread1
From thread2
Quit program