MultiThreading-Core Java Interview Question


MultiThreading-Core Java Interview Question


Q:What is the difference between processes and threads?
A:A thread is sometimes called a lightweight process which has its own execution stack.
    A process is an execution of a program but a thread is a single execution sequence within the process. A
    process can contain multiple threads.

   A JVM runs in a single process and threads in a JVM share the heap belonging to that process.
  That is why several threads may access the same object. Threads share the heap and have their own   
  stack space.This is how one thread’s invocation of a method and its local variables are kept thread safe
  from other  threads. But the heap is not thread-safe and must be synchronized for thread safety.

______________________________________________________________________________

Q:Explain different ways of creating a thread?

A:There are two ways to create Threads in Java .

Extending the Thread class
Implementing the Runnable interface.

Example By Using :􀂃 Extending the Thread class
class Counter extends Thread {
//method where the thread execution will start
public void run(){
//logic what need to execute
}
//let’s see how to start the threads
public static void main(String[] args){
Thread t1 = new Counter();
Thread t2 = new Counter();
t1.start(); //start the first thread. This calls the run() method.
t2.start(); //this starts the 2nd thread. This calls the run() method.
//Note: start() method need to use to execute (run ) a thread.
}
}

Example By Using :Implementing the Runnable interface.
class Counter extends Base implements Runnable {
//method where the thread execution will start
public void run(){
//logic to execute in a thread
}
//let us see how to start the threads
public static void main(String[] args){
Thread t1 = new Thread(new Counter());
Thread t2 = new Thread(new Counter());
t1.start(); //start the first thread. This calls the run() method.
t2.start(); //this starts the 2nd thread. This calls the run() method.
}
}

Note:Its good to use "
Implementing the Runnable interface" to create a thread .It can help you to use Inheritance ,If you want to extend your base class for new implementation. 


______________________________________________________________________________

Q:Explain different states of a threads ?
A:There are below given states can be possible for a thread.

Runnable(Just ready to Run-waiting for Turn) — waiting for its turn to be picked for execution by the thread scheduler based on thread priorities.
Running: The processor is actively executing the thread code. It runs until it becomes blocked, or voluntarily gives up its turn with this static method Thread.yield(). Because of context switching overhead, yield() should not be used very frequently.
Waiting: A thread is in a blocked state while it waits for some external processing such as file I/O to finish.
Sleeping: Java threads are forcibly put to sleep (suspended) with this overloaded method:
Thread.sleep(milliseconds), Thread.sleep(milliseconds, nanoseconds);
Blocked on I/O: Will move to Runnable after I/O condition like reading bytes of data etc changes.
Blocked on synchronization: Will move to Runnable when a lock is acquired.
Dead: The thread is finished working.


(Image sourced from: http://www.wilsonmar.com/1threads.htm)

______________________________________________________________________________


Q:What is the difference between wait and sleeping?

A:Main difference between wait and sleep is that wait() method release the acquired monitor when thread is waiting while Thread.sleep() method keeps the lock or monitor even if thread is waiting.In short, wait release lock on object while waiting while sleep doesn’t release lock while waiting.

-wait(an Object class method)  is called on Object while sleep is called on Thread.
-wait is normally done on condition, Thread wait until a condition is true while sleep is just to put your thread on sleep.
-wait is called from synchronized context only while sleep can be called without synchronized block
-waiting thread can be awake by calling notify and notifyAll while sleeping thread can not be awaken by calling notify    
  method.















Comments