首页 文章

如何在arraylist中存储线程

提问于
浏览 43 次
-4

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

ArrayList<Client> clients;
Thread client = new Thread(new ClientThread(socket));

想要在客户的arraylist中添加每个客户端 . 我已经成为客户的对象arraylist .

1 回答

  • 3

    假设你已经将 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);
    

相关问题