import java.util.concurrent.TimeUnit;
public class TestCalc
{
private static boolean stopRequested=false;
private static synchronized void requestStop()
{
stopRequested=true;
}
private static synchronized boolean stopRequested()
{
return stopRequested;
}
public static void main(String[] args)
throws InterruptedException
{
Thread backgroundThread=new Thread
(
new Runnable(){
public void run()
{
int i=0;
while(!stopRequested()){
i++;
System.out.println("------------> "+ i );
}
}
}
);
backgroundThread.start();
//TimeUnit.SECONDS.sleep(1); //建议用TimeUnit
backgroundThread.sleep(1000);
requestStop();
}
}