我有一个程序,我已经创建了10个线程来运行。我希望主线程等到一切都完成后再完成。我正在尝试使用.join命令但它似乎没有工作。另外,我试图显示所有线程的运行时但它无法正常工作。以下是我的代码。
//File: CohanThread.java
//Author: Ryan A. Cohan
//Date: August 4, 2016
//Purpose: To create, run, and analyze a thread based program. IOBound runs IO intensive
//operations in the form of printing 1000 times to the console. CPUBound runs CPU
//intensive operations by computing an equation and printing 1000 times.
package cohanthread;
import java.text.*;
public class CohanThread{
public static void main(String args[]) throws InterruptedException{
NumberFormat formatter = new DecimalFormat("#0.00000");
IOBound io1 = new IOBound();
IOBound io2 = new IOBound();
IOBound io3 = new IOBound();
IOBound io4 = new IOBound();
IOBound io5 = new IOBound();
CPUBound cpu1 = new CPUBound();
CPUBound cpu2 = new CPUBound();
CPUBound cpu3 = new CPUBound();
CPUBound cpu4 = new CPUBound();
CPUBound cpu5 = new CPUBound();
long scheduleStart = System.currentTimeMillis();
io1.start();
io2.join();
io3.join();
io4.join();
io5.join();
cpu1.join();
cpu2.join();
cpu3.join();
cpu4.join();
cpu5.join();
long scheduleEnd = System.currentTimeMillis();
System.out.println("Runtime of all threads: " + formatter.format((scheduleEnd - scheduleStart) / 1000d));
System.out.println("Processes complete.");
}
}
class IOBound extends Thread{
NumberFormat formatter = new DecimalFormat("#0.00000");
@Override
public void run(){
long start = System.currentTimeMillis();
for(int i = 0; i < 1000; i++){
System.out.println("Thread number is: " + i);
}
long end = System.currentTimeMillis();
System.out.println("IO Thread runtime: " + formatter.format((end - start) / 1000d));
}
}
class CPUBound extends Thread{
NumberFormat formatter = new DecimalFormat("#0.00000");
@Override
public void run(){
String binary = "";
long start = System.currentTimeMillis();
for(int i = 0; i < 1000; i++){
while(i > 0){
int remainder = i % 2;
binary = remainder + binary;
i = i / 2;
}
System.out.println("Binary number: " + binary);
long end = System.currentTimeMillis();
System.out.println("CPU Thread runtime: " + formatter.format((end - start) / 1000d));
}
}
}
答案 0 :(得分:0)
Join不会启动计算,您必须启动所有线程并使用join等待完成...
https://docs.oracle.com/javase/tutorial/essential/concurrency/join.html
使用此主....
public static void main(String args[]) throws InterruptedException{
NumberFormat formatter = new DecimalFormat("#0.00000");
IOBound io1 = new IOBound();
IOBound io2 = new IOBound();
IOBound io3 = new IOBound();
IOBound io4 = new IOBound();
IOBound io5 = new IOBound();
CPUBound cpu1 = new CPUBound();
CPUBound cpu2 = new CPUBound();
CPUBound cpu3 = new CPUBound();
CPUBound cpu4 = new CPUBound();
CPUBound cpu5 = new CPUBound();
long scheduleStart = System.currentTimeMillis();
io1.start();
io2.start();
io3.start();
io4.start();
io5.start();
cpu1.start();
cpu2.start();
cpu3.start();
cpu4.start();
cpu5.start();
io1.join();
io2.join();
io3.join();
io4.join();
io5.join();
cpu1.join();
cpu2.join();
cpu3.join();
cpu4.join();
cpu5.join();
long scheduleEnd = System.currentTimeMillis();
System.out.println("Runtime of all threads: " + formatter.format((scheduleEnd - scheduleStart) / 1000d));
System.out.println("Processes complete.");
}
答案 1 :(得分:0)
你可以使用java java.util.concurrent包;
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(10);
List<Future<?>> futureList = new ArrayList<>();
futureList.add(executor.submit(new IOBound()));
futureList.add(executor.submit(new IOBound()));
futureList.add(executor.submit(new IOBound()));
futureList.add(executor.submit(new IOBound()));
futureList.add(executor.submit(new IOBound()));
futureList.add(executor.submit(new CPUBound()));
futureList.add(executor.submit(new CPUBound()));
futureList.add(executor.submit(new CPUBound()));
futureList.add(executor.submit(new CPUBound()));
futureList.add(executor.submit(new CPUBound()));
for (Future future:futureList) {
try {
//this method will blocking until task finished
future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
ExecutorService是要走的路!
您可以在此处使用执行程序服务以及要执行的线程列表。
我已将程序更新为使用Executor Service。
import java.text.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CohanThread{
public static void main(String args[]) throws InterruptedException{
NumberFormat formatter = new DecimalFormat("#0.00000");
ExecutorService es = Executors.newFixedThreadPool(5);
List<Callable<Object>> threadList = new ArrayList<Callable<Object>>();
IOBound io1 = new IOBound();
IOBound io2 = new IOBound();
IOBound io3 = new IOBound();
IOBound io4 = new IOBound();
IOBound io5 = new IOBound();
threadList.add(Executors.callable(io1));
threadList.add(Executors.callable(io2));
threadList.add(Executors.callable(io3));
threadList.add(Executors.callable(io4));
threadList.add(Executors.callable(io5));
CPUBound cpu1 = new CPUBound();
CPUBound cpu2 = new CPUBound();
CPUBound cpu3 = new CPUBound();
CPUBound cpu4 = new CPUBound();
CPUBound cpu5 = new CPUBound();
threadList.add(Executors.callable(cpu1));
threadList.add(Executors.callable(cpu2));
threadList.add(Executors.callable(cpu3));
threadList.add(Executors.callable(cpu4));
threadList.add(Executors.callable(cpu5));
long scheduleStart = System.currentTimeMillis();
List<Future<Object>> results = es.invokeAll(threadList);
long scheduleEnd = System.currentTimeMillis();
System.out.println("Runtime of all threads: " + formatter.format((scheduleEnd - scheduleStart) / 1000d));
System.out.println("Processes complete.");
}
}
class IOBound extends Thread{
NumberFormat formatter = new DecimalFormat("#0.00000");
@Override
public void run(){
long start = System.currentTimeMillis();
for(int i = 0; i < 1000; i++){
System.out.println("Thread number is: " + i);
}
long end = System.currentTimeMillis();
System.out.println("IO Thread runtime: " + formatter.format((end - start) / 1000d));
}
}
class CPUBound extends Thread{
NumberFormat formatter = new DecimalFormat("#0.00000");
@Override
public void run(){
String binary = "";
long start = System.currentTimeMillis();
for(int i = 0; i < 1000; i++){
while(i > 0){
int remainder = i % 2;
binary = remainder + binary;
i = i / 2;
}
System.out.println("Binary number: " + binary);
long end = System.currentTimeMillis();
System.out.println("CPU Thread runtime: " + formatter.format((end - start) / 1000d));
}
}
}
请注意,List<Future<Object>> answers = es.invokeAll(threadList);
将等待,直到所有线程都已完成执行。
希望它有所帮助。快乐的编码干杯!