如何在arraylist中存储线程

Sam*_*Sam -2 java sockets multithreading arraylist

我有一台服务器和几个客户端。我为每个客户端创建了一个线程。我想将每个客户端的线程或对象存储在arraylist中。并对其进行操作。

ArrayList<Client> clients;
Thread client = new Thread(new ClientThread(socket));
Run Code Online (Sandbox Code Playgroud)

想要在客户端的数组列表中添加每个客户端。我已经建立了一个客户端对象arraylist。

Moi*_*ker 5

假设您已经ClientThread定义了可以实现的地方Runnable

List<Thread> clients = new ArrayList<Thread>();
Thread client = new Thread(new ClientThread(socket));
client.start(); //assuming you want the thread to start
//running before you put into the arrayList
clients.add(client);
Run Code Online (Sandbox Code Playgroud)