求高手:看看简单代码的问题
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
class B
{
Lock lock=new ReentrantLock();
Condition con=lock.newCondition();
void f()
{
lock.lock();
System.out.print(Thread.currentThread().getName());
System.out.println(">>I'm running");
try { con.await(); }
catch(InterruptedException ex)
{
System.out.print(Thread.currentThread().getName());
System.out.println(">>I'm interrupted!");
}
System.out.print(Thread.currentThread().getName());
System.out.println(">>exit!");
}
}
class A extends Thread
{
B b;
A(B b){this.b=b;}
public void run()
{
b.f();
}
}
public class Test
{
public static void main(String[] args)
{
B b=new B();
A a=new A(b);
a.start();
A aa=new A(b);
aa.start();
try{Thread.sleep(1000);}
catch(InterruptedException ex){/*Deal with it!*/}
a.interrupt();
aa.interrupt();
}
}
为什么a或aa线程其中有一个不能中断?
想不通问题出在哪儿,求高手!!
API docs:
The lock associated with this Condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
Some other thread invokes the signal() method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
Some other thread invokes the signalAll() method for this Condition; or
Some other thread interrupts the current thread, and interruption of thread suspension is supported; or
A "spurious wakeup" occurs.
In all cases, before this method can return the current thread must re-acquire the lock associated with this condition. When the thread returns it is guaranteed to hold this lock.
If the current thread:
has its interrupted status set on entry to this method; or
is interrupted while waiting and interruption of thread suspension is supported,
then InterruptedException is thrown and the current thread's interrupted status is cleared. It is not specified, in the first case, whether or not the test for interruption occurs before the lock is released.
最后一句话(红色高亮)不是很理解,意思是不是如果在进入await方法时已经设置了中断状态,该方法是否抛出异常与实现有关。。。
...全文
相关推荐
java代码-Java简单递归求1-100相加求和
java代码-Java简单递归求1-100相加求和
求字符串里面数字之和
无意看到别人面试问了很简单的问题,求字符串里面数字之和,所以自己来实现下。 例子: 比如字符串:aaaa13sseui9ddu78ff4sss 里面的字符串数字是13、9、78、4 得到的和为104 代码如下: package com.sangfor.tree; public class SumByString { public static int sumByString1(S
简单矩阵计算的源代码
做简单矩阵计算的源代码,只能做加减乘除求逆转置等简单计算,如有问题,请在https://blog.csdn.net/xk6891/article/details/82081931留言交流
分治法求最大字段和问题——C语言代码
课程的随堂作业,C语言的,用dev就能运行,萌新代码,勿喷,仅仅帮助不想写作业的朋友方便一下,反正老师也不会仔细检查的
求最大数的简单程序设计(源码)
本文主要介绍了求最大数的简单程序设计(源码)


Java SE

