Java Multithreading (a) Introduction to------concept: http://www.cnblogs.com/ysocean/p/6882988.html
Java multithreaded Walkthrough (ii)------How to create processes and threads: http://www.cnblogs.com/ysocean/p/6883491.html
Java multithreaded Walkthrough (iii)------thread synchronization: http://www.cnblogs.com/ysocean/p/6883729.html
Java multithreaded Walkthrough (iv)------producers and consumers: http://www.cnblogs.com/ysocean/p/6896219.html
This blog describes the life cycle of threads.
A thread is a process that executes dynamically, and it also has a process from creation to death.
In the Thread class, there is an enumeration inner class:
The above information is represented by a picture as follows:
First Picture:
The second picture: waiting, waiting, blocking as blocking a state
1. New State (new): Create a thread with new, just allocate memory space in the heap
In the new state, the thread has not yet called the start () method, but there is only one thread object.
Thread t = new thread ();//This is the new state of the T thread
2. Operational status (runnable): New state calls the start () method to enter the operational state. And this is divided into two states, ready and running, respectively, indicating the readiness state and the running state.
Ready state: The thread object calls the start () method, waits for the JVM to Dispatch, (at which point the thread is not running)
Run state: Thread object gets JVM scheduler, runs multiple threads running in parallel if multiple CPUs are present
Note: Thread objects can only be called once by the Start () method, otherwise error: Illegathreadstateexecptiong
3, blocking state (blocked): The running thread for some reason to abandon the CPU, temporarily stop running, will enter the blocking state. At this point the JVM does not allocate the CPU to the thread and knows that the thread is back in a ready state before the opportunity goes to the running state.
Note: Blocking status can only be entered in the ready state, not directly into the running state
The blocking state is divided into two situations:
①, when thread A is in the operational state, attempting to acquire a synchronization lock, is obtained by the B thread, at which point the JVM puts the current a thread into the lock pool and a thread goes into a blocking state
②, when the thread is in the running state, an IO request is made and the blocking state is entered
4. Wait state (Waiting): Wait state can only be awakened by other threads, using the no parameter Wait () method
①, when the thread is in the running state, calls the Wait () method, at which point the JVM puts the thread into the waiting pool
5. Timed wait (timed waiting): Call the Wait (long time) or sleep (long time) method with parameters
①, when the thread is in the running state, calls the WITH parameter wait method, at which time the JVM puts the thread into the waiting pool
②, the current thread called the Sleep (long time) method
6. Termination status (terminated): Commonly known as the Death state, indicates thread termination
①, normal termination, execute run () method, end normally
②, forcing termination, such as calling the Stop () method or the Destory () method
③, abnormal termination, exception occurred during execution
Here are a few ways to learn more about threading:
1. Sleep (long Millis) thread hibernation: Let the executing thread pause for a period of time and enter the timed wait state.
static void sleep (Long Millis): After this method is called, the current thread discards the CPU resources, and within a specified time, the thread in which sleep is not given a chance to run, the thread in this state does not release the sync Lock (note and wait () differences, wait Will discard the CPU resources and also discard the sync lock)
This method is more used to simulate the network delay, so that multi-threaded concurrent access to the same resource when the error effect more obvious.
2. Join () Union thread: Indicates that this thread waits for another thread to finish (death) before execution, and the thread object is blocked after the join method is called. Which thread is written on, which thread is blocking
This is also called a union thread, which means that the current thread and the thread on which the current thread is located are combined into one thread
Package Com.ys.thread;class Join extends thread{@Overridepublic void Run () {for (int i = 0; i <; i++) {System.out.pri Ntln ("Play Music" +i);}}} public class ThreadTest {public static void main (string[] args) {//Create join Thread object Join Jointhread = new Join (); for (int i = 0 ; I < 10; i++) {System.out.println ("Play Game" +i), if (i==3) {Jointhread.start ();} if (i==5) {try {jointhread.join ();//force the join thread to know that the join is running, and main has a chance to run} catch (Interruptedexception e) { E.printstacktrace ();}}}}
Results:
Play games 0 play Games 1 play games 2 play Games 3 play Games 4 play Games 5 play Music 0 play Music 1 play Music 2 play Music 3 play Music 4 play Music 5 play Music 6 play Music 7 play Music 8 play the game 9 play games 6
Background thread (daemon): A thread running in the background that is intended to serve other threads, also known as a daemon thread.
①, the garbage collection thread of the JVM is the daemon thread.
②, Main method is foreground thread, not background thread
public static void Main (string[] args) {String mainthreadname = Thread.CurrentThread (). GetName (); System.out.println (mainthreadname); MainSystem.out.println (Thread.CurrentThread (). Isdaemon ());//false}
Characteristics:
①, if all the foreground threads are dead, the background thread automatically dies;
②, the foreground thread does not end, the background thread will not end;
The thread created by the ③, foreground thread is the foreground thread, and the thread created by the background thread is a background thread.
Thread.setdaemon (Boolean on) must be called before the method of start (). Otherwise you will get an error.
Priority of the thread:
Each thread has a priority, which helps the system determine the order in which the threads are redeployed.
Java thread priority is an integer, the value range is: 1 (thread.min_priority)-Ten (Thread.max_priority)
By default, each thread will be assigned a priority of norm_priority (5).
A thread with a higher priority is more important to the program, and the processor resources should be allocated before a low-priority thread. However, thread precedence does not guarantee the order in which threads are executed, and is very dependent on the platform.
Thread comity:
Yield () method: Indicates that the current thread object prompts the scheduler to give up CPU resources, but the scheduler is free to ignore the hint.
When the method is called, the thread object enters a ready state, so it is entirely possible that a thread invokes the yield () method, but the thread scheduler dispatches it again to execute.
It is clear from the documentation provided by Java7 that this method is seldom used in development, which is mainly used in debugging or testing, and it may help to reproduce the error in the context of multi-threaded competition.
The difference between the sleep () and yield () methods:
①, can make the current running thread discard the CPU resources, the opportunity to run to other threads
The ②, sleep method gives the opportunity to run on other threads, but does not consider other thread priorities; The yield method takes precedence over the higher-priority threads to run the opportunity
③, after calling the sleep method, the thread enters the timed wait state, and after the yield method is called, the thread enters the ready state.
Java multithreaded Walkthrough (v)------The declaration period of a thread