0

i am not sure what title should i give to this question. basically my question is how thread share variable in ints own instance class: for e.g please check below code :

public class MainJava {
    public static void main(String[] args) throws InterruptedException {
        MyThread obje1=new MyThread(25);
        MyThread obje2=new MyThread(50);
        Thread t1=new Thread(obje1);
        t1.setName("Thread"+25);
        t1.start();
        Thread.sleep(2000);
        Thread t2=new Thread(obje2);
        t2.setName("Thread"+50);
        t2.start();
    }
}

class MyThread implements Runnable {
    int i;
    String threadName;

    public MyThread(int i) {
        this.i = i;  
    }

    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("Thread25")) {
            try {
                Thread.sleep(50000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
        }
        System.out.println(i +" FOR THREAD : "+Thread.currentThread().getName());
    }

}

in this thread , my assumption was Thread50 will overwrite value of i to 50 and subsequently Thread25 will be read value of i as 50. As i is instance variable and it will be shared between two threads. but output was 50 FOR THREAD : Thread50 25 FOR THREAD : Thread25 so i am confused about this outout. Can someone help me understand this. Thanks in advance

  • Use `static int i`? Learn `static`. It is `class` level not instance level. – Zaw Than oo Jul 24 '14 at 07:23
  • `i` is contextual to the individual instance of the class, so both `Thread50` and `Thread25` have there own copies that are not effected by the other... – MadProgrammer Jul 24 '14 at 07:24
  • @CycDemo why should i use static int. I dont wont it be share between the threads. – bluesky Jul 24 '14 at 07:27
  • @MadProgrammer yes after posting question i realized this, as two thread are separated thread object. But suppose in stead of int i , i put UserDefinedClassObject obje. then there will issue of sharing which i need to handle am i right? – bluesky Jul 24 '14 at 07:28
  • Check here http://stackoverflow.com/questions/3493752/java-share-a-variable-between-two-threads – Zaw Than oo Jul 24 '14 at 07:30
  • Assuming that it's the same instance `UserDefinedClassObject`, then, yes, you could have issues... – MadProgrammer Jul 24 '14 at 07:36

2 Answers2

0

MyThread obje1=new MyThread(25); and MyThread obje2=new MyThread(50); are two separate instance, what you are doing is create two threads that execute the run method independently one to the other. so there´s nothing to share.

The different in this example would be if the two Thread class that you have created was using the same MyThread instance. In that case yes, the second thread would change the values from the other thread

paul
  • 12,873
  • 23
  • 91
  • 153
0

"Instance variable" means that each instance of your class gets its own copy of the variable. If you want a single variable that is shared by all instances of the class†, then declare the variable to be static.


† Some day you will learn why static variables are a bad approach to software design, but not today.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57