Thread join && Sleep

Today’s topic more on concurrent package. Which kick in from Java 1.5 if I m not mistaken.  Have a peek with following class first.

Screen Shot 2017-02-04 at 12.37.26 PM.png

It’s a class with interface .  On the override method ,  simply print out the array “Demo_words” in a for loop and put thread into sleep for 2s after every print operation and throw out “InterruptedException” If encounter any.

Back to the main thread, code list as below:

Screen Shot 2017-02-04 at 12.44.30 PM.png

Which construct a new Thread with the object Thread_runnable() that we just created. Then launch the thread by t.start(); And try give out method 1s to finish all computation and finally interrupt it if no response after 1s.

Noted that interrupt is necessary in this case, it function like finally block. Because thread t is belong to the main so if main thread terminate before the all sub thread. Then the sub thread would keep running and occupied the resources.

Leave a comment