2012-07-10 63 views
0

我有一個簡單的問題:線程中的自定義函數

我有一個名爲rlMF的線程。我這樣創建它:

public Thread rlMF = new Thread(new Runnable() { 
    public void run() { 
     reloadMissingFiles(); 
     stopTh(); 
    } 

    public void stopTh() { 
     activityStopped = true; 
    } 
}); 

現在我想從外線程調用stopTh函數。爲什麼我不能簡單地調用rlMF.stopTh();我能做些什麼呢?

例子:

protected void onPause() { 
    Log.d("Info", "destroying..."); 
    activityStopped = true; 
    rlMF.stopTh(); 
    super.onPause(); 
} 

是不工作...

+0

你想達到什麼目的?當onPause被調用時,你是否試圖中斷你的線程? – assylias 2012-07-10 09:59:28

+0

是的,這就是我想要做的... – Styler2go 2012-07-10 10:42:09

+0

看到我的答案關於各種方法來做到這一點。 – assylias 2012-07-10 11:02:14

回答

2

因爲訪問的接口是從Thread。爲了讓您可以從外部訪問方法,您需要指定一個公開此方法的類型。

如果你仔細看看該方法是在Runnable的情況下實施的。甚至沒有在Thread

你可能有這樣的事情,如果你真的需要訪問Runnable對象:

class MyRunnable implements Runnable { 
    public void run() { 
    ... 
    } 

    public void fooBar() { 
    ... 
    }  
} 

public void someMethod() { 
    MyRunnable myRunnable = new MyRunnable(); 
    Thread thread = new Thread(myRunnable); 
    ... 
    myRunnable.fooBar(); 
    ... 
} 
+0

你能告訴我一個例子嗎?已經插入 – Styler2go 2012-07-10 09:57:42

+1

;) – 2012-07-10 10:07:18

+0

是否需要成爲新班級? – Styler2go 2012-07-10 10:37:24

0

弗朗西斯科 - 方法的一個例子,除了你要實現的目標是什麼。也許這可以在正確的方向

public class CustomRun implements Runnable { 
    public void run() { 
     reloadMissingFiles(); 
     stopTh(); 
    } 

    public void stopTh() { 
     activityStopped = true; 
    } 
} 

點你在你的代碼

// start thread with custom runner 
CustomRun runner = new CustomRun(); 
new Thread(runner).start(); 

// call your stopTh method on CustomRun class 
protected void onPause() { 
    Log.d("Info", "destroying..."); 
    activityStopped = true; 
    runner.stopTh(); 
    super.onPause(); 
} 
0

你的目標是從中斷線程。有幾種方法可以實現,但基本上,您需要在reloadMissingFiles中包含一些可中斷性。

選項1

您可以使用布爾標誌像你這樣 - 你需要將其申報爲volatile,以確保修改跨線程可見:

private volatile boolean activityStopped = false; 

public void reloadMissingFiles() { 
    while (!activityStopped) { 
     //load small chunks so that the activityStopped flag is checked regularly 
    } 
} 

public Thread rlMF = new Thread(new Runnable() { 
    public void run() { 
     reloadMissingFiles(); //will exit soon after activityStopped has been set to false 
    } 
}); 

protected void onPause() { 
    //This will stop the thread fairly soon if the while loop in 
    //reloadMissingFiles is fast enough 
    activityStopped = true; 
    super.onPause(); 
} 

選項2(更好的辦法)

我不知道你在做什麼reloadMissingFiles,但我想這是某種I/O操作,它們通常是可中斷的。然後,您可以有一箇中斷的政策,你只要一個InterruptedException捕捉停止:

public void reloadMissingFiles() { 
    try { 
     //use I/O methods that can be interrupted 
    } catch (InterruptedException e) { 
     //cleanup specific stuff (for example undo the operation you started 
     //if you don't have time to complete it 
     //then let the finally block clean the mess 
    } finally { 
     //cleanup (close the files, database connection or whatever needs to be cleaned 
    } 
} 

public Thread rlMF = new Thread(new Runnable() { 
    public void run() { 
     reloadMissingFiles(); //will exit when interrupted 
    } 
}); 

protected void onPause() { 
    runner.interrupt(); //sends an interruption signal to the I/O operations 
    super.onPause(); 
} 

注意:您還可以閱讀this article進行更深入的版本的它。