本文介绍了如何从线程2用Java多线程设置线程1的布尔标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个涉及三个线程一个简单的多线程应用程序:
发-1 线程2

I am writing a simple multithreaded application that involves three threads:Thread-1, Thread-2 and main.

发-1 随机数字发生器类,产生 随机双打和饲料到发-2

Thread-1 is a random number generator class that produces random doubles and feeds to Thread-2.

发-2 消耗以计算平均。我已经使用了数的PipedOutputStream 发-1 与随机数字。 线程2 使用的PipedInputStream 吃起来随机数字

Thread-2 consumes the numbers to calculate the average .I have used PipedOutputStream that Thread-1 feeds with random numbers. Thread-2 uses PipedInputStream to eat up the random numbers.

现在的问题是:

主题-2 ,我想信号发-1 停止生产数字。我在 A 布尔标志需要线程1 来开启。我怎样才能做到这一点?

if the average exceeds 1E5 in Thread-2, I want to signal Thread-1 to stop producing numbers. I have a boolean flag in Thread-1 that needs to be turned on. How can I achieve this?

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Random;

//

class NumGen extends Thread {

    PipedOutputStream pos;
    DataOutputStream dos;
    boolean isDone=false;

    public NumGen(PipedOutputStream pos){
        this.pos=pos;
        dos=new DataOutputStream(pos);
    }

    public void run(){
        while (!isDone){
            Random rand = new Random();
            try {
                dos.writeDouble(rand.nextDouble()+100.0);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

class RunningAvg extends Thread {

    PipedInputStream pis;
    DataInputStream dis;
    Double avg;
    int count;
    Double runningTotal;

    public RunningAvg(PipedInputStream pis){
        this.pis=pis;
        dis=new DataInputStream(pis);
            runningTotal=0.0;
    avg=0.0;
    }

    public void run(){
        try {
        while (dis.available()>0){
            count+=1;
            runningTotal+=dis.readDouble();
                avg=runningTotal/count;
                System.out.printf("The average in count no : %s is %s%n",count,avg);
                //if avg>1E5
                 //set NumGen.isDone=true
        }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


public class InterThreadComm {

    public static void main(String[] args){


    try {
        PipedOutputStream pos= new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        NumGen ng = new NumGen(pos);
        RunningAvg ra = new RunningAvg(pis);
        ng.start();
        ra.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }


}

PS:在code,因为它是永远运行下去而不打印在控制台上对此我没有什么弄清楚为什么!!

PS: The code as it is runs forever without printing anything on console which I am yet to figure out why!!

推荐答案

您可以使用的AtomicBoolean ,并把它传递给两个线程,原子类型是accessable扔多线程也是线程安全的。

You could use AtomicBoolean and pass it to the both threads, Atomic types is accessable throw multithreading also thread safe.

首先声明你的标志 isDone 的AtomicBoolean

private AtomicBoolean isDone;

再定义一个的AtomicBoolean 对象,并把它传递给两个线程

then declare one AtomicBoolean object and pass it to the both threads

PipedOutputStream pos= new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
AtomicBoolean isDone = new AtomicBoolean();
NumGen ng = new NumGen(pos, isDone);
RunningAvg ra = new RunningAvg(pis, isDone);
ng.start();
ra.start();

最后一次要停止generting号问发-2 设置 isDone 假的。

这篇关于如何从线程2用Java多线程设置线程1的布尔标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 09:10