我試圖創建我的第一個服務器,一個簡單的聊天服務器。試圖弄清楚如何使用Runnable線程類將參數傳遞給線程
我有一個Runnable
類叫做cCleanThread
創建使用休眠調用 çGlobals.mUserList.Clean();
每10秒一個線程。
我想擁有它,因此cCleanThread
的每個實例都會有自己的mUserList
對象。我找不出一個簡單的方法來做到這一點。
我想我會somw當我創建我的cCleanThread
對象時,如何通過mUserList
的參數?
代碼
public class cCleanThread implements Runnable {
Thread runner;
public cCleanThread() {
}
public cCleanThread(String threadName) {
runner = new Thread(this, threadName); // (1) Create a new thread.
System.out.println(runner.getName());
runner.start(); // (2) Start the thread.
}
public void run() {
//Display info about this particular thread
System.out.println(Thread.currentThread());
while(true)
{
try {
Thread.sleep(20*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cGlobals.mUserList.Clean();
}
}
}
可能重複[我如何將一個變量傳遞給一個新的Runnable聲明?](http://stackoverflow.com/questions/4297261/how-can-i-pass-a-variable-into-a-new-runnable-declaration) – Ralph