如何在Java中使用线程?
我在Java中使用如何在Java中使用线程?,java,multithreading,Java,Multithreading,我在Java中使用Thread。我创建了一个扩展Thread的类,这很好,但问题是,当调用这个Thread时,我不知道这个类的实例数,因为用户必须输入这个数字。例如: multiThread multiThreadInstance = new multiThread(/* number entered from user */); multiThreadInstance.start(); 这将调用此线程一次,但如果我写入: multiThread multiThreadInstance1
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
运行。我需要让这些线程实例同时运行。如何执行此操作?我怀疑您意外地重写了线程中的start()
方法。确保要覆盖的是run()
方法
如果您用自己的实现重写start()
方法,您就可以“移除线程
类的魔力”。神奇之处在于start()。看看倒计时锁和执行器。下面是一段代码,可以实现您想要的功能。我建议您仔细阅读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.*;
公共类并发计时器{
私有ConcurrentTimer(){}//不可实例化
公共静态长时间(Executor Executor、int并发、,
最终可运行操作)抛出InterruptedException{
最终倒计时闩锁就绪=新倒计时闩锁(并发);
最终倒计时闩锁启动=新倒计时闩锁(1);
final CountDownLatch done=新的CountDownLatch(并发);
for(int i=0;i
上面提供的代码示例的可运行版本:(已编辑)
import java.util.concurrent.*;
公共类并发计时器{
公共静态void main(字符串[]args){
试一试{
Runnable action=new Runnable(){
公开募捐{
System.out.println(“线程运行”);
}
};
时间(3,行动);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
私有ConcurrentTimer(){}//不可实例化
公共静态长时间(int并发,
最终可运行操作)抛出InterruptedException{
Executor Executor=Executors.newFixedThreadPool(并发);
最终倒计时闩锁就绪=新倒计时闩锁(并发);
最终倒计时闩锁启动=新倒计时闩锁(1);
final CountDownLatch done=新的CountDownLatch(并发);
for(int i=0;i
当我说multi-threadInstance.start()run-method工作时,我把自己的代码放在了run-method中。您的run-method中有任何同步吗?例如wait
/notify
。不,我的run方法中没有同步,我只有sleep方法将您的整个代码发布到例如pastebin.com中,我会查看一下。当然,这是供您参考的。我已经添加了一个可运行的代码,包含main。很抱歉,如果我遗漏了一些内容,但我不太清楚您的问题是什么。首先,用户输入的数字在您的问题中是否重要?你问题中的所有例子似乎都有效。您能否发布循环实现的代码,并告诉我们什么不起作用,或者您不知道如何做。
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;
}
}