所以我创建了5个线程,在完成工作后我想做另一个工作.那么如何找出执行者的线程何时完成他们的工作,然后才开始超级工作方法呢?
主要:
ExecutorService executor = Executors.newFixedThreadPool(5);
CountDownLatch doneSignal = new CountDownLatch(5);//thread number
for(int i=0;i<5;i++){
getLogFile n = new getLogFile(doneSignal, i);// work method
executor.execute(n);
doneSignal.await();
}
Run Code Online (Sandbox Code Playgroud)
//可能这里有类似executor.awaitTermination(60,TimeUnit.SECONDS); {不起作用}或者有效的东西
Superworkmethod(uses thread created files);//main thread probably starts thi
Run Code Online (Sandbox Code Playgroud)
类:
public static class getLogFile implements Runnable {
private final CountDownLatch doneSignal;
private final int i;
getLogFile(CountDownLatch doneSignal, int i) {
this.doneSignal = doneSignal;
this.i = i;
}
public int run1(String Filenamet) {
//do work
}
public void run() {
run1(file);
doneSignal.countDown();
}
Run Code Online (Sandbox Code Playgroud)
}
我有这个绝妙的主意,可以加快生成36个文件所需的时间:使用36个线程!不幸的是,如果我j2ssh用36个线程/会话启动一个连接(一个连接对象),那么一切都比我一次执行每个线程要多得多.
现在,如果我尝试创建36个新连接(36个j2ssh连接对象),那么每个线程都有一个与服务器的单独连接,要么我的内存异常(不知何故程序仍然运行,并成功结束其工作,比我的时间慢执行一个接一个的线程).
那么该怎么办?如何找到我应该使用的最佳线程数?因为Thread.activeCount()在开始我的36个线程之前是3?我正在使用联想笔记本电脑英特尔酷睿i5.