Life Cycle Java multithreading 02-- threads and common methods, you have mastered it

In the last chapter, we will introduce some basic knowledge, thread creation and termination of threads. This issue will bring the life cycle of threads and common methods you. I am concerned about the number of public "side Java Code" to learn more Java-related knowledge.

Thread Life Cycle

A thread is not being created immediately began to perform, nor is it has been in a state execution. Throughout the life cycle of the thread will experience New (New),
Ready (Runnable), running (Running), blocked (Blocked) and destroy (Terminated) 5 states.

Thread lifecycle .PNG

New

It refers to create a new thread object using the new keyword after the thread is in the new state. At this point its only allocate memory by the JVM, and initializes the value of its member variables.

Ready

When a thread object to call the start () method, the thread is in the ready state. The JVM call stack and program counter to create a method, waiting to be scheduled to run.

run

Thread of execution if the thread is in the ready state in CPU clock, started run () method, the thread is running.

Clog

Blocking state means that thread for some reason to give up the right to use the CPU to temporarily stop running. Case of obstruction of three categories:

  1. Waiting blocks (o.wait-> Wait column) : o.wait thread of execution run (running) () method, JVM will thread into the wait queue (waitting queue); and
  2. Synchronous blocking (lock-> lock pool) : thread running (running) when acquiring synchronization lock object, if the synchronization lock is occupied by another thread, the JVM will lock into the thread pool (lock pool) in.
  3. Other blocking (SLEEP / the Join) : running (running) thread execution Thread.sleep (long ms) or t.join () method, or issue the I / O request, the JVM will set the thread is blocked. When sleep () timeout, the Join () wait for a thread to terminate or times out, or when the I / O processing is complete, the thread may be re-run into (Runnable) state.

destroy

If the thread is finished or after normal thread is mandatory early termination or abnormal lead to an end, the thread will be destroyed, the release of resources.

Thread common method

From the thread of the life cycle, we have learned some commonly used methods thread. There are common methods thread wait, notify, notifyAll, sleep, join, yield and so on.

wait

  • Role : Forced to wait for a thread, the thread enters WATING state, only to wait another thread or interrupt notification will be returned;
  • Note : Calling wait () method, the object will release the lock. Thus, wait a method generally used in the synchronization code synchronization method or blocks.

sleep

  • Role : to force a thread to sleep for N milliseconds, the thread enters TIMED_WATING state, only to wait another thread or interrupt notification will be returned;
  • Note : wait and different methods of sleep is not currently occupied lock release.

yield

  • Role : the current thread yields the CPU execution time slice, slice again competing for CPU time with other threads;
  • Note : Under normal circumstances, a higher priority thread has a greater likelihood to compete successfully get CPU time slice, but this is not absolute, and some are not sensitive to the operating system thread priority.

interrupt

  • Role : interrupt a thread, its intention is to give this thread a notification signal, it will affect an internal thread interrupt flag;
  • Note :
  1. interrupt does not change the state of the thread, that thread will not be forced into the blocking, termination state;
  2. If the thread is TIMED-WATING state, then call interrupt () method throws InterruptedException, so that the thread end early TIMED-WATING state;
  3. Many declare throw InterruptedException methods (such as Thread.sleep (long mills method)), before throwing an exception, interrupt flag is cleared, so the exception is thrown, call isInterrupted () method will return false;
  4. You can call thread.interrupt () method, the internal thread run method can be elegant terminate the thread according to the value thread.isInterrupted () of.

join

  • Role : the current thread into the blocked state until the end of another thread, the current thread is then blocked by the state to the ready state;
  • Usage scenarios : the main thread promoter thread, the main thread waits for the child thread's return status.
System.out.println(Thread.currentThread().getName() + "线程运行开始!");
Thread6 thread1 = new Thread6();
thread1.setName("线程 B");
thread1.join();
System.out.println("这时 thread1 执行完毕之后才能执行主线程");

notify

  • Action : notify Object class () method, a single thread wake monitor waiting on this object;

    If all threads are waiting on this object will be selected wake up one thread, the choice is arbitrary, and wake up before the thread, the thread calling wait () method, are waiting on an object's monitor, until the current locking thread to give up on this subject, in order to continue to be awakened thread, the thread will be woken up in a conventional manner with all other active threads competing to synchronize on the object. Similar methods also notifyAll (), wakes up all threads waiting on this monitor.

Other thread method

  • isAlive () : determine whether a thread alive;
  • activeCount () : the number of threads in the program active;
  • the enumerate () : thread enumeration program;
  • currentThread () : Get the current thread;
  • isDaemon () : determines whether a thread is a daemon thread;
  • setDaemon () : Set a thread is a daemon thread;
  • setName () : set a name for the thread;
  • getPriority () : Gets the priority of the current thread;
  • setPriority () : set the priority of the current thread.
    注意:线程优先级高,被CPU调度的概率大,但不代表一定会运行,还有小概率运行优先级低的线程。

Multithreading and concurrency Series Recommended

Java multi-threaded 01-- thread creation and termination, you will in several ways

Guess you like

Origin www.cnblogs.com/weechang/p/12507989.html