|
|
1、让一个线程sleep有两种方法,一个是直接调用Thread.sleep(),另一个是使用枚举类型 java.util.concurrent.TimeUnit的枚举常量。
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"> Clock.java
<!---->package sleep;
import static java.util.concurrent.TimeUnit.SECONDS; // utility class
public class Clock extends Thread
{
// This field is volatile because two different threads may access it
volatile boolean keepRunning = true;
public Clock()
{ // The constructor
setDaemon(true); // Daemon thread: interpreter can exit while it runs
}
public void run()
{ // The body of the thread
while (keepRunning)
{ // This thread runs until asked to stop
long now = System.currentTimeMillis(); // Get current time
System.out.printf("%tr%n", now); // Print it out
try
{
Thread.sleep(1000);
} // Wait 1000 milliseconds
catch (InterruptedException e)
{
return;
}// Quit on interrupt
}
}
// Ask the thread to stop running. An alternative to interrupt().
public void pleaseStop()
{
keepRunning = false;
}
// This method demonstrates how to use the Clock class
public static void main(String[] args)
{
Clock c = new Clock(); // Create a Clock thread
c.start(); // Start it
try
{
SECONDS.sleep(10);
} // Wait 10 seconds
catch (InterruptedException ignore)
{
} // Ignore interrupts
// Now stop the clock thread. We could also use c.interrupt()
c.pleaseStop();
}
}
|
|