√
√
Will move to “ready to run” state
after being notice, after timeout or after being interrupted
Blocked on I/O
√
Will move to “ready to run” after I/O
Condition changes(for example,a byte of date is read)
Blocked on sync
√
Will move to “ready to run” when lock is acquired(passes synchronized statement)
Threads can be blocked in one of four states. When a thread executes Thread.sleep(), it blocks until the specified number of milliseconds passes or until it is interrupted by another thread. When a thread encounters a wait() statement (see Chapter 8, “Inter-thread Communication,” for more on the wait/notify mechanism), it blocks until it is notified, interrupted, or the specified number of milliseconds elapses (if a timeout was specified).
There are many ways a thread can block waiting on different I/O methods. One common way is the read() method of InputStream. This method blocks until a byte of data is read from the stream. It can block indefinitely, and no timeout can be specified.
A thread can also block waiting to acquire exclusive Access to an object’s lock. The synchronized statement and synchronized method modifier (see Chapter 7, “Concurrent Access to Objects and Variables,” for more on synchronization) are used to control concurrent access by more than one thread to a section of code. A thread will block on synchronized until it gets the specified lock. It can block indefinitely, and no timeout can be specified.
有四种方式线程会被阻塞:
1、调用Thread.sleep(),时间完成、被其他线程打断时改变状态
2、wait/notify时线程遇到wait()时,当被notified,被别的线程打断,或者特定时间结束时改变状态
3、I/O阻塞
4、锁阻塞,同步锁时,线程得到此锁时改变状态
Notice that not all blocked states are interruptible. When a thread is blocked waiting to read a byte of data from an InputStream, it will not respond to interrupts (see Chapter 15, “Breaking Out of a Blocked I/O State,” for some techniques for dealing with this). When a thread is blocked waiting to get the lock required by a synchronized statement, it also will not respond to interrupt requests (see Chapter 17, “BooleanLock Utility,” for a technique that deals with long waits for locks).
不是所有的阻塞状态都可以被打断的,I/O阻塞和锁阻塞是不能被中断的
Thread.yield()
To help ensure that other threads in the VM get a turn to run on the processor, a thread can voluntarily give up its turn early. If a thread invokes the static method Thread.yield(), the thread scheduler will swap it off the processor and allow another thread to run. It is likely (but not guaranteed) that only threads having a priority equal to or greater than the one that yielded control will be considered by the thread scheduler.
Thread.yield():线程自愿让出cpu控制权
A thread implicitly yields the processor when it goes to sleep or otherwise blocks. The Thread.yield() method allows a thread to specify other times that are convenient for it to pause to allow other threads access to the processor. If you have a thread that frequently blocks on its own, there is no need to make Thread.yield() calls. But, if you have a thread that is performing a long non-blocking calculation, an occasional call to Thread.yield() can help split up the processor resources among the other threads. Be careful not to overuse Thread.yield() as some system overhead is incurred to perform a context switch between threads. As a rough guideline, try to avoid calling Thread.yield() more than five times per second.
当线程阻塞时(四种原因),线程隐式自行yield。如果线程本身经常阻塞,就没有必要显式调用yield,如果线程执行一个长时间不阻塞的大运算,调用一下能够给其他进程平分一下处理器资源。注意:不要频繁调用,因为yield时系统做上下文切换,很是耗费系统资源和时间,避免疫秒内调用五次以上的yield。
实例:PriorityYield.Java
/*
* Created on 2005-7-8
*
* Java Thread Programming - Paul Hyde
* Copyright ? 1999 Sams Publishing
* Jonathan Q. Bo 学习笔记
*
*/
package org.tju.msnrl.jonathan.thread.chapter1;
/**
* @author Jonathan Q. Bo from TJU MSNRL
*
* Email:jonathan.q.bo@gmail.com
* Blog:blog.csdn.net/jonathan_q_bo
* blog.yesky.net/jonathanundersun
*
* Enjoy Life with Sun!
*
*/
public class PriorityYield {
private volatile int counter;
private Thread innerThread;
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有