⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timertask.java

📁 java多线程定时器
💻 JAVA
字号:
package practises.thread;

public abstract class timertask implements Runnable
{
    boolean cancelled = false;   //取消状态 
    long nextTime = -1;          //下一定时 
    long period;                 //执行间隔 
    boolean fixedRate;           //匀速执行标志
    
    protected timertask() {} 
    
    //取消任务执行,如果在运行,返回true,或者如果已被取消或从未定时,返回false
    public boolean cancel() 
    {
      if (cancelled) 
        return false;         
      cancelled = true;                    
      if (nextTime == -1) 
        return false;    
      return true;
    }  
    
    //定时器指定和时执行,run()方法用来确定是否在赢得时候被调用
    public long scheduledExecutionTime() 
    { 
      return nextTime; 
    } 
    
    //子类必须覆盖这一方法,提供要运行的代码
    //timer类从内部线程调用它
    public abstract void run();
    
    //由timer来告诉task的定时情况
    void schedule(long nextTime, long period, boolean fixedRate) 
    {
	this.nextTime = nextTime;
	this.period = period;
	this.fixedRate = fixedRate;
    } 
    
    //在timer调用后将timer取消
    boolean reschedule() 
    {
	if (period == 0 || cancelled) return false; 
	if (fixedRate) 
	  nextTime += period;
	else 
	  nextTime = System.currentTimeMillis() + period;
	return true;
    }       	
}

⌨️ 快捷键说明

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