Monday, October 25, 2010

Multithreading in Java

Question : What is Thread-based Multitasking?
Ans : Concept of executing more than one functionality simultaneously 
belonging  to the same memory domain(i.e., same program) is known as 
Thread based multi Tasking.


Question : How to Implement thread based multitasking in Java ?
Ans : Using Thread Class and Runnable Interface .


Description : The Runnable interface has a Function definition public void run().
For which we need to provide the body,and execute this functionality as a Thread.


Note : Start() method in the Thread class, recognizes the run() method of 
runnable Interface and then the run() method is executed as a Thread.


Example Program :
Class MSD extends Thread
{
   public void run()
{
   for(int i=0; i<40;i++)
{
 Syso("Inside run method");
}
}
public static void main(String args[])
{
   MSD a = new MSD();
   a.start();
for(int i = 45; i <50;i++)
Syso("sekhar");
}
}
Note: Calling a run method with object of the class, execute the run method as
just a normal method instead of Thread.so we should handover this method to
OS , to start execute this as a thread.


Question : How to handover this run() method to OS to execute it as a Thread?
Ans : By calling the start() method in class Thread.


Note: one class can have only one run() method.
So if we want more threads we need to define more classes, one for each thread.


Note: Output of the Thread Program cannot be guessed,it is based on the
 how the OS schedules the each thread.


Note : In java a class cannot extend two classes simultaneously.


Hierarchy of the Thread classes.
Interface : Runnable has method run();


interface Runnable
{
   public void run();
}


and


Class Thread implements Runnable
{
   public void run() { ----- -------- }
}


Question: How to Create  a Thread in a class,without extending the Thread class?
Answer: By Implementing the Runnable interface.


Question : What is start() method Will do ?
Ans: Remember a start() method,is to handover the run() method present
 in the object  on which the start() method is called to the local OS for scheduling.
once the start method is called then JVM allocated the stack to that thread.
and push the thread from "new" state to runnable state.


Question : In Which class the the Start() method presents?
Ans:   start() presents in the Class Thread.


Excellent Question :
Question : How can we make object of one class available to object of another
 class without using extends Keyword.
ANS: use constructor.


Observe the Below Program for better Explanation.


Class ThreadA implements Runnable
{
  public void run()
{
   Syso("Creating thread without extending thread class");
}
public static void main(String[] args)
{
  ThreadA obj = new ThreadA();
  Thread   threadObj = new Thread();
  threadObj .start();
}
}


Description:
we are creating a thread by implementing a Runnable interface, in-order to 
make run() method as a thread we need to have start method. But start method
present on thread class,and run() method present on the class ThreadA.


If we call threadClassObject.start(), the JVM will checks whether any run() is
there on the object threadClassObject. here is there is no run() method on the
object, threadClassObject.It has one run() method that is on the object ThreadA() class.


Question: How come object of one class can access the object of another class.
Ans: As i said earlier, it uses the constructor.


so by passing the object of class ThreadA(),we can access the object of class 
ThreadA.


Thread threadClassObject = new Thread(ThreadAclassObject);


Now thread class object can access the object of ThreadA class and can call the
method run() on the object ThreadA.


This is called Runnable Target.


Question : How to Control the Execution of a Thread?
Ans :
                 A Thread can be suspended in 3 ways.
1) Based on time, by using sleep(non-seconds) .
Note: Sleep() method can through a checked Exception, so we should put sleep()
method in try -- catch block.
2)Conditionally.
Two threads thread t1, thread t2 were present.
we can suspend the execution of thread t2 until thread t1 finishes it's execution.
by using join() function.
3) UN-Conditionally.
Using the non static method suspend() of the thread class.
Note: suspended thread can resume back by using the method resume().
Note: But resume()  and  suspend() methods are deprecated.Instead of those,
wait() and notify() methods of object class came into existence.


Note: Join() Method in Java used for conditional suspension of a thread.
Note:  wait() method works only in synchronized block.


Note: Constructor of thread class is defined to accept object of runnable interface.


Note: Always a notify() should be called before the wait() method.


Note: notify() method resumes only one thread which was already in wait state.


Note: notifyall() method resumes all threads which are in wait state.


Question: If more number of threads present in the wait() state, what order
 in which the notify() method will resume the threads.
Ans: Resuming the threads which are in wait() state is completely depends on OS.


Question: How to Overcome the above problem.
Ans: Assign priorities to threads.


Question: What is the possible priority values for the threads?
Ans : Ranges from 1 to 9 .


Question: How the Os schedules the Threads?
Ans: Os will never schedule the threads depending on the Priority.


Question : What is the default priority of the threads?
Ans: 5,  because all threads are spawn from the main thread only , all threads 
have the default priority of the parents.


Note: As long as the thread JVM executes, the garbage collector will also be 
in Execution.


Question : What is the datatype for the parameter of the sleep() method?
Ans: long.


Question : Which method waits for a thread to die?
Ans: join() method.


Question : What happens when start() method is called on an already running
 thread?
Answer: IllegalStateException is thrown

Question : Which is best approach to create a thread implementing Runnable 
        or extending Thread?
A: Implementing Runnable. Because we still have opportunity to extend another 

class as  well as implement  more interfaces.

Note: If a Class Extends the Thread Class,  then there is no necessity to that
 class  to override the  run() method in thread Class.
Example: Class ThreadA extends Thread
{
    /* public void run(){
   System.out.println("Still Fine Without the run Method");
}*/
}
Question :What is the difference between process and thread?
A thread is a separate path of execution in a program. A Process is a program in execution.

Qns : ThreadPool Implementation in Java?
This Topic is left.
Refer this Article : http://java.sun.com/developer/Books/javaprogramming/threads/chap13.pdf
http://www.informit.com/articles/article.aspx?p=30483&seqNum=6

A Good Article On Threads in Java ..


http://www.java-questions.com/Threads_interview_questions.html

No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...