我正在尝试创建我的第一个服务器,一个简单的聊天服务器。
我有一个名为 cCleanThread
的 Runnable
类,它创建一个使用 sleep 来调用的线程
cGlobals.mUserList.Clean();
每 10 秒一次。
我希望拥有它,以便 cCleanThread
的每个实例都有自己的 mUserList
对象。我想不出一个简单的方法来做到这一点。
我想当我创建 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();
}
}
}
最佳答案
关于java - 尝试弄清楚如何使用 Runnable 线程类将参数传递给线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8299814/