How does the join () method of the Thread class work?

Consider the following code:

class ThreadJoinTest{
    public static void main(String[] arguments){
        TimeUnit unit; 
        final Thread taskThread = new Thread(task);
        taskThread.start();
        taskThread.join(unit.millis(timeout));   
     }
}

So, when the main thread executes a line taskThread.join()with a timeout value, will the main thread give taskThreadenough time to complete its task? Is this the main goal of the join method?

What happens if:

  • taskThread terminates before timeout?
  • What happens if a timeout is reached but has taskThreadnot completed execution? Does he get the opportunity taskThreadto complete his task or not?
  • How does the main thread know that the connection has returned because it taskThreadended normally or because a timeout has been reached?
+6
source share
3 answers
  • main , taskThread .
  • main , taskThread . .
  • taskThread , - , main . main , - taskThread - .
+6

join() , (.. ..). join(). - join(), join() -.

, , taskThread .

+4

The timeout condition will take precedence. When the timeout is reached, the main thread and taskThread are equally likely candidates for execution.

+1
source

All Articles