亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java Executor類的注釋部分什么意思?

java Executor類的注釋部分什么意思?

FFIVE 2019-03-13 18:19:29
許多 Executor 實現都對調度任務的方式和時間強加了某種限制。以下執行程序使任務提交與第二個執行程序保持連續,這說明了一個復合執行程序。 class SerialExecutor implements Executor {     final Queue<Runnable> tasks = new LinkedBlockingQueue<Runnable>();     final Executor executor;     Runnable active;     SerialExecutor(Executor executor) {         this.executor = executor;     }     public synchronized void execute(final Runnable r) {         tasks.offer(new Runnable() {             public void run() {                 try {                     r.run();                 } finally {                     scheduleNext();                 }             }         });         if (active == null) {             scheduleNext();         }     }     protected synchronized void scheduleNext() {         if ((active = tasks.poll()) != null) {             executor.execute(active);         }     } }沒看明白, 這個在外面該如何使用呢? 構造參數Executor是干什么的, 怎么連續的提交任務?
查看完整描述

2 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

關于 Executor 可以參考 java并發編程-Executor框架。SerialExecutor 的作用是使用指定的(通過構建函數傳入)Executor 對象,順序的執行任務(通過 execute() 方法實現)

SerialExectuor.execute() 會把傳入的 Runnable 放在隊列里,然后按順序執行,如果隊列為空(初始或已經執行完),會立即啟動傳入的 Runnable 對象,用法大概就像這樣

SerialExecutor sExecutor = new SerialExecutor(sourceExecutor);

sExecutor.execute(task1);

sExecutor.execute(task2);

sExecutor.execute(task3);

// ...


查看完整回答
反對 回復 2019-04-16
?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

SerialExecutor與其他Executor的區別在于,使用execute(final Runnable r)提交任務時,如果當前任務執行完成之后,會繼續執行下一個任務,也就是代碼種finally里面的scheduleNext();這句起的作用,所以就是連續執行了。

普通的Executor框架是通過調度器自身的線程的run方法中無線循環讀取隊列中的任務去觸發任務的執行,而SerialExecutor在每個任務中顯示的調用了scheduleNext來觸發下一個任務的執行。


查看完整回答
反對 回復 2019-04-16
  • 2 回答
  • 0 關注
  • 407 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號