zoukankan      html  css  js  c++  java
  • 【Java多线程】使用多线程计算阶乘累加 1!+2!+3!+...+19!+20!。其中一个线程计算阶乘,另一线程实现累加并输出结果

    (如发现问题,请帮忙指出,谢谢)使用多线程计算阶乘累加 1!+2!+3!+…+19!+20!。其中一个线程计算阶乘,另一线程实现累加并输出结果。

    class Info{
        double sum=1;
        double count =0;
        private boolean flag=false;
        double count1;
        public synchronized void set(double sum){
            if(!flag){
                try{
                    super.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.setSum(sum);
            try{
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.setCount(count);
            flag=false;
            super.notify();
        }
        public synchronized void get(double count){
            if(flag){
                try{
                    super.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try{
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag=true;
            super.notify();
        }
    
        public double getCount() {
            return count;
        }
    
        public void setCount(double count) {
            this.count = count;
        }
    
        public void setSum(double sum) {
            this.sum = sum;
        }
    
        public double getSum() {
            return sum;
        }
    }
    
    class Producer implements Runnable {
        private Info info = null;
        public Producer(Info info) {
            this.info = info;
        }
    
        @Override
        public void run() {
            int i;
            boolean flag = true;
            if (flag) {
                for (i = 1; i <= 20; i++) {
                    try {
                        this.info.sum=1;
                        for (int j = 1; j <= i; j++) {
                            this.info.sum *= j;
                            if(i==20){
                                this.info.count1=this.info.sum;
                            }
                        }
                        Thread.sleep(90);
                        System.out.println(i + "!为" + this.info.sum);
                        this.info.set(this.info.sum);
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                flag=true;
            }else {
                try{
                    Thread.sleep(90);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                flag=false;
            }
        }
    }
    
    class Consumer implements Runnable{
        private Info info=null;
        private double count1=0;
        public Consumer(Info info){
            this.info=info;
        }
        @Override
        public void run() {
            for(int i=1;i<=20;i++){      
                try{
                    Thread.sleep(100);
                    this.info.count+=this.info.sum;
                    this.info.get(this.info.count);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("计算结果为:"+(this.info.count+this.info.count1));
        }
    }
    
    public class TestX {
        public static void main(String args[]){
            Info i=new Info();
            Producer pro=new Producer(i);
            Consumer con=new Consumer(i);
            new Thread(pro).start();
            new Thread(con).start();
        }
    }
    
    
  • 相关阅读:
    北京西格玛大厦微软社区精英 Visual Studio 2010 技术交流会记录
    2010522 Windows Phone 开发者日
    SharePoint Server 2010 RTM 安装过程(图)
    Windows HPC Server 2008 R2 简体中文版 下载
    转:Community Clips 使用指南
    关于CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files… ‘Access is denied.’ 的解决办法
    岗位职责
    PowerPoint 2010 的广播幻灯片功能尝试了一下,可以用。
    Silverlight 4 五
    Windows Server AppFabric 简体中文 下载地址
  • 原文地址:https://www.cnblogs.com/phyger/p/14029527.html
Copyright © 2011-2022 走看看