Java Threads-Multithreading-Daemon threads

Source: Internet
Author: User
Tags thread class

1, multithreading can be implemented runnable interface or inherited thread, can only be started by the thread class start () method to start a thread 2, there are two types of threads in Java: User thread, Daemon thread (daemon)
It is estimated that the students who have studied UNIX development but have not studied Java in detail will be puzzled that there is no concept of the Guardian thread in the operating system, only the daemon is said, but the Java language mechanism is built on the JVM, which means that the Java platform shields the bottom of the operating system. So it can be in its own virtual platform to construct a favorable mechanism for themselves, and the language or platform of the designer is more or less affected by the Unix idea, and the daemon thread mechanism is the JVM such a platform, so the Guardian thread came into being.

The role of Daemon is to provide services for the operation of other threads, such as GC threads. In fact, the user thread and the daemon thread daemon is essentially no different, the only difference is in the virtual machine's departure: If the user thread is all evacuated, then daemon thread is not a good service, So the virtual machine also exits.
The daemon thread is not available inside the virtual machine, and the user can set the daemon thread by itself: public final void Setdaemon (Boolean on), but there are a few things to note:
1), Thread.setdaemon (true) must be set before Thread.Start (), or a illegalthreadstateexception exception will run out. You cannot set a running regular thread as a daemon thread. (Note: This is a significant difference from the daemon, the daemon is created, let the process out of the original session control + let the process out of the original Process group Control + Let the process out of the original control terminal control; So the language mechanism pinned to the virtual machine is fundamentally different from the system-level language)
2), the new thread generated in the daemon thread is also daemon.

3. Multi-threaded coordination Help class
Countdownlatch control the parent thread blocks until the multithreaded execution is complete
Cyclicbarrier control multithreading at a certain point in sync
Semaphore control simultaneous execution of threads
Exchanger two threads exchanging data at a point

4, future, Futuretask remote Call does not block

Java Threads-Multithreading-Daemon threads

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.