需要恢復中斷狀態的一個場景
沒有恢復中斷狀態時,在Step1執行期間發生中斷,Step2操作還會繼續,這就存在讓資料出現不一致的風險:
import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /*2015-4-9*/ public class InterruptedDemo { private static final Logger LOGGER=LoggerFactory.getLogger(InterruptedDemo.class); public static void main(String[] args) throws InterruptedException { Thread playgame=new Thread(new TaskRunner(), "do a work"); playgame.start(); int waitTime=5; LOGGER.info("tips: task will be cannel ,after {}s",waitTime); TimeUnit.SECONDS.sleep(waitTime); playgame.interrupt(); } } class TaskRunner implements Runnable{ private static final Logger LOGGER=LoggerFactory.getLogger(TaskRunner.class); @Override public void run() { Step step1=new Step("step1",20); try { step1.process(); } catch (InterruptedException e) { LOGGER.info("clean 、 backup or other business ............."); LOGGER.error("step1 Fail",e); // Thread.currentThread().interrupt(); } Step step2=new Step("step2",10); try { step2.process(); } catch (InterruptedException e) { LOGGER.info("clean 、backup or other business ............. "); LOGGER.error("step2 Fail",e); // Thread.currentThread().interrupt(); } } } class Step{ private static final Logger LOGGER=LoggerFactory.getLogger(Step.class); private String step; private int costTime; public Step(String step,int costTime) { this.step=step; this.costTime=costTime; } void process() throws InterruptedException{ LOGGER.info("Step:{}",this.step); LOGGER.info("Need {}s",this.costTime); TimeUnit.SECONDS.sleep(this.costTime); LOGGER.info("end "); } }
Output:
[2015-04-10 01:37:57,634] [main] INFO - tips: task will be cannel ,after 5s [2015-04-10 01:37:57,634] [do a work] INFO - Step:step1 [2015-04-10 01:37:57,635] [do a work] INFO - Need 20s [2015-04-10 01:38:02,637] [do a work] INFO - clean 、 backup or other business ............. [2015-04-10 01:38:02,638] [do a work] ERROR - step1 Fail java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at thread.Step.process(InterruptedDemo.java:63) at thread.TaskRunner.run(InterruptedDemo.java:30) at java.lang.Thread.run(Thread.java:745) [2015-04-10 01:38:02,641] [do a work] INFO - Step:step2 [2015-04-10 01:38:02,641] [do a work] INFO - Need 10s [2015-04-10 01:38:12,641] [do a work] INFO - end
傳遞中斷狀態的場景:
去掉上面程式碼中Thread.currentThread().interrupt();這句的註釋
Output:
[2015-04-10 01:41:32,349] [do a work] INFO - Step:step1 [2015-04-10 01:41:32,350] [do a work] INFO - Need 20s [2015-04-10 01:41:32,349] [main] INFO - tips: task will be cannel ,after 5s [2015-04-10 01:41:37,351] [do a work] INFO - clean 、 backup or other business ............. [2015-04-10 01:41:37,352] [do a work] ERROR - step1 Fail java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at thread.Step.process(InterruptedDemo.java:63) at thread.TaskRunner.run(InterruptedDemo.java:30) at java.lang.Thread.run(Thread.java:745) [2015-04-10 01:41:37,355] [do a work] INFO - Step:step2 [2015-04-10 01:41:37,355] [do a work] INFO - Need 10s [2015-04-10 01:41:37,355] [do a work] INFO - clean 、backup or other business ............. [2015-04-10 01:41:37,355] [do a work] ERROR - step2 Fail java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at thread.Step.process(InterruptedDemo.java:63) at thread.TaskRunner.run(InterruptedDemo.java:39) at java.lang.Thread.run(Thread.java:745)
相關文章
- Activity狀態恢復
- Oracle 11g 資料庫恢復-場景5:部分檔案損壞恢復,開庫狀態,低可用恢復方式Oracle資料庫
- Oracle 11g 資料庫恢復-場景4:部分檔案損壞恢復,開庫狀態, 高可用恢復方式Oracle資料庫
- Oralce 11g資料庫恢復-場景3:部分檔案損壞恢復,關庫狀態,高可用恢復方式資料庫
- rman中三個不完全恢復場景
- Oracle 11g RMAN恢復-場景2:所有的資料檔案損壞,OPEN狀態Oracle
- 【備份恢復】恢復inactive狀態的日誌檔案
- 幾種場景的資料庫恢復資料庫
- [譯] 狀態恢復入門教程
- Oracle 11g 資料庫恢復-場景2:所有據檔案損壞,關庫狀態Oracle資料庫
- Oracle 11g 資料庫恢復-場景1:所有的資料檔案損壞,OPEN狀態Oracle資料庫
- PRemiere介面怎麼恢復?PR介面功能恢復預設狀態的技巧REM
- Java異常的中斷和恢復Java
- 判斷資料庫是否需要例項恢復資料庫
- 歸檔模式下四種完全恢復的場景模式
- 無備份恢復各種場景的處理
- 32. http狀態碼 應用場景HTTP
- 如何判斷一個場景是否是物聯網?
- iOS UI狀態儲存和恢復(三)iOSUI
- 前端 | Vue 路由返回恢復頁面狀態前端Vue路由
- 恢復oracle的scott使用者初始狀態操作Oracle
- MySQL是如何做到可以恢復到任意一秒狀態的?MySql
- 測試恢復3==當資料庫處於開啟狀態時的恢復資料庫
- 閃回恢復一個表中的資料
- QTP中為什麼恢復場景(Recovery Scenario)沒有被觸發?QT
- 結合具體場景,聊聊 React 的狀態管理方案React
- oracle關閉狀態刪除活動日誌報錯恢復(一)Oracle
- win10登錄檔恢復初始狀態的方法Win10
- Oracle 如何判斷一個死事務transaction的恢復進度Oracle
- 12C PDB使用RMAN的4種完全恢復場景
- 電商交易場景狀態機方案探索及應用
- go需要泛型的場景Go泛型
- Go 需要泛型的場景Go泛型
- 記前端狀態管理庫Akita中的一個坑前端
- MySQL斷電恢復的一點簡單分析MySql
- inactive狀態日誌組檔案損壞的恢復
- 在open狀態下恢復丟失的資料檔案
- 【DATAGUARD】手工恢復備庫日誌中斷