2013-10-29 26 views
1

我只是無法弄清楚這個問題的原因。線程停止時無法結束工作流程

問題:

1)我使用了Thread.Sleep功能設置爲我的應用程序定時器。

2)現在,只要用戶進入計時器開始的網頁,如果用戶點擊停止任何鏈接定時器(線程),然後新的計時器開始計時。 3)如果他們在3秒內沒有活動,則計時器結束,並且與該網頁關聯的工作流程也結束。

代碼:

DownloadSummariesPage.java

public DownloadSummariesPage(){ 

abc = new SimpleThread(this); 

Link<Void> link = new Link<Void>("downloadSummaryLink") { 

public void onClick() { 

        boolean threadStatus = abc.checkStatus(); 

        if (threadStatus) { 
         abc.interrupt(); 
         abc.stop(); 
         abc = new SimpleThread(DownloadSummariesPage.this); 
         abc.start(); 
         } 
        else 
         { 
         LOG.debug("thread is dead now"); 
         endWorkflow(); 
         LOG.debug("ending the workflow"); 
         setResponsePage(MenuPage.class);       
         } 
        } 

}; 
abc.start(); 
} 


public void endWorkflow() { 
    abc.interrupt(); 
    abc.stop(); 
    boolean downloadReport = false; 
    LOG.debug("before action 201 inside endworkflow"); 
    Map<String, Serializable> inputs = new HashMap<String, Serializable>(); 
    inputs.put("downloadReport", downloadReport); 
    try { 
     wf.doAction(id, 201, inputs);//do not worry about this its for workflow 
     String empty = ""; 
     populateDownloadReportDatabase(empty); 

     setResponsePage(MenuPage.class); 
    } catch (Exception e) { 
     LOG.debug("Exception while performing 201 workflow, getControlCancel " 
       + e); 
    } 
} 
} 

我的下一個類,它創建線程 SimpleThread.java

class SimpleThread extends Thread { 
private static final Logger LOG = Logger.getLogger(SimpleThread.class); 

boolean stillActive = true; 
DownloadSummariesPage dsp; 

SimpleThread(DownloadSummariesPage dp) { 
    this.dsp = dp; 
} 

public void run() { 
    // 5sec timer 
    LOG.debug("inside run method"); 
    try { 
     Thread.sleep(3000); 
     stillActive = false; 
    } catch (InterruptedException e) { 
     LOG.debug("Exception in thread " + e); 
     e.printStackTrace(); 
    } 
    LOG.debug("before endworkflow"); 
    dsp.endWorkflow();//so this is what i actually want to do... 
    LOG.debug("at the end of simplethread"); 

} 

public boolean checkStatus() { 
    return stillActive; 
} 
} 

案例:

1)發生了什麼:在線程用戶登錄睡覺時,用戶點擊一個鏈接線程停止,並創建一個新的,如果用戶再次點擊它,如果用戶沒有3秒鐘做任何事情再次,現在情況發生,stillAlive變量SimpleThread類被設置爲false,當用戶現在點擊它結束工作流程完美...

2)我想什麼:如果線程啓動用戶登錄,如果他們是由用戶沒有活動stillAlive變量設置爲假現在聲明和dsp.endWorkflow();應該結束工作流程。對 ?但它在達到endWorkflow()功能之後停止,並且實際上並沒有結束工作流程...

希望你得到這個,我盡我所能讓它理解。感謝您的時間..

我感謝所有幫助..

回答

1

所以這裏有一些奇怪的事情。
首先假定線程在3秒內沒有任何中斷地休眠,因此它將調用dsp.endWorkflow(),您可以從onClick方法中再次調用該線程。

第二的stillAlive標誌應volatile

boolean volatile stillActive = true; 

可能的錯誤/錯誤將引起通過此部分。

if (threadStatus) { 
abc.interrupt(); 
abc.stop();// you cannot get to this like just after interrupt, maybe thread abc goes first 
abc = new SimpleThread(DownloadSummariesPage.this); 
abc.start(); 
} 

,因爲當線程處於休眠狀態,你打斷它假設,然後停下來,但是這是可能的,線程完成它的工作,你剛剛中斷後(你停止它之前)。所以這最好是阻止它,或者當線程到達捕獲時return

try { 
     Thread.sleep(3000); 
     stillActive = false; 
    } catch (InterruptedException e) { 
     LOG.debug("Exception in thread " + e); 
     e.printStackTrace(); 
     return;//////give up the rest work. so you don't need to call the stop too. 
    } 

,誤差在這裏 假設用戶不點擊取消,或者新的下載,和線程剛剛完成其睡眠和調用dsp.endWorkflow(),所以這是怎麼回事這個方法?

public void endWorkflow() { 
    abc.interrupt(); 
    abc.stop();//error here! 
    boolean downloadReport = false;//this is unreachable 
} 

你看,你是abc線程中調用這個方法,錯誤的是,你正在殺死線程只是方法裏面,你設置downloadReportfalse之前。所以它可能會是這樣。

public void endWorkflow() { 
    boolean downloadReport = false;//this is unreachable 
    abc.interrupt(); 
    abc.stop();//error here! 
} 

我希望我能幫上忙。

+0

嘿謝謝你的所有努力。我很感激。它們對於變量downloadReport(無法訪問)沒有問題,但問題出在了abc.stop()上,試圖找出爲什麼從SimpleThread類(dsp.endWorkflow)調用時不執行此語句。如果有任何共享關於這個的意見..?謝謝.. – Ashish

+0

正如我所說的夥計,當線程中有2秒提醒時,請考慮用戶點擊,因此在第一次調用「中斷」,然後「停止」它。所以這可能是因爲當你中斷它時,線程會從睡眠中醒來並執行其餘的代碼,然後停止它。你可以不打斷,而停止它。 – 2013-10-30 15:56:46