I was going through one of the tutorials on memory model of Java and came across this concept of field visibility which happens in multi-threaded programming. I tried to simulate the same using the below code, however , I see in each thread, the latest value is being reflected (in ReaderThread).
The below is the complete program.
Edit
After some suggestion to use while(somevariable), I incorporated, but still getting the same behaviour. I removed sysout on reading the x
FieldVisibility.java
package com.example.threads.fieldvisibility;
public class FieldVisibility {
private int x;
private boolean condition;
public FieldVisibility() {
condition = true;
}
public void reader() {
System.out.println("x in reader() is " + x);
}
public void writer() {
x++;
}
public boolean getCondition() {
return condition;
}
public void setCondition(boolean condition) {
this.condition = condition;
}
}
ReaderThread.java
package com.example.threads.fieldvisibility;
public class ReaderThread extends Thread {
private FieldVisibility fv;
public ReaderThread(FieldVisibility fv) {
this.fv = fv;
}
#Override
public void run() {
while (fv.getCondition()) {
System.out.println("It mean condition is true, which was set initially");
}
for (;;) {
}
}
}
WriterThread.java
package com.example.threads.fieldvisibility;
public class WriterThread extends Thread {
private FieldVisibility fv;
public WriterThread(FieldVisibility fv) {
this.fv = fv;
}
#Override
public void run() {
fv.setCondition(false);
for (;;) {
fv.writer();
}
}
}
MainApp.java
package com.example.threads.fieldvisibility.main;
import com.example.threads.fieldvisibility.FieldVisibility;
import com.example.threads.fieldvisibility.ReaderThread;
import com.example.threads.fieldvisibility.WriterThread;
public class MainApp {
public static void main(String[] args) throws InterruptedException {
FieldVisibility fv = new FieldVisibility();
ReaderThread rt = new ReaderThread(fv);
WriterThread wt = new WriterThread(fv);
wt.start();
rt.start();
Thread.sleep(999999999L);
}
}
Edit
I added a new variable condition in FieldVisibility, whose default values is true. Next, I set its value to false in WriterThread, however, the same value (false) is still propagated to ReaderThread, so I still am not able to simulate it.
Original
I expected that at some time ReaderThread won't be able to "see" the latest value of variable x, but I saw every time I run it, it gave same results. I even run in debug mode, suspended ReaderThread while running WriterThread continuously. But that also didn't prevent ReaderThread to have latest values. I expected that I need to declare variable x as volatile in order for ReaderThread to read latest values of x.
How can I simulate the field visibility concept, or what changes I need to do for this?
Your example doesn't work because System.out.println() uses a shared resource (System.out), so it will synchronize with other uses of the same resource.
Therefore you will never* see a result where one thread uses the old value of the other. (*in theory it is possible for the reader to read x between x++ and the corresponding System.out.println()
Here is an example where a old value is used:
public class ThreadVisibility implements Runnable {
private boolean stop = false;
#Override
public void run() {
while (!stop);
}
public static void main(String[] args) throws InterruptedException {
ThreadVisibility test = new ThreadVisibility();
Thread t = new Thread(test);
t.setDaemon(true);
System.out.println("Starting Thread");
t.start();
Thread.sleep(1000);
System.out.println("Stopping Thread");
test.stop = true;
t.join(1000);
System.out.println("Thread State: " + t.getState());
}
}
If you run this code, it will display that the thread is still running at the end. Without the t.setDaemon(true), the VM would wait for the Thread to finish, which would never happen.
If you comment out the Thread.sleep, then the new Thread may terminate (at least it did in my tests), but it is not guaranteed to.
The right fix for this problem is to declare stop volatile.
Or add a memory barrier.
Related
I tried using a volatile boolean to act as a flag to stop/start/restart the action in the thread, however it does not work. It just keeps on going forever and never terminates. Any help on how to properly do this or why my code does not work will be greatly appreciated. Thanks in advance.
public class thread {
public static int i = 0;
private static Thread print = null;
private static printThread runnable = null;
public static void main(String[] args) {
runnable = new printThread();
print = new Thread (runnable);
print.start();
System.out.println("Starting");
runnable.begin();
if(i > 5)
{
runnable.terminate();
}
i = 10;
runnable.begin();
if(i > 15)
{
runnable.terminate();
}
}
public static final void print()
{
System.out.println(i);
i++;
}
public static final class printThread implements Runnable {
private volatile boolean running = false;
public void terminate() {
running = false;
}
public void begin() {
running = true;
}
public boolean isRunning() {
return running;
}
public void run() {
while(true)
{
if(running)
{
print();
}
else
{
}
}
}
}
}
In your code while loop execution never ends. You could introduce 2 states: terminated and waiting to simulate threads start/pause/restart/stop. However, even if you pause the Thread it will be running, just different branch of code will be executed inside while loop.
Please, see the code snippet below
public static final class printThread implements Runnable {
private volatile boolean waiting = false;
private volatile boolean terminated = false;
public void terminate() {
terminated = true;
}
public void pause() {
waiting = true;
}
public void restart() {
waiting = false;
}
public void run() {
while(!terminated) {
if(waiting) {
//the thread is paused
} else {
//the thread is running
}
}
}
}
however it does not work. It just keeps on going forever and never terminates.
In your run() method of your thread, you aren't watching for the value of your volatile boolean running field. It probably should be something like:
public void run() {
while(!running) {
print();
// you might want a short Thread.sleep(10); here to stop the spinning
}
}
However, as #Anton points out, once your thread terminates, it can't be restarted without some other flag. See his answer.
Also, you are sharing i between the main thread and the your printing thread. That also needs to be volatile so it can be properly shared. Since you are incrementing it in multiple threads, you should use an AtomicInteger for that.
public static AtomicInteger i = new AtomicInteger();
...
if (i.get() > 5) ...
...
i.set(10);
...
i.incrementAndGet();
Couple of other comments:
Be careful of static fields. print and runnable should be defined only inside of the main(...) method to restrict access.
Classes should begin with an uppercase letter so it should be PrintThread.
Actually, because PrintThread isn't a thread, it should be PrintRunnable or maybe even better, Printer.
This question already has answers here:
How interrupt/stop a thread in Java?
(8 answers)
Closed 8 years ago.
public class Threadsample implements ActionListener {
HelloRunner hr = new HelloRunner();
Thread tr1 = new Thread(hr, "ThreadOne");
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
if (source == b2){
hr.stopRunning();
}
if (source== b1){
tr1.start();
}
}
public class HelloRunner implements Runnable
{
private volatile boolean timeToQuit=false;
int i = 0;
public void run(){
while ( ! timeToQuit ){
System.Out.Println(i);
i++
}
}
public void stopRunning() {
timeToQuit=true;
}
}
}
How do I stop the running thread?
Thread interruption is the way to go:
// computingThread computing here
while (!Thread.currentThread.isInterrupted()){
System.Out.Println(i);
i++;
}
//.... in other part of your program, your main thread for instance:
public void stopComptuterThread() {
computingThread.interrupt(); // assuming computingThread reference reachable
}
Indeed, some people would use Thread.stop() method.. => here's why it would be very bad: https://www.securecoding.cert.org/confluence/display/java/THI05-J.+Do+not+use+Thread.stop()+to+terminate+threads
Thread.stop is deprecated and should not be used.
Sample code is here:
pause-and-resume-thread
Your code will do. You can use build-in interrupt method, which does mostly the same, but also awakes thread with InterruptedException, if it sleeps/waits. It's good to know, that Java doesn't allow to stop threads "the hard way" (except for using .stop() method on thread, which is deprecated).
So process, in general, looks as following:
user requests thread to stop, either by setting a flag (your case) or by invoking .interrupt() method, which sets the flag .interrupted() and "shakes" the thread so it awakes if was sleeping/waiting.
it's thread resonsibility to stop it's execution. If you don't implement some logic handling interruption flag, thread could not react to external thread trying to interrupt it and will die after JVM ends it's execution.
Are you sure, that it's thread issue? Have you checked, if .actionPerformed actually calls .stopRunning method?
Anyway, try following code sample. It works for 100%.
class HelloRunner implements Runnable {
private volatile boolean timeToQuit = false;
int i = 0;
public void run() {
while (!timeToQuit) {
System.out.println(i);
i++;
}
}
public void stopRunning() {
timeToQuit = true;
}
}
public class MainRunner {
public static void main(String[] args) throws InterruptedException {
HelloRunner hr = new HelloRunner();
Thread tr1 = new Thread(hr, "ThreadOne");
tr1.start();
Thread.sleep(100);
hr.stopRunning();
}
}
I want to demonstrate to my self the visibility thread-safety problem when accessing to a variable from more than one thread without using any kind of synchronisation.
I'm running this example from Java Concurrency in Practice:
public class NoVisibility {
private static boolean ready;
private static int number;
private static class ReaderThread extends Thread {
#Override
public void run() {
while (!ready) {
Thread.yield();
}
System.out.println(number);
}
}
public static void main(String[] args) throws InterruptedException {
new ReaderThread().start();
number = 42;
ready = true;
}
}
How to make it loop forever instead of printing 42 every time I run (looping for ever means that the modification of the variable ready = true; in the ReaderThread thread is not visisble to the main thread).
public static void main(String[] args) throws InterruptedException {
new ReaderThread().start();
number = 42;
//put this over here and program will exit
Thread.sleep(20000);
ready = true;
}
Place the Thread.sleep() call for 20 secs what will happen is JIT will kick in during those 20 secs and it will optimize the check and cache the value or remove the condition altogether. And so the code will fail on visibility.
To stop that from happening you MUST use volatile.
I can't figure out what is the problem in the following code:
I have a thread that can be suspended and resumed
Code bellow:
public class CustomThread implements Runnable {
private volatile boolean stop;
private volatile boolean suspend;
String[] names = new String[]{
"A", "B","C","D","E", "F", "G","H","I","J","K", "L"
};
public CustomThread(){
Collections.shuffle(Arrays.asList(names));
System.out.println("Available names:");
System.out.println(Arrays.asList(names));
}
#Override
public void run() {
while(!stop){
synchronized (this) {
if(suspend){
try {
System.out.println("Got suspended");
wait();
System.out.println("Resumed");
} catch (InterruptedException e) {
System.out.println("Got interupted");
}
}
else System.out.println("Suspend false");
}
int randomIdx = new Random().nextInt(names.length);
System.out.println(names[randomIdx]);
}
}
public synchronized void suspend(){
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>Suspend true");
suspend = true;
}
public synchronized void resume(){
suspend = false;
notify();
}
}
I run the following simple code:
public class CustomTest {
/**
* #param args
* #throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
CustomThread c = new CustomThread();
Thread t = new Thread(c);
t.start();
Thread.sleep(5000);
System.out.println("++++++++++++++++++++++++++++++++");
c.suspend();
}
}
What I am expecting to see is:
Thread custom runs, main sleeps, main suspends the custom thread by c.suspend() and since main terminates and noone resumes the thread, the thread remains in wait state.
But what I see instead is that the CustomThread prints continually Suspend false and an element from names.
What is the problem here? It is like the Thread.sleep(5000) and c.suspend() in main don't do anything.
The code is fine as written, but your problem is probably that you are running this through Eclipse and you are overwhelming the console. Put a shorter delay in main and you'll see good results.
Note: your suspend method doesn't need to be synchronized as it only writes to a volatile variable.
Instead of if(suspend) you should have while(suspend), see the explanation in javadoc here: http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#wait%28%29
From the javadoc of Object.wait():
...interrupts and spurious wakeups are
possible, and this method should always be used in a loop
In the below code, when I execute the producercon class, sometimes the execution stucks, looks like a deadlock. But if i make the get_flag () synchronized then there are no such problems.
I cannot figure out how there can be a problem. the flag can either true or false so only one of the producer or consumer will get into the if statement. After one of them enters the if it will enter the monitor with object r (both are initialized with the same object reference). The only problem which can happen that the r object being modified by the increment_decrement () function call, and the get_flag () reading the flag at the same time, but even then it will not enter the if in that iteration, but it will enter the if block on the next iteration, and even if the first thread did not leave the monitor, it will wait there for it (before the synchronized block).
How, and why is the program halting/hanging if get_flag () is not made synchronized ?
import java.io.*;
class resource
{
private boolean res, flag;
resource ()
{
flag=false;
}
boolean get_flag ()
{
return flag;
}
void increment_decrement (String s,boolean t)
{
res=t;
flag=t;
try
{
System.out.print("\n"+s+":"+res);
Thread.sleep(200);
}
catch(InterruptedException e)
{
}
}
}
class producer implements Runnable
{
resource r1;
Thread t1;
producer(resource r)
{
r1 = r;
t1 = new Thread(this);
t1.start();
}
public void run ()
{
while (true)
{
if(r1.get_flag () == false)
{
synchronized(r1)
{
r1.increment_decrement("Producer",true);
}
}
}
}
public void waitForThread () throws InterruptedException
{
t1.join ();
}
}
class consumer implements Runnable
{
resource r2;
Thread t2;
consumer(resource r)
{
r2 = r;
t2 = new Thread (this);
t2.start();
}
public void run()
{
while (true)
{
if(r2.get_flag () == true)
{
synchronized(r2)
{
r2.increment_decrement("Consumer",false);
}
}
}
}
public void waitForThread () throws InterruptedException
{
t2.join ();
}
}
public class producercon
{
public static void main(String args[])
{
try
{
System.out.print("PRESS CTRL+C TO TERMINATE\n");
resource r = new resource();
consumer c = new consumer(r);
producer p = new producer(r);
c.waitForThread ();
p.waitForThread ();
}
catch(InterruptedException e)
{
}
}
}
Your call to get_flag() is not thread safe nor volatile. This means in the cache of thread 1 it can be true while in the cache of thread 2 it can be false.
You need to make your boolean either volatile or an AtomicBoolean. Right now multiple threads are trying to access the boolean that is in no way synchronized.
This producer/consumer implementation is quite weird.
Neither the producer not the consumer wait for the resource to be in the adequate state, and the resource access is not well protected (the flag should be guarded by some lock to ensure its visibility between threads).
One way to improve on this design would be to use the standart wait/notify system. Another way would be to use a Semaphore in the Resource to ensure only one thread can access the resource at one given time. Finally, you could use a higher-level construct such an java.util.concurrent.SynchronousQueue to pass some data directly from the producer to the consumer.