sleepyhead.java

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

JAVA
39
字号
// Chapter 7, Listing 3
public class SleepyHead extends Thread
{
	// Run method is executed when thread first started
	public void run()
	{
		System.out.println ("I feel sleepy. Wake me in eight hours");
		
		try
		{
			// Sleep for eight hours
			Thread.sleep( 1000 * 60 * 60 * 8 );

			System.out.println ("That was a nice nap");
		}
		catch (InterruptedException ie)
		{
			System.err.println ("Just five more minutes....");
		}
	}


	// Main method to create and start threads
	public static void main(String args[]) throws java.io.IOException
	{
		// Create a 'sleepy' thread
		Thread sleepy = new SleepyHead();
		
		// Start thread sleeping
		sleepy.start();
		
		// Prompt user and wait for input
		System.out.println ("Press enter to interrupt the thread");
		System.in.read();

		// Interrupt the thread
		sleepy.interrupt();
	}
}

⌨️ 快捷键说明

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