Multi-threading thread: refers to the process of an implementation flow.
The difference between threads and processes: each process requires the operating system to assign a separate memory address space, and the same process all the threads in the same one address space to work, these threads can share the same piece of memory and system resources.
How do I create a thread?
There are two ways to create a thread, as follows:
1, extending java.lang.Thread class
2 to achieve Runnable interface
Thread class on behalf of the thread class, its two most important methods are:
run () - contains the thread to run code that is executed when the
Start () - used to start a thread
A thread can only be started one time. The second starts an exception will be thrown java.lang.IllegalThreadExcetpion
Inter-thread state conversion (as shown)
New Status: The new statement creates a new thread object is in the state, this time it like any other java object, only the memory is allocated in the heap ready state: When a thread is created after the other thread calls its start () method, which thread into the ready state. In view of this state to run the thread in the pool, waiting to get right to use the CPU to run the state: in this state of the thread take up CPU, the implementation of the code for the blocking state: When the thread is in blocking state, java virtual machine will not thread allocation CPU, until the thread re-enter the ready state, it is a chance to go running.
Blocking state is divided into three situations:
1, is located in the blocking state of the object to wait for the pool: When the thread is running, if you perform a certain object's wait () method, java virtual machine on the back to put the thread into the waiting pool of the object
2, located in the object lock in the blocking state, when the thread is running when trying to get an object's synchronization lock, if the object's synchronization lock has been occupied by the other threads, JVM will put this thread in this object The Suo pool.
3, the other blocking states: The current thread is executed sleep () method, or the calling of the other thread join () method, or issued by I / O request, it will enter the state.
The state of death: When the thread exits the run () method, entered the death state, the thread end of life cycle.
Or a normal exit
Or encounter an exception out of
Thread class isAlive () method to determine whether a thread is alive, when the thread is in the state of death or a new state, the method returns false, the rest of the state, the method returns true.
Thread scheduler thread scheduling model: time-sharing scheduling model and the preemptive scheduling model
JVM using preemptive scheduling model.
The so-called multi-threaded run concurrently, in fact, refers to a macro perspective, each thread in turn to obtain the right to use CPU, respectively, carrying out their respective mandates.
(Thread scheduling is not a cross-platform, it not only depends on the java virtual machine, it also depends on the operating system)
If you want to explicitly allow a thread to another thread to run an opportunity to one of the ways you can take the following
1, adjust the priority of each thread
2, so that in a running thread calls Thread.sleep () method
3, so that in a running thread calls Thread.yield () method
4, so that the thread is running another thread calls join () method
Adjust the priority of each thread
Thread class setPriority (int) and getPriority () methods were used to set priorities and reading priority.
If you want the program can shift the value to each operating system, should ensure that the priority of the thread in the set, only the use of MAX_PRIORITY, NORM_PRIORITY, MIN_PRIORITY these three priority.
Thread Sleep: When the thread is executed in the operation of sleep () method, it will abandon the CPU, to block the state.
Thread concession: When the thread is running in the implementation of the Thread class of the yield () static method, this time with the same priority, if the other threads in the ready state, then the yield () method will run the currently running thread into the pool make another thread to run. If you can not have the same priority thread is running, then the yield () method to do nothing.
Sleep () method and yield () method of Thread class are static methods, will cause the currently running thread to give up in the CPU, to run the chance to give other threads, the difference is:
1, sleep () method will give the opportunity to run other threads, regardless of the priority of other threads, it will give the opportunity to run a lower thread; yield () method will give the same priority or higher priority thread a chance to run.
2, when the thread executed sleep (long millis) method, will be transferred to blocking state, the parameter millis specify the sleep time; when the thread executed yield () method, will be transferred to ready state.
3, sleep () method declaration throws InterruptedException exception, but yield () method does not declare any exception thrown
4, sleep () method than the yield () method has better portability
Other threads to wait for the end: join ()
The currently running thread can call another thread join () method, the currently running thread will be transferred to blocking state until the end of another thread to run, it was restored to normal operation.
Timer Timer: The java.util package in the JDK provides a utility class Timer, it can from time to time to perform a specific task.
Thread synchronization atomic operation: According to the Java specification, for the basic types of assignment or a return value of operation is atomic operation. But here's the basic data types does not include long and double, because the JVM can see the basic storage unit is 32, while the long and double, should use 64 to represent. It can not be completed in one cycle.
Increment operations (+ +) is not atomic operation, as it relates to a read and a write.
Atomic operations: from a group of related operations completed, these operations may be manipulated with other threads to share resources, in order to ensure correct computing results of a thread in the implementation of atomic operations In the meantime, other measures should be taken to make the other threads can not be manipulation of shared resources.
Synchronized block of code: In order to ensure that each thread can be the normal execution of atomic operations, Java introduced a synchronization mechanism, the specific practice is on behalf of the atomic program code with synchronized pre-marked, so that the code is known as the synchronized block of code.
Sync Lock: Each JAVA object has and only a synchronization lock, at any time up to allow only one thread owns the lock.
When a thread attempts to access a synchronized (this) marked block of code, you must obtain this keyword references the object's lock, in the following two cases, this thread has a different fate.
1, if the lock has been occupied by other threads, JVM will put this thread into the lock of the object pool. This thread into the blocking state. Lock the pool may have a lot of threads, wait until the other thread to release the lock, JVM will be randomly removed from the lock a thread pool to make this thread has a lock, and go to ready state.
2, if the lock has not been occupied by other threads, this thread will get the lock to begin implementation of the synchronized block of code.
(Under normal circumstances in the implementation of synchronized block of code will not release synchronization lock, but there are special circumstances would be to release the object lock, such as in the implementation of synchronized block of code has encountered an exception which led to the termination of the thread, the lock will be released; in the implementation of the code block when the the implementation of the lock-owned object wait () method, the thread will release the object lock, enter the object's wait for the pool)
Thread synchronization features:
1, if a synchronized block of code and non-synchronized block of code simultaneously shared resources, will still result in a shared resource competition. Because when a thread executing a synchronized block of code object, other threads can still be the implementation of an object non-synchronous code block. (So-called synchronization between threads, to refer to different threads in the implementation of the same synchronized block of code of an object, because to obtain the object's synchronization lock to contain each other)
2, each object has a unique synchronization lock
3, in front of static methods you can use the synchronized modifier.
4, when a thread is started when the synchronized block of code does not mean that must run uninterrupted into the synchronized block of code thread can perform Thread.sleep () or perform Thread.yield () method, this time it does not release the object lock, just opportunities to run the other thread.
5, Synchronized statements will not be inherited, if a modification of the method with the synchronized class quilt cover, then the sub-class, this method does not keep pace, unless synchronized modifier.
1, this class of object can simultaneously be multiple thread-safe access.
2, each thread can be a normal implementation of atomic operations, get the correct results.
3, in each thread of the atomic operations are completed, the object in a logical and reasonable state.
To release the object lock:
1, the implementation of synchronized block of code will end the release of the object lock
2, in the implementation of the synchronized block of code in the process of thread encountered an exception which led to the termination, the lock will be released
3, in the implementation of the synchronized block of code in the process of implementation of the lock object belongs wait () method, the thread will release the object lock, waiting to enter the object pool.
Deadlock when a thread is waiting for a lock held by another thread, while the latter has been the first one waiting thread holds a lock when the deadlock occurs. JVM does not monitor nor try to avoid this situation, thus ensuring deadlock does not occur became the programmer's responsibility.
How to avoid deadlock a common rule of thumb is: when several threads access a shared resource must be A, B, C, to ensure each thread with the same order to access them.
Java.lang.Object class provides two methods for thread communication
1, wait (): the implementation of this method is the thread releases the object lock, JVM will wait on an object in the thread pool. The thread is waiting for other threads wake up
2, notify (): the implementation of the method of the object to wait for the thread wake up in the pool waiting for a thread, JVM from a pool of randomly selected objects to wait for a thread, lock it to the object pool.