18 Mar 2013

Threading in Java

Well before going to threading in java, let us see what is a Thread.

Thread:Thread is a small process which will have same features as process but switching threads is simple and easy to the processor than switching processes,because process will have big tables associated with them for maintaining there states and lot more info needs to be save while switching and taking back the process but threads want to maintain such tables.

How do we create threads in Java?

Threads can be created in two ways in Java

1.Implement a Runnable Interface.
2.Extend Thread Class .

Implementing Runnable Interface appears to be more useful because as we know we can extend only one class in Java so if we extend Thread Class then we cannot extend any other Class so.

Knowing both exists is a good thing so let us learn both.

Example of creating a Thread through implementing Runnable interface.

      class Student implements Runnable
     {
         @Override
          public void run()
         {


             for( int i = 0 ; i < 10 ; i++ )
             {
                   System.out.println("Implementing Runnable interface");     
             }            
          }   
    
}


Runnable is a interface with only one method run() so we need to define that method in the implementing class (here Student).

Creating a Thread from the Threaded class(here Student).

      Student objStudent = new Student();//create an object of Class Student

      Thread threadStudent = new Thread(objStudent);//Pass object to Thread                                                                                          //constructor to make it a thread

How to start a Thread:

     threadStudent.start();

This will spawn a thread which will start executing method run() of Student class
asynchronously(term used to specify that the process wont happen in sequence but will happen parallely), that is now the statements in run() method will be executed parallely to statements following threadStudent.start().

Grouping threads


    We can Group threads while creating them, which will help us identify the threads by there groups.

     Create a thread group to which threads are added.

          ThreadGroup objStudGrp = new ThreadGroup("UGGroup");

     Create a thread with objStudGrp as thread group.

          Thread threadStudent = new Thread(objStudGrp,objStudent);

     
    We can find the count of Threads running in the current JVM  doing the following:

         Thread.activeCount();

    The above statement gives all the Threads running in the current JVM, but to differentiate  threads we created we can use the Thread Groups to find the count,

      objStudGrp.activeCount();

    We can make a thread wait, Say For Example:We created a two threads,

     Thread thread1 = new Thread(objStudGrp,new Student());

     Thread thread2 = new Thread(objStudGrp,new Student());
    
    start both the threads,

     thread1.start();
     thread2.start();

    Above statements starts the threads thread1 and thread2 i.e threads will execute what is there in run() method of class Student.If you want to make thread1 to wait for 10 seconds then you can do it using the following statement

   thread1.wait(10000); //thread1 waits for 10000 milliseconds i.e 10 seconds.

   If you want thread2 to wait till thread1 finishes then 

   thread1.start();//start both the threads
   thread2.start();

  
   synchronized( thread2 )
   {
         thread2.wait(); //make thread thread2 wait
   }

   // check if the thread thread1 is alive(still running)
   while( thread1.isAlive() )//returns true if the thread is started and not yet died.
   {
         Thread.sleep(1000);//let the current thread sleep for 1000 milliseconds before
                                       //checking again
   }

   
   //when the above  looping comes out then it implies thread1 is not alive anymore.
   //notify the thread2 to restart the execute
 
   thread2.notify();
  
     

    

















 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.