抱歉,如果问题不清楚
我正在制作一个简单的多线程程序,它有一个链表来存储除主线程之外创建的所有线程。然后我想发送一些信号来终止主线程,但只有在所有其他线程都关闭时,我打算这样做,当线程关闭时,它将自己从链表中删除,然后主线程将检查该列表是否size == null 与否
这是我的代码
public class MainProgram {
//some global static variable
static List<Thread> threadList = new LinkedList<Thread>();
public void main() throws IOException {
ServerSocket serverSocket;
serverSocket = new ServerSocket(1234);
while(true){
if(Shutdown_Handler.shutdown==true){
//wait for all other thread close/terminate
return
}
Socket s = serverSocket.accept();
ClientThread x = new ClientThread(s);
Thread y = new Thread(x);
threadList.add(y);
y.start();
}
}
}
当 Shutdown_Handler.shutdown==true 时,main 将检查 threadList 是否为null
. 问题是我不知道如何使线程从列表中删除。正如我所搜索的,对于普通对象,我可以创建这样的方法
public class someObject {
public static void delete(){
if(list.size = null) return;
list.remove(this);
}
}
但是,在线程的情况下,类实现Runnable
所以this
引用是对象而不是存储在列表中的线程