I have two different subprocess running two different commands in two different threads.the second thread updates a value in a static variable and the first thread takes the value and uses it.
The flow should be like this : Thread2 updates static variable and thread1 takes up the value from the static variable and then prints it.. but the flow that's happening is thread1 picks up the value from the static variable first. In this case it has a null value and then thread2 updates the value.
Both the threads are running in parallel, i am using ExecutorService class to do this. I'm using Runtime class to run the command and a while loop to read the outputs from the Stream continiously on both threads.
Thread1 keeps on giving (X,Y) values , Thread two gives out the value only when it gets a text.
Output I'm getting :
(12, 123) null --> thread2 didn't get any value so it doesnt update, thread1 would get null from the static variable
(123,334) null --> thread1 picks up the value from the static variable and uses it, thread2 then updates value "Hello" to static variable
(134,654) "Hello" --> thread1 picks up "Hello" and uses it, thread2 then updates value "World" to static variable
Expected output:
(12, 123) null --> thread2 didn't get any value so it doesnt update, thread1 would get null from the static variable
(123,334) "Hello" --> thread2 updates value "Hello" to static variable, thread1 picks it up and uses it
(134,654) "World" --> thread2 updates value "World" to static variable, thread1 picks it up and uses it
i've used a volatile variable too but the output didn'y change. I'm i missing something here? Please help...