1

While passing a volatile variable to another class (both extend Thread Class), do I have to import this volatile variable as a volatile or just public definition would be enough?

ex:

public class A extends Thread{

     public volatile int a = 0;

 public void run(){
     B bObj = new B(a);
     a++;
 }
}

Now the class receiving a volatile variable, should use define the receiver variable as volatile or just public? like this

public class B extends Thread{

  public volatile int a;

    public B(int a){
     this.a =a ;
    }
  }

OR should be defined like this?

public class B extends Thread{

  public int a;

    public B(int a){
     this.a =a ;
    }
  }

With both classes exchanging the variable over some period of time or the other.

Any help is highly appreciated. Thanks

Johnny Sins
  • 71
  • 1
  • 10
  • 1
    There is no such thing as a _volatile object_. It's the variable that is `volatile`, not its value. – Sotirios Delimanolis Jun 23 '16 at 00:34
  • I meant Volatile variable . Sorry. Will edit it Now. – Johnny Sins Jun 23 '16 at 00:35
  • 1
    Still, method arguments are values, not variables. The variable (or argument expression) is evaluated to produce a value. _volatile_ does not apply to the value. – Sotirios Delimanolis Jun 23 '16 at 00:36
  • If you know what volatile means, you should be able to answer your own question. If you don't, you might want to take a few steps back and learn about it. – shmosel Jun 23 '16 at 00:37
  • So, should I define the inheriting class's 'a' variable as just public int a; ? – Johnny Sins Jun 23 '16 at 00:38
  • Have any links ? @shmosel – Johnny Sins Jun 23 '16 at 00:38
  • You can start [here](https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html), [here](http://stackoverflow.com/questions/106591/do-you-ever-use-the-volatile-keyword-in-java) and [here](http://tutorials.jenkov.com/java-concurrency/volatile.html). – shmosel Jun 23 '16 at 00:41

1 Answers1

0

Java is pass-by-value. In the B constructor, you are not storing a reference to the A object's a field in bObj; you are just storing the value. (In other words, B is not "receiving a volatile variable"; it is receiving a value that happens to be stored in a volatile field.) The value of the a field in the A object and the B object can change values independently.

With the code you posted, there's no need to declare A.a as volatile.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • In addition, declaring a variable 'volatile' then using auto-increment on it will lead to concurrency bugs. You need to synchronize the entire auto-increment operation to maintain consistency. Also, it is a simply terrible idea to extend 'Thread'. As another commenter mentioned, it is a good idea to learn what 'volatile' means - it means to coordinate reads and writes between threads to maintain the /happens-before/ relationships. – Lew Bloch Jun 23 '16 at 00:55
  • Thank you very much, for the replies. – Johnny Sins Jun 23 '16 at 01:06
  • @LewBloch - Good point about the postfix increment operator. However, since `volatile` isn't actually needed (at least in the code as posted there's no concurrency going on), there's no harm done. By the way, why is it "a simply terrible idea" to extend `Thread`? As long as one is using `Thread` (instead of another multi-threading utility), it's not much different from using `new Thread(Runnable)`. – Ted Hopp Jun 23 '16 at 01:09
  • @TedHopp, I guess When he said its terrible to extend Thread Class, he meant , since a class can `extend only one class, its rather useful to implement the `Runnable class. – Johnny Sins Jun 23 '16 at 01:22
  • @AdityaKulkarni - I have no idea what he means; that's why I asked. Sure, if you need to extend some other class, you can implement `Runnable` and then pass it to a `Thread` constructor; that provides more flexibility. However, it doesn't follow that extending `Thread` is a bad idea. – Ted Hopp Jun 23 '16 at 01:45