【发布时间】:2012-08-01 10:53:27
【问题描述】:
当我运行以下代码时,这是一个使用 java netbeans 编译器的多线程示例,我的电脑挂起。
为什么会这样?
class clicker implements Runnable
{
int click=0;
Thread t;
private volatile boolean runn=true;
public clicker(int p)
{
t=new Thread(this);
t.setPriority(p);
}
public void run()
{
while(runn)
click++;
}
public void stop()
{
runn=false;
}
public void start()
{
t.start();
}
}
public class Hilopri
{
public static void main(String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clicker hi=new clicker(Thread.NORM_PRIORITY+2);
clicker low=new clicker(Thread.NORM_PRIORITY-2);
low.start();
hi.start();
try
{
Thread.sleep(500);
}
catch(Exception e)
{
low.stop();
hi.stop();
}
try
{
hi.t.join();
low.t.join();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Low"+low.click);
System.out.println("High"+hi.click);
}
}
【问题讨论】:
-
@user1560596 请不要回滚到您最初无法阅读的帖子。
-
@user1560596 很多好心的人都试图帮助您获得问题的答案。与其将问题恢复到其原始状态(由于格式不佳,许多人会忽略甚至否决),而是将其作为如何有效提问的示例:)
-
是的 Grundlefleck 我会记住这一点
-
停止回滚更改。或者更确切地说,在锁定问题后停止回滚。
标签: java multithreading freeze