Java Multithreading Interview Questions And Answers For Freshers

by Monday, January 30, 2012 0 comments
(Q) What is a Thread?
(A) Thread is a part of execution which functions independently to complete the process. Huge Java programs which runs without Multi Threading leads to dead lock problems.
(Q) Let`s say you have thread T1, T2 and T3, how will you ensure that thread T2 run after T1 and thread T3 run after T2?
(A) It can be achieved using join method of Thread class.
(Q) What is the difference between wait() and sleep() in Java?
(A) wait() method takes integer value (time) to make the process wait. While sleep() is used to pause the execution.
 (Q) What is synchronization in Multi-threading?
(A) Synchronization is the capability to control the access of multiple threads to shared resources. If we are not using Synchronization it may arise to significant error.
 (Q) What is the difference between Thread.start() method and Thread.run() method?
(A) Thread.strat() method is used to run the Thread.run() method in a thread. run() method is used to start the thread.
(Q) What is the difference between sleep() and suspend()?
(A)   Thread.sleep() sends the current thread into the “Not Runnable” state. suspend() is deprecated, A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.
(Q) How to create a thread in Java?
(A) Java team graciously designed two ways of creating threads.
One method is by implementing Runnable interface, in java program.
Another is by extending Thread class, in java program. ImplementingRunnable interface is the easiest method.
(Q) How to create a thread using Runnable interface?
(A) Implement Runnable interface using implements and add run() method.
Inside run() you need to define code that contains new thread.
Instantiate an object of type Thread.Thread defines several constructors one we use is..
Thread(Runnable threadOb,String threadName).
(Q) Write a sample program for Java Mutlithreading?
(A) Class A implements Runnable{
Thread t;
A(){
t=new Thread(this,”Demo thread”);
t.start();
}
public void run(){
try{
Thread.sleep(1000);
for(int i=0;i<8;i++){
System.out.println(“Values:”+i);
}
}
catch(InterruptedException e){
System.out.println(“fdf”+e);
}
System.out.println(“Out of loop”);
}
}
Class MultiThrd0{
public static void main(String ea[]){
new A();
System.out.println(“This is main()”);
}
}
(Q) In Java Multi threading where I need to place the main()?
It is recommended that main thread must be the last to finish running. In fact, for some older JVMs, if main thread finishes before a child thread, then Java run time system may hang.
(Q) What is thread priority?
(A) To see that all threads in an application runs equally, programmers use priority concept to set thread priority.
Example:
final void setPriority(int level);
Level should be replaced with MIN_PRIORITY and MAX_PRIORITY values. MIN_PRIORITY is 1 and MAX_PRIORITY is 10.
(Q) How to know the thread priority?
isAlive() —it returns true when Thread is running
join() these two methods are used to know thread priority.

Pankaj Mehndiratta

Network Engg

i am Network Engg in WIPRO