码迷,mamicode.com
首页 > 编程语言 > 详细

java TimeUnit synchronized demo

时间:2016-04-04 11:50:06      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

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();
  }
}

 

java TimeUnit synchronized demo

标签:

原文地址:http://www.cnblogs.com/rojas/p/5351523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!