Skip to content
Advertisement

Thread join(1) usage in mutlithread wait-notify example in Java

I have example with wait-notify application which is invoked from main:

JavaScript

}

and invoker

JavaScript

}

As I expected “exception” message should be printed as I join(1) to threads and wait them to die only 1 mills, but they are sleeping more than that. What am I missing?

Advertisement

Answer

join(1) has 3 ways ‘out’:

  • The thread you called join on ends, in which case your call to join(1) stops running by returning.
  • Some code someplace (it’s only code that can do it, e.g. a user pressing CTRL+C or whatnot never causes this) calls .interrupt() on your thread; join(1) stops running by throwing InterruptedException.
  • 1 milliseconds passes, in which case join(1) stops running by returning.

You are evidently under the impression that the join method exits by way of throwing InterruptedEx if the time runs out. It does not.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement