java定时任务嵌套

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

 

你可能感兴趣的:(Java)