Review multithreading Two

Source: Internet
Author: User

Daemon Threads

There are two kinds of threads in Java: One is the main thread and one is the daemon thread.

User threads are: User-defined threads that are created, the main thread stops, and the user thread does not stop.

Daemon thread: The daemon thread stops when the process does not exist or the main thread stops.

Use the Setdaemon (True) method to set the daemon thread

public class Daemonthread {public static void main (string[] args) {thread thread=new thread (new Runnable () {@Overridepubli c void Run () {while (true) {try {Thread.Sleep],} catch (Exception e) {}system.out.println ("I am a Child thread");}}); Thread.setdaemon (True); Thread.Start (); for (int i=0;i<8;i++) {try {thread.sleep)} catch (Exception e) {} System.out.println ("I am the main thread");} SYSTEM.OUT.PRINTLN ("The main thread is finished! ");}}

  

Multi-threaded operation status:

Threads are always in one of the following five states from creation, run to end: New state, ready state, running state, blocking state, death state.

New status:

When a thread is created with the new operator, such as new Thread (R), the thread has not started running, and the thread is in the new state. When a thread is in a new state, the program has not started running code in the thread

Ready state:

A newly created thread does not automatically start running, and the thread's start () method must be called to execute the thread. When the thread object calls the start () method, the thread is started, the start () method creates the system resources that the thread runs, and the scheduler thread runs the Run method.

When the start () method returns, the thread is in the ready state.

A thread that is in a ready state does not necessarily run the run () method immediately, and the thread must also compete with other threads for CPU time, only to get CPU time to run the thread. Because in a single-core CPU computer system, it is not possible to run multiple threads at the same time, only one thread is running at a time. Therefore, there may be multiple threads in the ready state at this time. Multiple threads that are in a ready state are dispatched by the thread scheduler (threads Scheduler) of the Java Runtime System.

Running Status:

When the thread gets CPU time, it goes into a running state and really starts executing the Run method.

Blocking Status:

A thread may enter a blocking state for various reasons during the run:

1. The thread goes to sleep by calling the Sleep method

2. The thread invokes an operation that is blocked on I/O, that is, the operation does not return to its caller until the input and output is complete;

3. The thread view gets a lock that is being held by another thread.

4. The thread is waiting for a trigger condition

Death Status:

There are two causes of thread death:

1, run () method normal exit and natural death,

2. An uncaught exception terminates the Run () method and causes the thread to sudden death.

To determine whether a thread is currently alive (or is either operational or blocked), you need to use isAlive method. This method returns true if it is operational or blocked, or false If the thread is still in the new state and is not operational, or the thread is dead .

Review multithreading Two

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.