0

I need clarification related to basic thread functionality and volatile method.In the given example:

public class ThreadDemo {
     public static void main(String args[]){
         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
         Priority_test hi=new Priority_test(Thread.NORM_PRIORITY+2);

         hi.start();

         try{
             Thread.sleep(1000);
         }catch(InterruptedException e){
             System.out.println("Main thread interrupted");
         }

         hi.stop();

         try{
             hi.t.join();
         }catch(InterruptedException e){
             System.out.println("Interrupted");
         }
         System.out.println("High priority:"+hi.click);
      } 
}

    public class Priority_test implements Runnable{
       Thread t; 
       long click=0;
       private volatile boolean running=true;
       Priority_test(int p){
           t=new Thread(this);
           t.setPriority(p);
       }

       public void run(){
           while(running)
               click++;
       }

       public void start(){
           t.start();
       }

       public void stop(){
         running=false;
       }
    }

So,here 'hi' object is common to currentThread and child thread created inside 'hi'.That means both refer to same memory location....in that case if currentThread modifies value of variable 'running'(without using volatile) then it means value gets updated to the memory location from where child thread is reading its value.....but i guess something else is happening and my concept is not clear as without using volatile its going into infinite loop.Please explain how its happening internally.....i mean both threads are referring to same object and what difference does volatile make.I am really getting confused....:(

Bhavya Sharma
  • 309
  • 3
  • 15
  • possible duplicate of [Why does Java not see the updated value from another thread?](http://stackoverflow.com/questions/21135870/why-does-java-not-see-the-updated-value-from-another-thread) – the8472 May 24 '15 at 11:04
  • [This answer](http://stackoverflow.com/a/4905123/4125191) to a related question might help you as well. – RealSkeptic May 24 '15 at 11:15

1 Answers1

1

Basically what is happening is that the JVM normally allows threads to cache copies of instance or class variables i.e. each thread might have their own copy. If one copy is updated by one thread, the other thread might not see the new value. By making a variable volatile, the JVM will not allow threads to cache copies of the variable and the variable is kept in a single location (main memory), so if multiple threads are reading a value they will get the same result.

Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • Thanks for d answer....it helped a lot....just one more thing......when new thread is created ,it is provided a separate stack....so that means copies of instances are stored here only....please correct me if I am wrong....and 1 more thing you have mentioned that "If one copy is updated by one thread, the other thread might not see the new value.".....so in what case one thread can see value of any instance variable updated by other thread? – Bhavya Sharma May 24 '15 at 12:50