The previous blog is a simple introduction to the concept of multithreading in Java, the difference between the process, two ways to create, state and get the thread name and so on. This article goes on to introduce multithreading in Java. Mainly from a few aspects of introduction.
1 common methods for threading Classes 1.1 start ()
Start thread execution Run method
1.2 Thread.Sleep ()/sleep (Long Millis)
The time at which the current thread sleeps/millis (Millis Specifies that the sleep time is its minimum non-execution time, since sleep (Millis) is not guaranteed to be dispatched immediately by the JVM when Sleep arrives);
Sleep () is a static method, so he does not stop other threads from sleeping, and thread sleep () does not lose owning object locks.
Function: Keep the object lock, give up the CPU, call the purpose is not to let the current thread alone occupy the CPU resources obtained by the process, to leave a certain time for other threads to execute the opportunity.
1.3 Thread.yield ()
The yield () method will not work if there is no equal priority for the thread that gives the CPU the right to execute the opportunity for other threads to run the same priority thread (but does not guarantee that the current thread will be dispatched again by the JVM to bring the thread back into the running state).
1.4
Thread.Join ()
When the thread is forced to execute, other threads cannot run during the execution of the thread, and other threads must be executed before the thread executes.
1.5
Object.wait ()
Causes the thread to enter into the waiting pool (waiting pools), while losing the machine lock of the object---temporarily, and returning the object lock after wait. The current thread must have a lock on the current object, and if the current thread is not the owner of the lock, the illegalmonitorstateexception exception is thrown, so wait () must be called in the synchronized block.
1.6 object.notify ()/notifyall ()
Wakes the first thread/all threads waiting in the current object waiting pool. Notify ()/notifyall () must also have the same object lock, or a Illegalmonitorstateexception exception will be thrown.
The difference between 1.7object.wait () and Thread.Sleep (long)
1.sleep () method
Sleep () causes the current thread to go into a standstill (blocking the current thread), giving up the use of the cup to allow the current thread to seize the CPU resources obtained by the process for a certain amount of time for other threads to execute.
Sleep () is the static (static) method of the thread class, so he cannot change the machine lock of the object, so when the sleep () method is called in a synchronized block, the thread sleeps, but the object's machine lock and the wood is released. Other threads cannot access this object (even if asleep also holds the object lock).
After the sleep () sleep time expires, the thread does not necessarily execute immediately, because other threads may be running and are not scheduled to abort execution unless the thread has a higher priority.
2. Wait () method
The wait () method is a method in the object class, and when a thread executes to the wait () method, it enters into a waiting pool associated with the object and loses (frees) the object's machine lock (temporarily loses the machine lock, wait (long timeout) The time-out is also required to return the object lock, and other threads can access it.
Wait () wakes the threads in the current waiting pool using notify or notifyalll or specifying sleep time.
Wiat () must be placed in the synchronized block, otherwise the "java.lang.IllegalMonitorStateException" exception will be thrown when the program runtime. To summarize:
Sleep () keeps the object locked while still occupying the lock.
When Wait () sleeps, the object lock is released.
2 Thread-terminating 3-Thread synchronization issues
Reference:
Java multithreading look at me this one is enough.
Java Multithreading-Concept & Create Start & interrupt & Daemon Thread & priority & Thread State (one of multithreaded programming)
Java Thread Summary
Java Multi-Threading operations