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

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方法时已经设置了中断状态,该方法是否抛出异常与实现有关。。。
...全文
335 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
内容概要:本文提出一种基于鱼鹰优化算法(OOA)优化的CNN-BiGUR-Attention混合模型,用于提升短期风电功率预测的精度与稳定性,采用Matlab实现代码仿真。该模型融合卷积神经网络(CNN)提取输入数据的局部空间特征,利用双向门控循环单元(BiGUR)捕捉风速、功率等时间序列的前后向动态依赖关系,并引入注意力机制自适应强化关键时间步的特征权重,从而增强模型对非平稳风电数据的表征能力;进一步,采用OOA算法对模型超参数进行全局寻优,有效提升模型收敛速度与泛化性能。研究基于实际风电场历史数据开展实验验证,结果表明,该方法相较传统模型在预测精度、鲁棒性和误差抑制方面表现更优,适用于高比例可再生能源接入背景下的电力系统调度需。; 适合人群:从事新能源发电预测、电力系统优化调度、智能算法与深度学习融合应用等方向的科研人员及工程技术人员,尤其适合具备Matlab编程能力、熟悉时间序列建模与深度学习框架的研究者。; 使用场景及目标:①实现风电场短期功率高精度预测,支撑电网安全稳定调度与能量管理;②为深度学习模型结构设计与智能优化算法联合调参提供实践范例;③推动人工智能技术在可再生能源预测、智能电网运行等领域的落地应用。; 阅读建议:建议结合提供的Matlab代码深入理解CNN-BiGUR-Attention网络架构搭建、注意力机制实现方式及OOA优化流程,重点关注数据预处理、模型训练与参数调优细节,可通过替换不同风电数据集进行对比实验,进一步掌握模型迁移能力与适应性。

62,622

社区成员

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

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