ScheduledExecutorService 定時任務,線程


java5 之后,並發線程部分增加了許多新的東西,新的啟動、調度、管理線程的一大堆API,這時通過Executor來啟動線程比Thread.start()更好,更容易控制線程的啟動,銷毀等,還可以使用線程池的功能。

一.創建任務

實際上就是實現Runnable接口,實現run方法。

二.執行任務

通過java.util.concurrent.ExecutorService接口對象來執行任務,該接口對象通過工具類java.util.concurrent.Executors的靜態方法來創建。

Executors此包中所定義的 Executor、ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 類的工廠和實用方法。

1、創建線程池(針對ScheduledExecutorService)

private static ScheduledExecutorService singleScheduler = Executors.newScheduledThreadPool(4);

2、添加任務到線程池中

當將一個任務添加到線程池中,線程池會為每個任務分配一個線程去跑任務。執行任務時間可以定時,可以臨時。

(1)執行臨時任務

ScheduledExecutorService 有兩個比較好的方法執行臨時任務 execute(),submit()

三個區別:

   a.參數不同

    execute() 只有一個方法,接收Runnable類,

    submit() 有三個重載方法,分別接收Runnable類,Callable類,和Runnable ,T(執行成功返回的值)

   b.返回值

    execute() 沒有返回值,submit()有返回一個Future<> 執行后返回是否成功的標識。假如一個任務執行后,需要知道是否執行成功,如果失敗,原因是什么。

   c.submit 方便異常處理

(2)執行定時任務 scheduleAtFixedRate,scheduleWithFixedDelay 

a.scheduleAtFixedRate

具體參數說明

  public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay,long period,  TimeUnit unit);

  command 執行的線程;initialDelay 初始化延遲;period;兩次開始執行最小時間間隔;unit 計時單位

b.scheduleWithFixedDelay 

具體參數說明

  public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay,long delay,  TimeUnit unit);

  command 執行的線程;initialDelay 初始化延遲;delay;前一次執行結束到下一次執行開始的間隔時間(間隔執行延遲時間);unit 計時單位

三、關閉對象

  shutdown();

四、示例

 1.簡單的Runnable類

  excute()和submit()單個參數 的執行比較簡單,不列。

  

package test;

public class ThreadDemo implements Runnable{

    public ThreadDemo(){
        
    }
    @Override
    public void run() {
        System.out.println("簡單執行一下!");
    }
}

  2.臨時任務的執行

  

package test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

public class TestThread {

    private static ScheduledExecutorService singleScheduler =  Executors.newScheduledThreadPool(4);
    
    public static void main(String[] args) {
        Future<String> f =singleScheduler.submit(new ThreadDemo(),"成功!");
        try {
            System.out.println(f.get());    
        } catch (InterruptedException  e) {
            e.printStackTrace();
        }  catch (ExecutionException  e) {
            e.printStackTrace();
        }
        
    }
} 

返回結果

簡單執行一下!
成功!

   3.定時任務的執行

scheduleWithFixedDelay與此方法區別只是參數有些差異。

package test;

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

public class TestThread {

    private static ScheduledExecutorService singleScheduler =  Executors.newScheduledThreadPool(4);
    
    public static void main(String[] args) {
        singleScheduler.scheduleAtFixedRate(new ThreadDemo(), 1, 1, TimeUnit.SECONDS);
    }
} 

  返回結果

  

簡單執行一下!
簡單執行一下!
簡單執行一下!
簡單執行一下!
簡單執行一下!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM