java定時任務巢狀

cube_chen發表於2020-10-09
package com.huatek.d20150803;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ScheduledTest {
	public static void main(String args[]) throws Exception
    {
        Temp command = new Temp();
        
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        //延遲固定時間執行某個任務
        ScheduledFuture<?> scheduleTaskOut = scheduler.scheduleWithFixedDelay(command, 0, 10, TimeUnit.HOURS);
    }
}
/**
 * 外部定時執行的任務
 */
class Temp extends Thread
{
    public void run()
    {
        System.out.println("Hello,out");
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        TempTwo commandTwo = new TempTwo();
        //以固定頻率執行某個任務
        ScheduledFuture<?> scheduleTaskIn = scheduler.scheduleAtFixedRate(commandTwo, 0, 5, TimeUnit.SECONDS);
    }
}
/**
 * 內部定時執行的任務
 */
class TempTwo extends Thread
{	
    public void run()
    {
        System.out.println("Hello,in");
    }
}

 

相關文章