Sunday, August 26, 2012

Java Threading Programming Interview Questions 1


Q) Explain different way of using thread?
A) A Java thread could be implemented by using Runnable interface or by extending the Thread class. The Runnable is can be used for Thread because we can inherit one more class.

Q) What are the two types of multitasking?
A) process-based and Thread-based

Q) Thread Example by extending Thread Class
A) public class ThreadExample extends Thread{

public void run(){
System.out.println("Thread Starting");
for(int i=0;i<9;i++)
System.out.println(i);
}

public static void main(String args[]){
new ThreadExample().start();

}

}

Q) Thread Example by Implmenting runnable interface?
public class ThreadExample implements Runnable{

public void run(){
System.out.println("Thread Starting");
for(int i=0;i<9;i++)
System.out.println(i);
}

public static void main(String args[]){
Thread t1=new Thread(new ThreadExample());
t1.start();
}

}

Q) What is synchronization in respect to multi-threading in Java?
A) Synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one Java thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.
Example
=======
public class ThreadExample implements Runnable{

public  synchronized void run(){
System.out.println("Thread Starting");

{
for(int i=0;i<9;i++)
System.out.print(i);
}
}

public static void main(String args[]){
Thread t1=new Thread(new ThreadExample());
t1.start();

Thread t2= new Thread(new ThreadExample());
t2.start();

Thread t3= new Thread(new ThreadExample());
t3.start();

}

}

Q)Difference  Thread.start() & Thread.run() method?
A) Thread.start() method (native method) of Thread class actually does the job of running the Thread.run() method in a thread. If we directly call Thread.run() method it will executed in same thread, so does not solve the purpose of creating a new thread.

Q) Why do we need run() & start() method both. Can we achieve it with only run method?
A) We need run() & start() method both because JVM needs to create a separate thread which can not be differentiated from a normal method call. So this job is done by start method native implementation which has to be explicitly called. Another advantage of having these two methods is we can have any object run as a thread if it implements Runnable interface. This is to avoid Java’s multiple inheritance problems which will make it difficult to inherit another class with Thread.


Q) Can you declare a static method as synchronized?
A) Yes, we can declare static method as synchronized. But the calling thread should acquire lock on the class that owns the method.


Q) & A) Constrcutor in Thread Class : Thread(Runnable threadob,String threadName)

Q) & A) Metods available in the Runnable Interface? run()

Q) & A) What is the data type for the method isAlive() and this method is available in which class? : boolean, Thread

Q) What are all the methods available in the Thread class?
A)isAlive()
join()
resume()
suspend()
stop()
start()
sleep()
destroy()

Q) What are all the methods used for Inter Thread communication and what is the class in which these methods are defined?
A)wait(),notify() & notifyall() and Object class

Q) What is the mechanisam defind by java for the Resources to be used by only one Thread at a time? : Synchronisation

Q) Thread Priority values
A) MAX-10, MIN-1 NORMAL-5 and Method is setPriority()

Q)Default thread at the time of starting the program?: Main Method

Q) Thread States: new,runnable,blocked & dead

Q) The suspend()method is used to teriminate a thread? : False

Q) The run() method should necessary exists in clases created as subclass of thread? : True

Q) Which method waits for the thread to die ? : join() method

--wait(),notify(),notifyall() are defined as final & can be called only from with in a synchronized method
  wait(),notify(),notifyall() the wait() method only throws IOException

Q)Garbage collector thread belongs to which priority? : low-priority

Q) What is meant by daemon thread? In java runtime, what is it's role?
A) Daemon thread is a low priority thread which runs intermittently in the background doing the garbage collection operation for the java runtime system.

No comments:

Post a Comment

Thanks for your valuable comments