One: Creation of threads
1: Write a class that inherits the thread class, and then rewrite the run () method of the thread class later.
Steps
A Create a class that inherits the thread
B overriding the run () method in the thread subclass created
C Creating an instance of the thread subclass
D by invoking the run () method on the instance
2: Write a class to implement the Runable interface
A create an implementation runable interface
b Implement the Run () method in a class that implements an interface ianyrunable
C Create an instance of the Runable class
D Create a thread class that tells the runable instance to be passed in as a constructor parameter
E by invoking the instance of the thread class start () method
Second: The life cycle of the thread
The life cycle has several states
New state : The thread object has been created, and the start () method has not been invoked on it.
Operational Status : When a thread is eligible to run, but the scheduler has not selected it as the state in which the thread is running. When the start () method is called, the thread first enters the operational state. After the thread has run or has returned from blocking, waiting, or sleeping, it returns to the operational state.
Running state : The thread Scheduler selects a thread from a running pool as the current thread is in the state of the thread. This is also the only way that a thread goes into a running state.
wait/block/sleep state : This is the state at which the thread is eligible to run. In fact, this three-state combination is one, and its common denominator is that the thread is still alive, but there is no condition to run at the moment. In other words, it is operational, but if an event occurs, he may return to the operational state.
dead State : When the thread's run () method finishes, it thinks it is dead. This thread object may be alive, but it is not a separate thread. Once a thread dies, it cannot be resurrected. If you call the start () method on a dead thread, the java.lang.IllegalThreadStateException exception is thrown.
The thread sleeps Sleep (): Executes the method but the current thread stops running for several milliseconds, the thread is run into a non-operational state, and the sleep time continues to enter the operational state.
Three: Troubleshoot thread synchronization issues
1 using the Sync block synchronized (the lock object taken) {
}
2 methods of using synchronized.
Four: Threads communicate through Wait (), notify (), Notifyall () methods.
Java multithreaded programming