JAVA-based knowledge 05 threads

 

 

1. Please describe briefly threads sleep () method and the yield () method What is the difference?

Reply:

①sleep () method does not consider other threads to run when the opportunity priority thread, so that will give the opportunity to the low priority thread to run; yield () method will only give the same priority or higher priority thread to run opportunity;

After the execution thread ② sleep () method into the blocked (blocked) state, and after the implementation of yield () method into the ready (ready) state;
③ sleep () method declaration throws InterruptedException, while the yield () method does not declare any exception ;
④ SLEEP () method than the yield () method (associated with the operating system, CPU scheduling) more portable.

2. Please state thread synchronization method you know

 A: wait (): make a thread in a wait state, and releases the lock held by the object.
sleep (): make a running thread in a sleep state, it is a static method call this method to capture InterruptedException exception.
notify (): wake up a thread in a wait state, noting that at the time this method is called, does not exactly wake up one thread wait state, but which is determined by the JVM thread wakes up, but not by priority.
Allnotity (): wake up a thread into a wait state at all, note that not all wake up threads to lock an object, but let them compete.

3. Please explain sleep () and wait () What is the difference?

 A: sleep is a method of the Thread class (Thread), leading to this thread to suspend the specified time, the opportunity to perform other threads, but monitoring status remains, to the post will be automatically restored. Call sleep will not release the object lock.
wait is a method of the Object class, object calls this method leads to wait this thread to give the object lock, waiting to enter the pool waiting for a lock for this object, and only issued after notify method (or notifyAll) for this thread to lock this object before entering the target pool to get ready object lock into operation.

4. Please describe in detail about the threads from creation to the death of several states have what?

 A: 1. New (new): create a new thread object.
 2. run (runnable): After the thread object is created, other threads (such as main thread) calls the start () method of the object. The thread state is located runnable threads in the pool, waiting to be selected thread scheduling, acquiring the right to use the cpu.
 3. Run (running): runnable (Runnable) obtained threads of cpu time slice (timeslice), execution of program code.
 4. blocked (block): blocking state is the thread for some reason to give up the right to use the cpu, that is let out of the cpu timeslice, temporarily stop running. Until the thread becomes runnable (runnable) state, you have a chance to get cpu timeslice to run (running) state again. Where blocking three categories:
. (A) blocked waiting for: o a thread of execution run (running) a wait () method, JVM will thread into the wait queue (waitting queue) in.
(B) synchronous blocking: thread running (running) when acquiring synchronization lock object, if the synchronization lock is occupied by another thread, the JVM will lock into the thread pool (lock pool) in.
(3) Other blocking: Run (running) thread execution Thread sleep (long ms) or t join () method, or issue the I / O request, the JVM will set the thread is blocked. When sleep () timeout, the Join () wait for a thread to terminate or times out, or when the I / O processing is complete, the thread may be re-run into (Runnable) state.
5. death (dead): Thread run (), main () method has finished executing, or due to abnormal withdrew from the run () method, the thread end of the life cycle. Thread of death can not be resurrected again.

Guess you like

Origin www.cnblogs.com/9797ch/p/11610251.html