The state and method of the thread

Source: Internet
Author: User
Tags thread class

There are six thread states in the virtual machine, defined in Thread.state ():

T1.getstate (): View the current thread state.

1. New status, such as "thread t = new Thread ()

2, can run the state runnable, call the Start () method

3, blocking blocked, is waiting for the monitor lock, in order to enter a synchronized block/method,

4. Wait for Waiting,object () method, thread join () method, Locksupport Park () method

5. Timeout wait timed_waiting, call the wait with timeout (in), join (), Park () method

6. Termination status Terminated,run () method execution End

Instance methods in thread:

1, start (),

2, run (),

3, isAlive (), ensure that thread execution is complete.

12 Thread.sleep(100);//睡眠0.1s保证mt中的线程执行完。System.out.println("end == "+ mt.isAlive());

4, GetId (), has a long type globally unique thread ID generator threadseqnumber

5, GetName (), default to "Thread-threadinitnum" the way to name the newly generated thread, SetName

6, GetPriority (), default setpriority (5)

7, Isdaemon, Setdaemon (Boolean on), one is the user thread, one is the daemon thread

1234567 TestThread1 mt = newTestThread1();mt.setDaemon(true);//设置为守护线程,5s后必定结束。mt.start();Thread.sleep(5000);mt.join();//等待线程销毁时,再执行后面代码。main线程就会无限期阻塞直到mt的run()方法执行完毕,内部调用的是wait()方法。mt.interrupt();//未阻塞,不起作用。System.out.println("我离开thread对象再也不打印了,我停止了!");
<wiz_tmp_tag class= "Wiz-block-scroll" >

8, interrupt (), when blocking throws an interrupt signal, so that the thread exits the blocking state, no blocked threads, the call interrupt () method is not working?

9, isinterrupted (), test whether the thread has been interrupted, but does not clear the status identity

10, join (), is waiting for the thread to destroy.

Join (2000) indicates that the thread that calls the join () method waits up to 2000ms, and the lock is released during execution;

Static methods in thread:

1, CurrentThread (), returns a reference to the currently executing thread object.

2. Sleep (long Millis) to hibernate the currently executing thread (Thread.CurrentThread ()) in the specified milliseconds (pause execution)

3, yield (), pauses the currently executing thread object, and executes other threads. This pause is to discard CPU resources, and the time to discard the CPU is indeterminate. Execute yield () in run and the exit time is indeterminate.

4, Interrupted (), test when the front-line has been interrupted, after execution has the ability to clear the status ID to False.

1, the construction method of the thread class, static block is called by the main thread;

2, and the thread class's run () method is the application thread itself calls the

1, ()

The thread represented by this invocation method is the thread instance itself, which is thread_0 in the main function;

2, Thread.currentThread.XXX () or ()

This method of invocation represents the thread that is executing the code block where Thread.currentThread.XXX () resides;

3, the current implementation of the thread is not necessarily the thread itself;

Sleep (2000) does not release the lock, join (2000) releases the Lock:

1, join (2000) is to wait up to 2s, through constant polling, to see if the thread is active, destroy after understanding release lock;

2, Join () =join (0) is constantly asking for thread status until the thread destroys the join () method to end;

3, sleep is the native method, the wait time is over before releasing the lock, regardless of the thread state.

Run () and start () in the thread class:
Run () method: Call the Runnable object's run () method within this thread, can be repeated multiple calls, not the start thread, just the normal calling method, automatically wait for the call to end the execution of the following code;
Start () method: Starts a thread, calls the Runnable object's Run () method, and cannot start a thread multiple times;

Two thread call states:

After creation, enter new state;

After calling the Start method, enter the runnable state.

The wait () method corresponds to a thread that calls the sleep method and enters the timed_waiting state;

The wait () method releases the lock and enters the waiting state:

Notify () method does not release the lock, wait for the corresponding thread to enter the blocked state, waiting for the notify corresponding thread to run the end, wait for the corresponding thread to enter the runnable state;

The wait thread finishes executing and enters the terminated state.


The state and method of the 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: 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.