java - 程序未在多线程环境中终止-Java
问题描述
尝试制作一个简单的多线程程序,在该程序中打印阶乘系列,其中每个数字由不同的线程打印,最后我报告哪个线程打印了哪个数字。我得到了所需的输出,但不知何故我的程序是不终止。
约束:我不允许使用并发包
import java.util.ArrayList;
import java.util.Scanner;
class Report {
private long factorial;
private String threadName;
private int activeThreads;
public Report(long factorial, String threadName, int activeThreads) {
this.factorial = factorial;
this.threadName = threadName;
this.activeThreads = activeThreads;
}
public long getFactorial() {
return factorial;
}
public String getThreadName() {
return threadName;
}
public int getActiveThreads() {
return activeThreads;
}
public void setActiveThreads(int activeThreads) {
this.activeThreads = activeThreads;
}
}
public class Factorial implements Runnable {
public static ArrayList<Report> report = new ArrayList<Report>();
private static int count;
public static void main(String[] args) throws InterruptedException {
Scanner in = new Scanner(System.in);
System.out.print("N: ");
int n = in.nextInt();
count = n;
Factorial f = new Factorial();
f.series(n);
Thread.sleep(1000);
// Series
for(Report r : report) {
if(r.getFactorial() == 1) {
System.out.print(r.getFactorial());
}
else {
System.out.print(r.getFactorial() + "*");
}
}
System.out.println();
// Report
for(Report r : report) {
System.out.println(r.getFactorial() + " printed by " + r.getThreadName() + " " + r.getActiveThreads());
}
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
System.out.println("In Main");
in.close();
}
public void series(int n) throws InterruptedException {
for(int i=0;i<n;i++) {
Thread t = new Thread(new Factorial());
t.start();
}
}
public synchronized void generate() {
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
report.add(new Report(count--, Thread.currentThread().getName(), threadGroup.activeCount()));
notifyAll();
System.out.println("In generate" + threadGroup.activeCount());
}
@Override
public void run() {
generate();
synchronized (this) {
try {
wait();
}
catch(Exception e) {
e.printStackTrace();
}
}
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
System.out.println("In Run" + threadGroup.activeCount());
}
public static int getCount() {
return count;
}
public static void setCount(int count) {
Factorial.count = count;
}
}
虽然我知道我们可以使用.stop()杀死线程,但我认为不推荐这样做。
解决方案
要使同步有效(synchronized
, wait
, notify
),您必须使用相同的实例。
在series
中,您在每个循环上创建一个新Factorial
实例,使每个线程无限期地等待。
public void series(int n) throws InterruptedException {
for(int i=0;i<n;i++) {
// Thread t = new Thread(new Factorial()); // creates an new instance
Thread t = new Thread(this);
t.start();
}
}
在该run
方法中,您首先调用notifyAll()
(通过generate
),然后调用wait
.
最后创建的线程将wait
在所有其他线程完成之后。
无论如何,必须通知最后一个线程。它可能是在sleep
通话之后,与:
synchronized(f) {
f.notify();
}
或者可能使用专用的同步方法。
推荐阅读
- c++ - 由于 Miracast,Windows 10 上出现 CWiFiDisplayException
- javascript - 无法在“文档”上执行“退出全屏”:文档未处于活动状态
- visual-studio - Visual Studio 嵌入大型资源文件(将近 4gb)
- node.js - 在 amqp 中列出有界队列
- javascript - 在 amp-truncate-text 标记内呈现图像时出现问题
- netsuite - 当我们在旧发票上运行预定脚本时,重新打开全额支付的发票
- r - R:日期运算结果为空日期
- swift - 等待谷歌反向地理编码
- lightningchart - 区域范围系列的 setDataCleaningThreshold
- symfony - 你能定义一个只消费多队列传输的特定队列的消费者命令吗?