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.