Threading | Multi Threading Interview Questions and Answers | Real time Questions and Answers

Threading | Multi Threading Interview Questions and Answers | Real time Questions and Answers

Q) What threads will start when you start the java program?

A) Finalizer, Main, Reference Handler, Signal dispatcher. 

Q) Thread

Thread is a smallest unit of dispatchable code.

 Q) Diff process and threads?

A) Thread is a smallest unit of dispatchable code, Process is a sub program will perform some specific actions.

    (I) Process is a heavy weight task and is more cost. (ii) Thread is a lightweight task, which is of low cost.

    (iii) A Program can contain more than one thread. (v) A program under execution is called as process. 

Q) Sleep(), wait(), notify(), notifyAll(), stop(), suspend(), resume()

 sleep       -sleep for a thread until some specific amount of time.

wait         - wait for a thread until some specific condition occurs (Or) Tells the calling thread to give up the              monitor and go to sleep until some other thread enters the same monitor and calls notify().

notify( )    - wakes up the first thread that called wait() on the same object.

notifyAll( )- wakes up all the threads that called wait() on the same object, the highest priority thread will run first.

stop( )       - The thread move to dead state.

suspend( ) & resume( ) - To pass and restart the execution of a thread. In case of suspend, thread will be suspended by calling the lock on the object. Resume will restart from where it is suspended.

join( )        - wait for a thread to terminate.

 Q) Yield( )

            Yield method temporarily stop the callers thread and put at the end of queue to wait for another turn to be executed. It is used to make other threads of the same priority have the chance to run.

 - Thread.sleep(milliseconds);

 - Thread.sleep(milliseconds, nanoseconds);

 Q) Multi Threading

Multithreading is the mechanism in which more than one thread run independent of each other within the process.

 Q) Daemon Thread      

            Daemon thread is one which serves another thread, it has no other role normally a daemon thread carry some background program. When daemon thread remains the program exist.

 Q) Thread Priority

            MIN_PRIORITY = 1

            NORM_PRIORITY = 5

            MAX_PRIORITY = 10

 Q) Can I restart a stopped thread?

A) Once a thread is stopped, it cannot be restarted. Keep in mind though that the use of the stop() method of Thread is deprecated and should be avoided.

 Q) Thread Priorities

        Class A implements Runnable{

Thread t;

Public clicker(int p){

            T = new Thread(this)

            t.setPriority(p);

             }

public void run(){

}

public void stop(){

}

public void start(){

t.start();

}

try{

            thread.sleep(1000);

    }

lo.stop();

hi.stop();

try{

            hi.t.join();

            lo.t.join();

    }

class HiLo{

public static void main(Stirng args[]){

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

Clicker hi = new Clicker(Thread.NORM_PRIORITY+2);

Clicker lo = new Clicker(Thread.NORM_PRIORITY-2);

Lo.start();

Hi.start();

 Q) What is the use of start() function in starting a thread? why we do not use the run() method directly to run the thread?

Start method tell the JVM that it needs to create a system specific thread. After creating the system resources it passes the runnable object to it to execute the run() method.

- Calling run() method directly has the thread execute in the same as the calling object, not a separate thread of execution.

 Q) What are the different levels of locking using ‘Synchronize’ key word?

A) Class level, method level, object level, block level

 Q) Which attribute are thread safe?

Objective

Multi Threaded Model

Single threaded Model

Local variables

Y

Y

Instance variables

N

Y

Class variables

N

N

Request attributes

Y

Y

Session attributes

N

N

Context attributes

N

N