how to set boolean flag of thread-1 from thread-2 in Java multithreading

前端 未结 4 884
故里飘歌
故里飘歌 2020-12-21 22:32

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

相关标签:
4条回答
  • 2020-12-21 23:08

    I would create a method called setIsDone() in NumGen.

    public void setIsDone(boolean isDone) {
        this.isDone = isDone;
    }
    

    Your other thread can call this when it is time to for NumGen to finish.

    Also, the general recommendation is that in most cases you should implement the Runnable interface instead of subclassing Thread directly, but that is just a rule of thumb not a hard and fast rule.

    0 讨论(0)
  • 2020-12-21 23:09

    I would have the main() routine create a new AtomicBoolean object, and I would pass references to the object to the constructors of both Thread classes. The RunningAvg.run() method can set the AtomicBoolean, and the NumGen.run() method can examine it.

    class NumGen extends Thread {
        ...
        AtomicBoolean isDone;
    
        public NumGen(PipedOutputStream pos, AtomicBoolean isDone){
            ...
            this.isDone = isDone;
        }
    
        public void run(){
            while (!isDone.get()){
                ...
            }
        }
    }
    
    class RunningAvg extends Thread {
        ...
        AtomicBoolean isDone;
    
        public RunningAvg(PipedInputStream pis, AtomicBoolean isDone){
            ...
            this.isDone = isDone;
        }
    
        public void run(){
            try {
            while (dis.available()>0){
                ...
                if (avg > 1E5) {
                    isDone.set(true);
                    ...
                }
            }
            ...
        }
    
    
    public class InterThreadComm {
    
        public static void main(String[] args){
    
        try {
            ...
            AtomicBoolean isDone = new AtomicBoolean(false);
            NumGen ng = new NumGen(pos, isDone);
            RunningAvg ra = new RunningAvg(pis, isDone);
            ...
        }
    
    0 讨论(0)
  • 2020-12-21 23:11

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

    first declare you flag isDone as AtomicBoolean

    private AtomicBoolean isDone;
    

    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();
    

    finally once you want to stop generting numbers ask Thread-2 to set the isDone false.

    0 讨论(0)
  • 2020-12-21 23:32

    You need to create a method that is accessible in both, using sychronized.

    for ex:

       public synchronized boolean getDone()
       {
                return isDone;
       }
    

    Otherwise you will have trouble keeping track of the variable between threads.

    Here is a link that should help :http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

    0 讨论(0)
提交回复
热议问题