Ads Sponsored By Google :)

Friday, 18 January 2013

extending|methods of java.lang.thread|thread class in java|java6 with example


methods of java.lang.thread class  java.lang.thread in java thread class in java thread class in java 6 methods of thread class in java thread class methods in java methods in thread class in java thread class in java example extending thread class in java extending thread class in java with example

        
                Java.Lang.Thread: Or Thread Class in Java.Lang.*

           In multi threading a flow of control can be created by using Java.Lang.Thread class 
      in three ways
a)      By using new operator (dynamic memory allocation operator)directly.
Eg: Thread tobject=new Thread();
b)      By using Factory Method
Eg: Thread tobject=Thread.currentThread();
c)       An object of sub class of Java.Lang.Thread class is an object of Java.Lang.
       Thread class

Class thderive extends Thread
{
   ----------
   ----------
}
thderive th1=new thderive();
                Here th1 is an object of thderive class, thderive is subclass of thread class, 
          indirectly th1 is an     object of Java.Lang.Thread.
        
          
         Profile of Java.Lang.Thread Class :-
          Data members:
1)      Public static final int MAX_PRIORITY(=10);
2)      Public static final int MIN_PRIORITY(=5);

 Constructors:
1)      Thread():
       This constructor is used for creating an object of Thread class without giving user friendly name of the thread.

Eg: Thread  t1=new Thread();
Here t1 is called reference / object name.

2)      Thread(String):
     This constructor is used for creating an object of Thread class by using user friendly name to the thread.

Eg: Thread t1=new Thread(“Javajavax”);
Here t1 is refrence and javajavax is user friendly message.

3)      Thread(Runnable):
     This constructor is used for converting Runnable interface object into Thread class object for entering into run() method of Runnable interface, by using of all the methods of Thread class specially start().

Eg: thderive th1=new thderive();
thderive is subclass of Runnable interface.
Then
   th1.start();// INVALID
   Thread t1=new Thread(th1);// th1 reference is kept in t1.
With this constructor we are unable to give user friendly name of the Thread.

4)      Thread(Runnable,String):
   This constructor functionality is exactly similar to Thread(Runnable) but addition to it we are able to give user friendly name of the thread.
Eg: Runnable r=new Th1();
      r.start();// INVALID
     Thread t1=new Thread(r,”JavaJavax”);
     t1.start();

                     Instance Methods:
1)      Public final void setN
                    ame(String)
2)      Public final String getName()
      By using above methods are used for setting user friendly name to the thread and getting the name of the thread from thread class object respectively.
Eg: Thread t1=new Thread();
 String tname=t1.getName();
 System.out.println(tname);//Thread_0 default name of the thread
t1.setName(“javajavax”);//setting user friendly messages
3)      Public final void setPriority(int)
4)      Public final int getPriority()
     The above 2 methods are used for setting the priority to the thread and getting the priority from threads.

Eg: Thread t1=new Thread(“JavaJavax”);
      int pri=t1.getPriority();
      System.out.println(“pri);//5
t1.setpriority(Thread.MAX_PRIORITY);
pri.getPriority();
System.out.println(pri);//10

1)      Public static void start():
    This method is used for performing the following operations.
a)      Transfers the thread from new state to ready state.
b)      It provides the features of threading (Concurrency, synchronization, inter thread communication etc..)
c)       This method automatically calls the run().

2)      Public void run():
    This method is used for providing the logic of the thread or provide the logic of the thread in user defined method and call the user defined method as a part of run() body.
        Originally run() defined in Thread class with null body and as a java programmer we need to override in our sub class by extending from Thread class.

Eg:     Class th1 extends Thread
        {
            Public void run()
          {
           --------------
           ---------------
           }
       }
    th1 t1=new th1();
    t1.start();

3)      Public final Boolean isAlive():
     This method is used for checking execution status of the thread. This method returns true provided the thread is in ready, running and waiting states. This method returns false provided the thread is in new and halted states.

4)      Public final void Suspend():
    This method is used for suspending the currently executing thread. When we call suspend() upon the thread class object, the temporary execution details of the thread will be saved in process / job control block, and the suspended thread entered into waiting state from running state.

5)      Public final void resume():
       This method is used for transferring the thread from waiting state to ready state. When   the thread is resumed , it starts continuing its execution where it left off. Before its suspension by retrieving the temporary execution details from process control block (PCB).

6)      Public final void stop():
      This method is used for stopping or terminating the currently executing thread. When the thread is stopped its execution, it will be entered into halted state. When the stopped thread’s starts executing, it starts executing from the beginning.

7)      Public final void Join():
     This method is used for joining the threads which are completed their execution as single unit for improving the performance of multi threading applications.
    The default environment of multi threading says when for ground threads are completing their execution, Thread Group Name is individually collecting threads at once and collectively handover to garbage collector, which is one of the time consuming process.
  So for collecting all the completed threads at once and collectively handover to garbage collector at once by ThreadGroupName at once.
  This method throws an Exception called InterruptedException in busy environment.
To handle exceptions as follows:

Eg:
try
{
T1.Join();
T2.Join();
T3.Join();
}
Catch(InterruptedException ie)
{
System.out.println(ie);
}
          
      Static Methods:

1)      Public static final Thread CurrentThread()
           This method is used for obtaining the threads which are by default running in java execution environment.

2)      Public static final void sleep(long millisecond)throws InterruptedException:
          This method is used for making the currently executing thread to sleep for a period of time in terms of milliseconds. Once the sleep time is completed, the thread will be entered into ready state from waiting state.

The Following Example Illustrate The Concept Of Thread Creation and Executing the logic in run() by using Thread Class

Class ThDemo extends Thread{
    Public void run()  { 
       try     { 
       for(int i=1;i<=10;i++)
      {
        System.out.println(“the value of I”+i);
        Thread.Sleep(1000);
          }
        }
       Catch(InterruptedException ie)
      {
          System.out.println("Problem in thread execution");
       }
     }// run()
    }
   Class thdemo{
      Public static void main(String args[])
     {
        ThDemo t1=new ThDemo();
         t1.start();
          try{
              Thread.Sleep(1000);
              }
          Catch(InterruptedException ie)  {
     System.out.println("Problem in Thread Execution");   
              } 
      }
  }


     The following concepts are supports Multi threading
  •       Life Cycle Of Threads
  •       Ways of Creating Threads
  •       Java.Lang.Thread
  •       Java.Lang.Runnable
  •       Internal Flow Of Thread
  •       Synchronization
  •       Inter Thread Communication
  •       Multi Threading PPT



No comments:

Post a Comment

LinkWithin