只是动态地减少这个sleep时间,怎么弄呀

解决方案 »

  1.   

    Thread1: sleep(2hours);
    Thread2: sleep(1hour) then interrupt Thread1;
      

  2.   

    谢谢楼上回答,但我这个线程的引用是在servlet中声明的,难道要把它存在session里?(可不可能在同一线程里完成呀?)
      

  3.   

    把间隔时间设短啊,比如每次sleep5分钟,控制好循环次数不就OK了
      

  4.   

    我也想过楼上的,但请问这样做如果时间长了,会不会计时不准确呀。(人家要时间精确呢)您的意思是这样吧:?
    for(int i=0;i<24;i++)  //24个5分钟是二小时
    {
       Thread.Sleep(5min);
       if(someFlag==true)break;
    }
      

  5.   

    我习惯 用 WaitForSingleObject
      

  6.   

    DWORD WaitForSingleObject(    HANDLE hHandle, // handle of object to wait for 
        DWORD dwMilliseconds  // time-out interval in milliseconds  
       );
     ParametershHandleIdentifies the object. For a list of the object types whose handles can be specified, see the following Res section. 
    Windows NT: The handle must have SYNCHRONIZE access. For more information, see Access Masks and Access Rights. dwMillisecondsSpecifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses. 
      

  7.   

    嗯,按照你所设计的最小时间间隔来sleep,然后每次醒来后判断一下,如果不到条件就继续睡,我的一个程序就是这么跑的,时间的间距是很严格的,如果你的要求不是到达毫秒级没什么问题的。