controlledthread2.java

来自「java的线程类演示」· Java 代码 · 共 57 行

JAVA
57
字号
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;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?