Interview Questions on Thread

Thread behavior is unpredictable ?

  1. Thread behavior is unpredictable -> becuase it depends on Thread Scheduler and thread scheduler depends on the env ,

  2. even the same program on same machine may produce different output

Example

  • creating a java program to implement runnable interface and creating two treads and triggering start method -> still the order of execution is differnt in both the tries :)
import java.util.Arrays;

class Test2 {

    public static void main(String args[]) throws InterruptedException {


        System.out.println("inside main" + Thread.currentThread() +"   ");
        MyRunnable r1 = new MyRunnable();
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r1);

        t1.start();
        t2.start();

        System.out.println(" main finished " + Thread.currentThread() +"   ");
    }
}

class MyRunnable implements Runnable {

    public void run() {

        System.out.println("inside run");
        for (int i = 0; i < 5; i++) {

            System.out.println(Thread.currentThread()+"  : "+ i);

        }
    }
}

Output of first run and second run will be different. :)

What will happen if we don't override run method of thread in java ?

  • we we create new thread using Thread Class and we do not override the run method that time nothing will happen -> since we will call the start method and it will look for run method

  • becuase internally Thread class implements the Runnable interface and run method of the parent class that is empty that will be called

What will happen if we try to start the tread again in java ?

  • we will get exception -> illegalThreadStateException

  • since once we call the start method it will internally calls the run method -> and run method is called for the first time the state is changed to dead state -> and now if we try to again change the state back to normal that is not possible :)

What will happen if we override the start method of thread in java ?

  1. if we override the start method of Thread class in that case that override method will be called not the start method of the parent class so it means, even if we override the run method in this case run method will not be called only the overriden start method will be called.

Difference between starting a thread with run and start method

  1. so what will happen -> when we call the run method directly -> new thread willl not be created and the main thread is already having a call stack -> so the run method will be pushed to that same call stack

    but in case of We are calling the start method in that case -> new thread will be created and run method will be pushed to new thread call stack

Volatile keyword in java - difference between synchronized and volaticle

why volatice variables are not cached in memory

  • volatile keyword makes sure that all the threads have the latest value of the variable

  • volatile variables are not cached in memory it will be always stored in main memory , these variables are also read from the stack where it is stored, that is why we can make sure we are always having the fresh copy

  • what if thread0 updated x and it is not cached properly

  • wha tif thread 1 tries to read the value from cache it will read the older value and hence there will be inconsistency :)