gpt4 book ai didi

java - 无法正确中断平均线程

转载 作者:行者123 更新时间:2023-12-03 13:09:13 26 4
gpt4 key购买 nike

因此,基本上,我的程序使用Avg线程从Generator获取数字,从中获取平均值并将其发送给Collector。我想在工作10秒钟后中断所有线程,但是即使生成器和 Controller 的行为正常,我似乎也无法以与前者相同的方式来中断Avg线程。您能给我一些为什么他们不会终止的见解吗?

import java.util.Scanner;

public class ElaboratoEsame {


public static void main(String[] args) {
int N ;
int M ;
Scanner scanner = new Scanner(System.in);
System.out.println("Inserire il numero dei Thread Avg");
N=scanner.nextInt();
System.out.println("Inserire M");
M=scanner.nextInt();
scanner.close();

Monitor1 m1 = new Monitor1(N);
Monitor2 m2 = new Monitor2(N);

Generator g = new Generator(m1);
g.start();

Collector c = new Collector(m2, N);
c.start();

Avg[] avg = new Avg[N];
for (int i = 0; i < N; i++) {
avg[i] = new Avg(i, m1, m2, M);
avg[i].start();
}

try{
Thread.sleep(2*1000);
}catch(InterruptedException ie) {}

g.interrupt();
for (int i=0; i > N; i++){
avg[i].interrupt();
}
c.interrupt();
try{Thread.sleep(500);

}catch(InterruptedException ie){}
System.out.println(g.isAlive());
}
}

public class Generator extends Thread {

private final Monitor1 m1;

public Generator(Monitor1 m1) {
this.m1 = m1;
}

@Override
public void run() {
int i = 0;
try {
do {
m1.doWrite(i);
++i;
Thread.sleep(100);
} while (true);
} catch (InterruptedException ie) {
System.out.println("Generator is interrupted");
}
}
}

public class Monitor1 {
private final Integer[] numbers;
private int index = 0;

public Monitor1(int N){
numbers = new Integer[N];
}

public synchronized int doRead(int id) throws InterruptedException{
while(numbers[id]==null){
wait();
}
int result=numbers[id];
numbers[id]=null;
notifyAll();
return result;
}

public synchronized void doWrite(int i) throws InterruptedException{
while(numbers[index]!=null){
wait();
}
numbers[index]=i;
index=(index+1)%(numbers.length);
notifyAll();
}



}

public class Avg extends Thread {

private final int id;
private final Monitor1 m1;
private final Monitor2 m2;
private final int M;

public Avg(int id, Monitor1 m1, Monitor2 m2, int M) {
this.id = id;
this.m1 = m1;
this.m2 = m2;
this.M = M;
}

@Override
public void run() {
try {
do {
double sum = 0;
for (int i = 0; i < M; ++i) {
sum += m1.doRead(id);
}
double avg = sum / M;
System.out.println("Avg " + id + " : " + avg);
m2.doWrite(id, avg);
} while (true);
} catch (InterruptedException ie) {
System.out.println("Avg " + id + " is interrupted");
}
}


}

public class Monitor2 {
private final Double[] averages;


public Monitor2(int N){
averages = new Double[N];
}

public synchronized double doRead(int i) throws InterruptedException{
while(averages[i]==null){
wait();
}
double result=averages[i];
averages[i]=null;
notifyAll();
return result;
}

public synchronized void doWrite(int id, double avg) throws InterruptedException{
while(averages[id]!=null){
wait();
}
averages[id]=avg;
notifyAll();

}
}
public class Collector extends Thread{
private final Monitor2 m2;
private final int N;

public Collector (Monitor2 m2, int N){
this.m2=m2;
this.N=N;
}

@Override
public void run(){
try{
do{
double sum=0;
for (int i = 0; i < N; ++i) {
sum += m2.doRead(i);
}
System.out.println("La somma è " + sum);
}while(true);
}catch(InterruptedException ie){
System.out.println("Collector is interrupted");
}

}
}

最佳答案

您在调用avg中断的循环中有一个错误。尝试更改此-

for (int i=0; i > N; i++){
avg[i].interrupt();
}

为此-
for (int i=0; i < N; i++){
avg[i].interrupt();
}

关于java - 无法正确中断平均线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42434752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com