求高手:看看简单代码的问题

FengPrince 2012-09-16 05:40:29

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方法时已经设置了中断状态,该方法是否抛出异常与实现有关。。。
...全文
340 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
FengPrince 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

个人浅见:

"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."
查jdk 1.……
[/Quote]
哈哈,结贴的时候发现你新回复了,我已经找到问题了,就是你说的那样。
要是你早点回我就好了,呵呵,不过,还是要谢谢你。
分给你吧。。。。
FengPrince 2012-09-17
  • 打赏
  • 举报
回复
Hi,guys,I've made it!
It doesn't run as I expected in that I comprehend the await() incorrectly!
I'd otherwise have released the lock with unlock().
With respect to the red-highlight sentence,I regard I've got it!
Sorry,it's my fault and the library is non-buggy.

BTW,I've found the same problem at stackoverflow despite being unresolved.

Terminate the post and give credits,now!!!
nmyangym 2012-09-17
  • 打赏
  • 举报
回复
个人浅见:

"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."
查jdk 1.6中文解释:
“在所有情况下,在此方法可以返回当前线程之前,都必须重新获取与此条件有关的锁。在线程返回时,可以保证它保持此锁。 ”

程序在执行a.interrupt();后,a线程从等待状态返回,(因为这是con上的锁是空闲的,a线程持有该锁)。而程序中没有释放,(程序没有lock.unlock()),所以锁就一直被线程a占用着了。

而当执行aa.interrupt()时,线程aa因为无法得到con锁,所以不能从“等待”中返回。这就是为什么aa不能中断的原因。

可以在类B的f()中加上释放锁的语句,aa就可以正常中断了。

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!");
}
finally
{
lock.unlock(); //释放锁.
}
}



liujun3512159 2012-09-16
  • 打赏
  • 举报
回复
这已经不简单了
FengPrince 2012-09-16
  • 打赏
  • 举报
回复
真郁闷,没有人知道么?
CSDN高手哪去了??
FengPrince 2012-09-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

interrupt 恐怕没有你想的那么强悍。

建议你参考下这哥们的Blog,写的相当不错:
http://www.ticmy.com/?p=31
[/Quote]
我知道,interrupt有点复杂。
我的目的不是想终止线程,而是中断线程的等待状态。
按照API说明,await在线程阻塞期间如果有其它线程中断它,该线程就会抛出InterruptedException异常。但是事实上并不是这样,我怀疑是不是API库有bug!!
zz_yi 2012-09-16
  • 打赏
  • 举报
回复
变量名不要用lock
MiceRice 2012-09-16
  • 打赏
  • 举报
回复
interrupt 恐怕没有你想的那么强悍。

建议你参考下这哥们的Blog,写的相当不错:
http://www.ticmy.com/?p=31
内容概要:本文研究了基于DPWMA调制与正负序分离的ANPC三电平并网逆变器前馈控制策略,旨在解决传统三电平逆变器存在的谐波含量高、电网不平衡工况适应性差及动态响应速度不足等问题。通过采用有源中点箝位(ANPC)三电平逆变器拓扑,结合双极性倍频脉宽调制(DPWMA)、正负序分离锁相技术和电网电压前馈控制,构建了一套一体化的高性能并网控制体系。该体系不仅优化了逆变器的开关动作机制,改善了输出电压电流的谐波特性,而且通过精确的相位同步和扰动补偿,显著提高了系统的动态响应能力和抗扰性能。仿真结果显示,所提出的控制策略能有效降低并网谐波含量,提升锁相精度与系统动态稳定性,确保在复杂电网工况下的高质量稳定并网。 适合人群:具备一定电力电子基础知识和仿真技能的研发人员,尤其是从事新能源发电、储能系统、柔性输电等领域研究的专业人士。 使用场景及目标:①研究和开发高性能并网逆变器,特别是针对大功率、高电能质量要的应用场景;②探索如何通过先进的调制和控制策略来提高并网逆变器对电网扰动的适应性和响应速度;③为相关领域的学术研究和技术开发提供理论依据和实践指导。 阅读建议:建议读者结合实际的仿真软件(如MATLAB/Simulink)进行实践操作,以便更好地理解和掌握文中提到的各种控制策略的具体实现方法。同时,鼓励读者关注最新的研究成果和发展趋势,不断深化对该领域的认识。

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧