java - 在主功能中..不显示平均值。线程不活动但未终止。为什么?

标签 java

我想创建一个程序来使用java中的线程计算通过控制台输入的数字的平均值。在主函数中,我从未从函数 getAverage() 中获得平均值的输出。出了什么问题..当我调试时..程序终止但在正常运行中..当我输入除 double 值以外的任何内容时它应该终止,但它不会发生。

import java.util.*;
public class P1
{
    private AverageCalculator ac; 
    private boolean stop;

    public Thread inputThread,averageThread;

    public P1()
    {
        ac = new AverageCalculator();
        new UserInteraction(ac);
        new ToAverage(ac);
    }

    public void printAverage()
    {
        System.out.println("Average is " + ac.getAverage());
    }

    private class AverageCalculator{
        private double average=0,sum=0;
        private int i=0;
        private boolean flag=false;

        private double getAverage()
        {
            return average;
        }

        private synchronized void sum(double val) {
            while(flag) {
                try {
                    wait();
                } catch(InterruptedException e) { System.out.println("Thread Interrputed"); }
            }
            sum += val;
            i++;
            flag = true;
            notify();
        }

        private synchronized void calculateAverage()
        {
            while(!flag) {
                try {
                    wait();
                } catch(InterruptedException e) { System.out.println("thread interrupted"); }
            }
            average = (sum / i);
            flag = false;
            notify();
        }
    }

    private class UserInteraction implements Runnable {
        private AverageCalculator ac;
        //private boolean take=true;
        private double input=0;

        Scanner s = new Scanner(System.in);

        private UserInteraction(AverageCalculator ac) {
            this.ac = ac;
            inputThread = new Thread(this,"Input thread");
            inputThread.start();
            stop=false;
        }

        public void run()
        {
            System.out.println("Enter number: ");

            while(!stop) {
                if(s.hasNextDouble() == false) {
                    stop = true;
                    s.close();
                }
                else
                {

                    input = s.nextDouble();
                    s.nextLine();

                    ac.sum(input);

                }
            }
        }
    }

    private class ToAverage implements Runnable {
        private AverageCalculator ac;

        private ToAverage(AverageCalculator ac)
        {
            this.ac = ac;
            averageThread = new Thread(this,"Average doer Thread");
            averageThread.start();
        }

        public void run()
        {
            while(!stop) {

                ac.calculateAverage();
            }
        }
    }


public static void main(String[] args) {
        P1 p = new P1();
        try
        {
            p.inputThread.join();
            p.averageThread.join();
        } catch(InterruptedException e) { System.out.println("Interrupted in Main"); }
        System.out.println("Thread input alive check: " + p.inputThread.isAlive());
        System.out.println("Thread average alive check: " + p.averageThread.isAlive());
        p.printAverage();
    }
}

最佳答案

我没有完全调试您的代码,但问题是您正在调用 wait()。此方法将您的线程置于等待状态,并且必须调用 notify()notifyAll() 来唤醒您的线程。

下面是对调用 wait() 方法时发生的情况的合理解释: Difference Between Wait and Sleep in Java

关于java - 在主功能中..不显示平均值。线程不活动但未终止。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978867/

相关文章:

java - libGDX wav 听起来像锯子

javascript - 在我的网页上尝试编辑器

Java库解析文本并计算唯一单词的数量?

java - JSF2 和 RichFaces 的分层数据表

java - 使用 self 作为参数创建实例

java - Android 如何显示已下载文件的列表?

java - 如何在计时器中通过 java 旋转 android 中布局背景的图片?

java - 有没有办法在 delphi(Pascal 对象)中像在 java 中那样为每个对象创建一个?

java - Eclipse 链接/JPA : Multiple @DiscriminatorColumn annotations for Entity

java - 尝试将 JLabel 图标设置为图像目录,出现空指针异常