InterruptCheck.java
public class InterruptCheck extends Object {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
t.interrupt();
System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted());
try {
Thread.sleep(2000);
System.out.println("was NOT interrupted");
} catch ( InterruptedException x ) {
System.out.println("was interrupted");
}
//在这里因为sleep抛出了异常,所以它清除了中断标志
System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
}
}
class ThreadTest implements Runnable
{
private int tickets = 100 ;
public void run()
{
while(true)
{
//假如一个线程未执行完,而另一线程又运行此代码,则会出现不同步问题
//为说明问题,将线程延迟
if(tickets>0)
{
9
7
3
1
2
3
4
5
6
7
8
9
10
4
8
: