stopme.java

来自「Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用」· Java 代码 · 共 36 行

JAVA
36
字号
// Chapter 7, Listing 4
public class StopMe extends Thread
{
	// Run method is executed when thread first started
	public void run()
	{
		int count = 1;

		System.out.println ("I can count. Watch me go!");

		for (;;)		
		{
			// Print count and increment it
			System.out.print (count++ + " ");

			// Sleep for half a second
			try { Thread.sleep(500); } catch (InterruptedException ie) {}
		}
	}


	// Main method to create and start threads
	public static void main(String args[]) throws java.io.IOException
	{
		// Create and start counting thread
		Thread counter = new StopMe();
		counter.start();
		
		// Prompt user and wait for input
		System.out.println ("Press any enter to stop the thread counting");
		System.in.read();

		// Interrupt the thread
		counter.stop();
	}
}

⌨️ 快捷键说明

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