我有一个程序,其中创建了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));
}
}
}
联接不会启动计算,您必须启动所有线程并使用联接来等待完成...
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.");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句