1

我想创建线程来操作它们(运行、等待、停止)。我知道怎么做一个一个实例

实现可运行的类

public class ThreadRunner implements Runnable{ .....

我创建对象的类

ThreadRunner thread1 = new ThreadRunner ("thread1");
ThreadRunner thread2 = new ThreadRunner ("thread2"); 
.
.
.
thread1.start()
.
.

我想要一个只有在用户想要时才结束的循环(可能带有一个布尔标志)。如何创建每 10 秒动态实例化 Thread 的循环?

4

1 回答 1

0
boolean stop = false;
List<Thread> threads = new ArrayList<Thread>();
while (!stop) {
    Thread t = new ThreadRunner("Thread " + threads.size());
    t.start();
    threads.add(t);
    // Do something here to check whether 'stop' should be updated.
    // And wait for 10 seconds here
} 
于 2015-08-21T03:29:13.250 回答