threadctrl.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 46 行

JAVA
46
字号
import java.util.Date;


//public class ClassName extends SuperClass implements Runnable
public class ThreadCtrl implements Runnable
{
	private Thread threadName = null;
	private Date now = new Date();
	public void start()
	{
		threadName = new Thread(this);
		threadName.start();
		System.out.println("thread start . . .");
	}
	public void stop()
	{
		threadName = null;
	}
	public void run()
	{
		Thread currentThread = Thread.currentThread();
		int i = 0;
		while (threadName == currentThread)
		{
			now = new Date();
			System.out.println(now);
			try
			{
			threadName.sleep(1000);
			}
			catch(InterruptedException ie)
			{
				System.err.println("Thread error: " + ie);
			}
			i ++;
			if (i > 6)
			{
				stop();
				System.out.println("thread terminated!");
			}
			//or
			//threadName.yield();
		}
	}
}

⌨️ 快捷键说明

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