【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了ControlledThread2.java提供大家参考,希望对大家有所帮助!
import java.awt.*;
public class ControlledThread2 extends Thread
{
static final int RUN = 0;
static final int SUSP = 1;
static final int STOP = 2;
private int state = RUN;
private int i;
private Label lb;
public ControlledThread2(Label l)
{
lb = l;
}
public synchronized void setState(int s)
{
state = s;
if (state == RUN)
notify();
}
public synchronized boolean checkState()
{
while (state == SUSP)
{
try {
wait();
}
catch (InterruptedException e) { }
}
if (state == STOP)
return false;
return true;
}
public void run()
{
while (true)
{
i++;
lb.setText(String.valueOf(i));
try {
Thread.sleep(500);
}
catch (InterruptedException e) { }
if ( !checkState() )
break;
}
}
}
上一篇:
ControlledThread.java
下一篇:
多年来只想说一句,我不怪你