Tell me, please, how to call another stream (stream 1) asynchronously in java from one thread (stream 0) and then return to stream 0 without using the java.util.concurrent library. (This is a question from an interview, the most difficult).

  • What do we mean by words to cause flow ? Call the method in another thread and get the result asynchronously? - Then you can use a couple of queues (one for the input arguments, and the other for the results, and some kind of (depending on the task) wait-notify combination). - avp

2 answers 2

 // thread 1 Thread2 t = new Thread2(); t.start(); t.join(); // thread 2 // do something 

you have a mismatch in the question, if the call is asynchronous, then there is no point in returning to stream 0, because execution there was not blocked during the call to stream 1

  • Just noticed! And at join, "thread 0" performing this method on "thread 1" will wait until "thread 1" completes its execution. - Kerins_Sataier
  • one
    The answer is incorrect. - Barmaley
  • 2
    @Barmaley, so write the correct one :) - misha-nesterenko
  • the question is really incorrect - when we "call" the stream, we do not leave for it, so we don’t have to return - we did not leave stream 0. - rfq
  • one
    @Barmaley, nerd, about О как! человек который не отличает join() от асинхронности О как! человек который не отличает join() от асинхронности keep your idle code and no longer tell anyone that it is the correct ideone.com/l4U1i1 - misha-nesterenko

The easiest way to return to the main thread is to throw an exception in the child stream :)), after hanging the handler in the main thread:

 setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) 

Update

Working code example. Collect, run and make sure that everything works asynchronously 2 threads are working in parallel and after a second the child thread throws an exception and control returns to the main thread.

Remove the minus, otherwise :)

 public class Main implements Thread.UncaughtExceptionHandler { public static void main(String[] args) { Main main=new Main(); Thread childThread=new ChildThread(); childThread.start(); childThread.setUncaughtExceptionHandler(main); for(int i=0; i < 100; i ++) { try { Thread.sleep(100); System.out.println("Main thread, loop "+i); } catch(InterruptedException e) { break; } } } public void uncaughtException(Thread t, Throwable e) { System.out.println("Caught thread "+t.getClass().getName()+" - we're back in main thread!"); } } class ChildThread extends Thread { @Override public void run() { int j=0; while(true) { try { Thread.sleep(100); System.out.println("Child thread, loop "+j++); if(j>=10) { throw new RuntimeException("Test!"); } } catch(InterruptedException e) { break; } } } } 
  • @ misha-nesterenko you can argue? - Barmaley
  • give an example code - misha-nesterenko
  • In general, if the handler is not hung up on the main thread, but on the child thread, then it can be used as a signaling device that the thread is complete. But this does not solve the problem of blocking the main thread for the duration of the child, besides using exceptions to send messages is not the best thing you can do, synchronized , wait , notify or join are enough for that. Waiting for a code from you :) - misha-nesterenko
  • @ misha-nesterenko see sample code in update answer :) - Barmaley
  • @Barmaley, if I understand correctly ChildThread will end when j == 10? - avp