zoukankan      html  css  js  c++  java
  • 并行流水线--求 (B+C)*B/2

    public class Msg {
        public double i;
        public double j;
        public String orgStr = null;
    }
    
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    
    /**
     * A = B + C
     * j = i + j
     */
    public class Plus implements Runnable {
        public static BlockingQueue<Msg> queue = new LinkedBlockingQueue<>();
    
        @Override
        public void run() {
            while (true){
                try {
                    Msg msg = queue.take();
                    msg.j=msg.i+msg.j;
                    Multiply.queue.add(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    
    /**
     * D = A * B
     * i = i * (i+j)
     */
    public class Multiply implements Runnable{
        public static BlockingQueue<Msg> queue = new LinkedBlockingQueue<>();
    
        @Override
        public void run() {
            while (true){
                try {
                    Msg msg = queue.take();
                    msg.i=msg.i*msg.j;
                    Div.queue.add(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    
    /**
     * D = D / 2
     * i =  i * (i+j)/2
     */
    public class Div implements Runnable {
        public static BlockingQueue<Msg> queue = new LinkedBlockingQueue<>();
        @Override
        public void run() {
            while (true){
                try {
                    Msg msg = queue.take();
                    msg.i=msg.i/2;
                    System.out.println(msg.orgStr+"="+msg.i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    /**
     * 并行流水线
     * 求 (B+C)*B/2
     *      Plus:A = B + C
     *  Multiply:D = A * B
     *       Div:D = D / 2
     *
     *  i =  i * (i+j)/2
     */
    public class PStreamMain {
        public static void main(String[] args){
            new Thread(new Plus()).start();
            new Thread(new Multiply()).start();
            new Thread(new Div()).start();
            for (int B = 1; B < 1000; B++) {
                for (int C = 1; C < 1000; C++) {
                    Msg msg = new Msg();
                    msg.i = B;
                    msg.j = C;
                    msg.orgStr = "(("+B+"+"+C+")*"+B+")/2";
                    Plus.queue.add(msg);
                }
            }
        }
        //((1+1)*1)/2=1.0
        //((1+2)*1)/2=1.5
        //((1+3)*1)/2=2.0
        //((1+4)*1)/2=2.5
        //((1+5)*1)/2=3.0
        //((1+6)*1)/2=3.5
        //((1+7)*1)/2=4.0
        //((1+8)*1)/2=4.5
        //((1+9)*1)/2=5.0
        //((1+10)*1)/2=5.5
        //((1+11)*1)/2=6.0
        //((1+12)*1)/2=6.5
        //...
    }
    
  • 相关阅读:
    清明节实现所有网页变灰色
    点击按钮,复制文本
    Matlab笔记
    spring框架中配置mysql8.0需要注意的地方(转载)
    移动吉比特H2-2光猫超级用户与密码
    JS关闭chorme页面
    MATLAB利用solve函数解多元一次方程组
    微信聊天记录导出为csv,并生成词云图
    fmt.Sprintf(格式化输出)
    iris,context源码分析
  • 原文地址:https://www.cnblogs.com/fly-book/p/11475843.html
Copyright © 2011-2022 走看看