1. Difference between process and thread?
SN. | Process | Thread |
---|---|---|
1. | Process means any program is in execution. | Thread means segment of a process. |
2. | Process takes more time to terminate. | Thread takes less time to terminate. |
3. | It takes more time for creation. | It takes less time for creation. |
4. | It also takes more time for context switching. | It takes less time for context switching. |
5. | Process is less efficient in term of communication. | Thread is more efficient in term of communication. |
6. | Process consume more resources. | Thread consume less resources. |
7. | Process is isolated. | Threads share memory. |
8. | Process is called heavy weight process. | Thread is called light weight process. |
9. | Process switching uses interface in operating system. | Thread switching does not require to call a operating system and cause an interrupt to the kernel. |
10. | If one server process is blocked no other server process can execute until the first process unblocked. | Second thread in the same task could run, while one server thread is blocked. |
11. | Process has its own Process Control Block, Stack and Address Space. | Thread has Parents’ PCB, its own Thread Control Block and Stack and common Address space. |
2. In how many ways we can create threads in java?
There are two ways of defining a thread.
- By implementing runnable interface
class Multi3 implements Runnable{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi3 m1=new Multi3(); Thread t1 =new Thread(m1); t1.start(); } }
- By extending thread class
class Multi extends Thread{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start(); } }
3. Which is the best approach for creating thread ?
The preferred way of creating a thread is by implementing runnable interface. ie. When we extend a thread class,
- We can’t extend any other class even we require and When we implement Runnable, we can save a space for our class to extend any other class in future or now.
- Each of our thread creates unique object and associate with it. When we implements Runnable, it shares the same object to multiple threads.
4. Explain the life cycle of thread?
A thread can exist in one of the 5 states, According to sun, there are only 4 states in a life cycle of a thread and those are new, runnable, blocked and terminated. but for better understanding, we will describe thread in 5 states.
New Runnable Running Non-Runnable (Blocked) Terminated
New Whenever you define a new thread its in the new state. As soon as you call start()
method its state changes to Runnable state.
Runnable When a thread is in the runnable state, it dosen’t mean that thread is ready to run, If the thread scheduler allocates the processor to this thread then only it goes into running state.
Running As soon as thread scheduler selects the particular thread to run, it goes into running state.
Non-runnable or blocked A blocked state is reached when the thread is still alive but not eligible to run, this happens because either sleep()
or wait()
or lock or suspended methods are called.
once the thread goes into blocked, it can has to come to runnable state by methods like resume()
, notify()
or notifyAll()
or when sleep()
time is over.
Terminated or dead a thread enters into this state once the run()
method execution ends. This can be better understood by the state diagram below
5. Can we restart a dead thread in java?
If you look at the life cycle diagram above, there is no way that a dead thread can again go to a runnable state. Instead you can create a new thread if you require one.
6. Can we overload run() method in java?
run() method can be overloaded but the overloaded run method will be treated as a normal method and have to be called explicitly. let’s look at this example.
// Java Program to illustrate the behavior of // run() method overloading class Tecnotab extends Thread { public void run() { System.out.println("tecnotabs.com"); } public void run(int i) { System.out.println("Script your ideas"); } } class Test { public static void main(String[] args) { Tecnotab t = new Tecnotab(); t.start(); } }
Output
tecnotabs.com
If you see in the above example, when t.start()
is called, run method with no parameters will be executed, if you have to run the run()
method with argument, you need to call explicitly like t.run(1)
;
7. What is a lock or purpose of locks in java?
Lock is an Interface which is used to manage Synchronization in java. And the package java.util.concurrent
contains lots of implementation of locking mechanism.
There are two types of locks
- Object level lock Every Object is java has a unique lock and whenever we are using synchronized keyword only lock concept will come to picture. If a thread wants to execute a synchronized on any object of a method then it has to get get the lock of that object first.
- class level lock Every class in java has a unique lock which is nothing but class level lock, whenever any thread wants to execute any static synchronized method it has to obtain lock of that class.
8. In how many ways we can do synchronization in java?
First of all, you should know that there are two types of synchronization in java. (1) Process Synchronization (2) Thread synchronization
Here will will discuss everything about thread synchronization.
There are two types of thread synchronization
- Mutual exclusive
- Synchronized method
- Synchronized block
- Static Synchronization
- Inter-Thread Communication (cooperation)
9. When do we use synchronized methods in java?
Synchronized is a keyword in java which is used to give mutual exclusive access to the shared resources. You can define a method as synchronized in java just by using synchronized keyword before the method and it guarantees that no two threads can execute a synchronized method concurrently. Syntax:
synchronized void methodTest(){ //synchronized method body }
10. What is deadlock?
Deadlock is a situation in java where two or more threads are waiting for each other to the the lock of an object but gets blocked forever. Here is an example.
In multithreaded environment, you should write code in such a order that your code should never go in deadlock situation.
11. What is class level lock ?
Class level lock when you want to synchronize a static method or block a class level lock is required. Suppose you are using 10 instances of a class, only one thread will be able to execute that static method or block. This is used to protect static data.