`

Java Thread programming basical knowledge

阅读更多

yield() :
Give a hint to the thread scheduling mechanism that, you’ve done enough and some other thread might as well have the CPU. And this is really a hint --- there is no guarantee that your implementation will listen to you! And this hint takes the form of the yield() method.
In general, yield( ) is useful only in rare situations, and you can’t rely on it to do any serious tuning of your application.

join():
a.join();  a will run first, the thread who call a.join(); will block, until a finished running.
Code example: for(int i = 0; I < 5; i++) newSleepingThread().join();

The difference between yield() and sleep()
yield() means release the CPU immediately, I just give up the use of CPU, I give somebody else to use CPU. But I didn’t go to sleep.
sleep(ms) means I will go to sleep for ms million seconds. I may waked up by somebody else(if somebody call myThread.interrupt();), so this is an exception! Need to be surrounded by try catch. wait() is quite similar in this scenario.

Just remember: don’t control the execution order using sleep() or yield(), there is no way to control using sleep() and yield() methods.

Daemon threads:
A daemon thread is one that is supposed to provide a general service in the background. It’s not part of the main application. So, when all the non-daemon threads complete, the program is terminated.


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics