6 states of Thread
1) NEW: The initial state, the thread is built, but the start () method has not been called yet.
2) Runnable: Operating state, Java is called the system's running state and the readiness state is referred to as the running state
3) Blocked: Blocking state, indicating thread blocking Yu Yu (synchronized)
4) Waiting: Wait state, enter this state indicates that the thread needs to wait for other threads to make some action
(Interruption or notification, object.wait ())
5) Time_waiting: Timeout Wait state, this state is different from waiting, he is the sleep that can be returned automatically at a specified time causes
6) Terminal: Terminating state, indicating that the current thread has finished executing
Note that adding synchroized causes the block state
Lock causes waiting status
Daemon Threads
1) The main thread terminates after starting the thread Daemonrunner as the main method executes, and the virtual machine exits without a non-daemon thread in the Java virtual machine. All daemon threads in the virtual machine terminate immediately.
2) when building a daemon program, you cannot rely on the finally block to clean up resources
3) Suspend () stop () does not release resources at the time of invocation, but instead takes up resources into hibernation, which can easily cause deadlocks. Stop () does not release resources when a thread is terminated, so the thread may be guaranteed to enter an unhealthy state
wait/notification mechanism
1 Basic Flow
1) When you use Wait () notify () Notifyall (), you need to lock the object first.
2) After calling the Wait () method, the thread state is changed from runnable to waiting state, and the current thread is placed in the current object's wait queue
3) after the Notify () or Notifyall () method call, the waiting thread is still not returned from wait (), the thread that needs to call notify () or Notifyall () releases the lock, waiting for the thread to return from Wait ()
4) The Notify () method is to put the thread from the waiting queue into the synchronization queue, while the Nofityall () method is to put all the waiting threads from the waiting queue into the synchronization queue, and the moved thread changes from the waiting state to the blocked state
5) Returning from the Wait () object is the prerequisite for acquiring the lock on the object
2 Classic pseudo-code
Wait party pseudo code
1) Synchronized (object) {
while (insufficient judgment condition) {
Object. await ();
}
DoSomething ();
}
Wake-up party pseudo-code
2) synchronized (object) {
Judging the conditions of satisfaction
Object. Notify ();
}
3 pipe input/output stream
The difference between a pipe input/output stream and a normal text input or output stream or a network input/output stream is that he is primarily used for data transfer between threads, and the primary media is memory
Out.conn
Use of the 4join () method
If a thread a uses thread.join () then it means that the current thread a waits for the thread thread to terminate before returning from the join () method
5 Wait Timeout
A method is called to wait for a period of time, if the method can return a result within a given time, the result is returned immediately, and vice versa returns the default result
Pseudo code
Public synchronized Object get (long millons) {
Long future = System.millons+millons
Long remaining=millons;
while (result==null&&remaining>0) {
Wait (remaining);
Remaining=future-system.millons;
}
return result;
}
6 thread-Safe connection pool instances
public class connectionpool{
Private LinkedList pool = new LinkedList ();
private static int initcapacity=10;
Public ConnectionPool () {
Initializing the connection pool
for (int i=0;i<initcapacity;i++) {
Pool.addlast (Connectiondriver.getconnection ());
}
}
Releasing the connection pool
public void Releaseconnection (Connection Connection) {
Synchronized (Pool) {
Pool.addlast (connection);
This.notify ();
}
}
}
Get Connection Pool
Public Connection fetchconnection (long millons) {
long remaining = millons;
Long future = system.currentmillons+millons;
Synchronized (Pool) {
while (Pool.isempty () &&remaining>0) {
Pool.wait (millons);
Remaining= system.currentmillons-future;
}
Connection Conn=null;
if (!pool.isempty ()) {
conn = Pool.removefirst ();
}
Return conn;
}
}
This article is from the "ITER Summary" blog, please be sure to keep this source http://5855156.blog.51cto.com/5845156/1958232
Concurrent Programming Art Reading notes-fourth chapter threading Basics