Setting a static variable in a run () method in java

264 Views Asked by At

I have a static variable which I would like to set in a run() method. I have the following:

public class Test{
   public static int temp;
   public static void main(String [] args)
     {
         update();
         System.out.println("This is the content of temp"+temp);
     }
   public static void update()
    {
        (new Thread() {
        @Override
        public void run() {
          // do some stuff
                   Test.temp=15;
       }}).start();
    }

I would like the content of temp to be updated to 15; but when I print it in the main function, it shows 0. How can this be fixed?

2

There are 2 best solutions below

0
Filipp Voronov On BEST ANSWER

Threads working concurrently so you should wait until your new thread finishes:

public class Test{
   public static int temp;
   public static void main(String [] args) {
      update().join(); //we wait until new thread finishes
      System.out.println("This is the content of temp"+temp);
   }

   public static Thread update() {
       Thread t = new Thread() {
          @Override
          public void run() {
             // do some stuff
             Test.temp=15;
          }
       };
       t.start();
       return t;
    }
0
Shreyas Sarvothama On

You have to understand how Thread works.

I will show you two pieces of code here first is to understand, variables that are initialized inside the thread takes take time to update until the thread is finished.

public class Num {

    public static int temp;
       public static void main(String [] args) throws InterruptedException
         {
            update();

             System.out.println("This is the content of temp"+Num.temp);//This will print before temp=15 is updated
         }
       public static void update()
        {
            (new Thread() {
            @Override
            public void run() {
              // do some stuff
                       Num.temp=15;   
                       System.out.println("Value of temp:"+Num.temp);//This statement prints after
           }}).start();
        }
}

It Prints the following:

This is the content of temp0
Value of temp:15

Second one shows, if you wait for a small amount time(Thread.sleep(10)) after the thread is executed, the value gets updated:

public class Num {

    public static int temp;
       public static void main(String [] args) throws InterruptedException
         {
            update();
            Thread.sleep(10);
             System.out.println("This is the content of temp"+Num.temp);//This will print correct value now
         }
       public static void update()
        {
            (new Thread() {
            @Override
            public void run() {
              // do some stuff
                       Num.temp=15;                      
           }}).start();
        }
}

But here I would suggest the same method as Philip did. Just add throws InterruptedException in main function