Java multithreading (i) thread

Source: Internet
Author: User
Tags thread class

Six states of a thread

1. New status

New but the state of a thread that does not start, such as "thread thread = new Thread ()", is a thread that is in a new state.

2. Operational status

New thread that calls the Strat () method is in the running state (Runnable), the running thread may be running in a Java virtual machine, and may be waiting for the processor's resources, since a thread must obtain the CPU's resources before it can run its contents in the Run () method , or wait in line.

3. Blocking status

If a thread is waiting for a monitor lock to enter a synchronization method or a synchronous method block, the thread is blocked.

4. Wait Status

A thread is waiting because it calls the wait () method without the timeout object, the thread's join () method without a timeout, and the Locksupport Park () method.

5. Timeout wait status

A thread is called by the Wait () method of the object with the specified wait time, the join () method of the thread, the sleep () method of the thread, the Parknanos () method of the Locksupport, Locksupport the Parkuntil () method, it will be in a time-out wait state.

6. Termination status

When a thread call terminates or the run () method finishes executing, it enters the terminating state, and the thread in the terminating state does not have the ability to continue running.

Method of the thread instance start () method

Starts the thread, waits for the CPU to call the thread object's run () method, resulting in an asynchronous execution effect.

Note: When multiple threads call the start () method at the same time, the order of the CPU boot threads is random. The order in which the start () method is called does not represent the boot order. The startup of a thread is random

Run () method

The thread starts executing and the virtual machine calls the contents of the run () method

Note: If only the Run () method is called, the start thread is not called by the start () method, and will all be performed by the main function without an asynchronous effect. Just calling the run () method doesn't make any sense

IsAlive () method

Tests whether the thread is active and returns true whenever the thread starts and does not terminate

GetId () method

In a Java application, there is a long global unique thread ID generator Threadseqnumber, each new out of a thread, this generator is self-increment, and gives the thread the TID attribute, which is done by thread itself, the user cannot execute the ID of a thread

GetName () method

In the new thread, you can specify the name of the thread, and the name of the thread is obtained through the GetName () method. If you do not specify a name, there is a globally unique thread initializer generator in thread that has an int type Threadinitnum,java first threadinitnum and then "Thread-threadinitnum" to name the newly generated thread

GetPriority () method and setpriority (int newpriority) method

These two methods are used to get and set the priority of the thread, the easier the priority thread is to be executed first, and the setting priority to help the thread planner determine which thread to select next time to take precedence. Two threads waiting on the CPU, higher priority threads are more likely to be executed by the CPU

Add: The default priority for a thread is 5, and if you do not specify it manually, the priority of the thread is inherited, such as the thread main starts thread A, and the priority of a is the same as the priority of Main. The CPU will try to give execution resources to a higher priority thread

Isdaemon () Method and Setdaemon (Boolean on) method

Determines whether the thread is a daemon thread, or Cheng the line as a daemon thread (true)

There are two kinds of threads in Java, one is the daemon thread and the other is the user thread. A daemon thread is a special thread that serves as a convenient service for the operation of other threads, and the most typical application is GC threads. If there is only a daemon thread in the thread, the daemon thread will automatically destroy it.

Note: the Setdaemon () method must precede the start () method

Interrupt () method

When a thread is blocked, it throws an interrupt signal, which causes it to exit the blocking state without the blocked thread, and calling the interrupt () method has no effect

Join () method

The join () method is used to wait for the thread to destroy, and the join () method causes the thread that invokes the method (such as thread A.join (), a) to block indefinitely, if run as main in the main method, until the thread that calls the join () method is destroyed. The main thread will block indefinitely until the run () method of thread a finishes executing.

The join (long Millis) method means that the maximum time to wait for the thread to be destroyed is mills milliseconds, and the difference between this method and the sleep () method is:

The Sleep (2000) method does not release the lock, and the Join (2000) method releases the lock. The join () method internally uses the Wait () method, so the lock is freed. Join () is join (0)

 Public Final void throws interruptedexception {        join (0);    }

The source of the join (Long Mills):

1  Public Final synchronized voidJoinLongMillis)2     throwsinterruptedexception {3         LongBase =System.currenttimemillis ();4         Longnow = 0;5 6         if(Millis < 0) {7             Throw NewIllegalArgumentException ("Timeout value is negative");8         }9 Ten         if(Millis = = 0) { One              while(IsAlive ()) { AWait (0); -             } -}Else { the              while(IsAlive ()) { -                 LongDelay = Millis-Now ; -                 if(Delay <= 0) { -                      Break; +                 } - wait (delay); +now = System.currenttimemillis ()-Base; A             } at         } -}

static method of Thread

The static method of the thread class indicates that the thread of the operation is "the thread that is executing the code block where the static method resides." Thread uses static methods to operate on threads that are currently running on the CPU

CurrentThread () method

The CurrentThread () method returns an object reference to the currently executing thread

Note: The method of constructing a thread class, the static code block is called by the main thread, and the Run () method of the thread class is called by the application thread itself (provided that the thread start () method is started)

Additions: The This.xxx () method and the object in the Thread.currentThread.XXX () method are not necessarily the same. In other words, the thread that is currently executing is not necessarily the thread itself, such as the main thread, not the thread, that executes the thread's construction method.

Sleep (Long Millis) method

Let the currently executing thread hibernate millis milliseconds, this "currently executing thread" refers to the thread returned by the Thread.CurrentThread () method. According to the JDK API, "the thread does not lose ownership of any monitor", simply said that the sleep code context if locked, the lock is still in, but the CPU resources will be given to other threads.

Yield () method

Pauses the currently executing thread object and executes other threads. This pause will discard the CPU resources, and discard the CPU resources are not certain, it is possible to just give up the resources, it is possible to give up for a while, the CPU will execute

Interrupted () method

Test if the front thread has been interrupted, after execution has the ability to clear the status ID to False (the interrupt state of the thread is cleared by the method), in other words, if the method is called two times, the return must be false

Java multithreading (i) thread

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.