我在Java中使用Thread
。我创建了一个扩展Thread
的类,它很好,但问题是,当调用此Thread
时,我不知道此类的实例数,因为用户必须输入此数字。例如:
multiThread multiThreadInstance = new multiThread(/* number entered from user */);
multiThreadInstance.start();
这将调用此线程一次,但如果我写:
multiThread multiThreadInstance1 = new multiThread(/* number entered from user */)
multiThreadInstance1.start()
multiThread multiThreadInstance2 = new multiThread(/* number entered from user */)
multiThreadInstance2.start()
这将同时调用两次,依此类推。
如果我将其放在for
循环中,那么如果用户输入3,那么start1
会运行,start1
完成,start2
运行,start2
运行时start3
1}}完成,{{1}}运行。我需要保持这些线程实例同时运行。我怎么能这样做?
答案 0 :(得分:2)
我怀疑您意外地覆盖了start()
中的Thread
方法。确保它是您要覆盖的run()
方法。
如果您使用自己的实现覆盖start()
方法,则可以“移除Thread
类的魔力”。神奇之处在于start()
,它在一个新线程中开始执行run()
方法,因此请将您自己的代码保存在run()
中。
答案 1 :(得分:1)
您需要使用java高级并发实用程序来执行此操作。看看倒计时和执行者。以下是可以执行您想要的代码。我建议你阅读java并发实用程序。
import java.util.concurrent.*;
public class ConcurrentTimer {
private ConcurrentTimer() { } // Noninstantiable
public static long time(Executor executor, int concurrency,
final Runnable action) throws InterruptedException {
final CountDownLatch ready = new CountDownLatch(concurrency);
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; i++) {
executor.execute(new Runnable() {
public void run() {
ready.countDown(); // Tell timer we're ready
try {
start.await(); // Wait till peers are ready
action.run();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
done.countDown(); // Tell timer we're done
}
}
});
}
ready.await(); // Wait for all workers to be ready
long startNanos = System.nanoTime();
start.countDown(); // And they're off!
done.await(); // Wait for all workers to finish
return System.nanoTime() - startNanos;
}
}
上面提供的代码示例的可运行版本:(已编辑)
import java.util.concurrent.*;
public class ConcurrentTimer {
public static void main(String[] args) {
try {
Runnable action = new Runnable() {
public void run() {
System.out.println("Thread Running");
}
};
time (3, action);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private ConcurrentTimer() { } // Noninstantiable
public static long time(int concurrency,
final Runnable action) throws InterruptedException {
Executor executor = Executors.newFixedThreadPool(concurrency);
final CountDownLatch ready = new CountDownLatch(concurrency);
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; i++) {
executor.execute(new Runnable() {
public void run() {
ready.countDown(); // Tell timer we're ready
try {
start.await(); // Wait till peers are ready
action.run();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
done.countDown(); // Tell timer we're done
}
}
});
}
ready.await(); // Wait for all workers to be ready
long startNanos = System.nanoTime();
start.countDown(); // And they're off!
done.await(); // Wait for all workers to finish
return System.nanoTime() - startNanos;
}
}