Friday, 15 April 2016

THREAD

               Well on this time we are going to discuss about thread in java. Thread is a light weight sub process, a smallest unit of processing. Thread uses the multi-threading to run multiple threads simultaneously in the program, Multi-threading uses the common memory area for all the process.

                 In multitasking there are two types of thread they are,

                                                                   *   Process-based
                                                                   *   Thread-Based

PROCESS BASED:

                 It is a heavy-weight sub process, it uses the separate memory area for each process and it takes time while switching from one process to another process.

THREAD BASED:

                 It is a light-weight subprocess, it uses the signal memory area for all the process and it takes the less time for switching from one process to another process

                  In java there are five states of thread , but according process, Sun there is four states they are new , Runnable , non-runnable and terminate.

                  New In the beginning thread is in new state if you create an instance of thread class but before the invocation of start() method

Runnable-> The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

Running-> The thread is in running state if the thread scheduler has selected it.

Non-Runnable-> This is the state when the thread is still alive , but is currently not eligible to run,

Terminated-> A thread is in terminated or dead state when its run() method exits.

MAIN THREAD:

                 In java it contain the main thread on an main method.There are many thread methods are used in java they are start(), sleep(), run(), join(), wait(), isAlive, getpriority(), gtcurrentthread(), etc.

                  There are two ways to access a thread in java they are 
                                                  *   By implementing the Runnable
                                                  *  By extending the Thread

Implementing the runnable:

                 While implementing the runnableThreadExa implements 

                                              Runnable{
                                             @override
                                              public void run()
                                           {
                                        //TODD AUTO-GENERATED METHOD STUB
                                                        for(int i=5;i>0;i--)
                                                  {
                                          try{
                                                          Thread.sleep(500);
                                                          System.out.println(i);
                                                   }
                                                      catch(InterruptException e)
                                             {
                                            //TODD AUTO-GENERATED CATCH BLOCK
                                                       e.printStackTrace();
                                                   }
                                                 }
                                                      }
                             public static void main(String args[])throws InterruptedException
                                                   {
                                              RunnableThreadExa runn=new RunnableThreadEXa();
                                                         Thread t=new thread(runn);
                                                         Thread t1=new thread(runn);
                                                         Thread t2=new thread(runn);
                                                         system.out.println(t.getName());
                                                               t.setname(jarvis);
                                                               t.start();
                                                                t.start();
                                                                t.join();
                                                                  }
                                                                   }

Extending the thread:


                                            public class extendThread extends thread
                                             {
                                                      pubic void run()
                                            {
                                            //TODD AUTO-GENERATED METHOD STUB
                                                       for(int i=5;i>0;i++)
                                            {
                                                           try{
                                                          sleep(500);
                                                        }

                                                        catch (InreruptsException e){
                                            //TODD AUTO-GENERATED CATCH BLOCK 
                                                       e.intracellular();
                                                              }
                                                          }
                                                             }
                                                    public static void main(String args[])
                                                                               throws exception
                                                       {
                                                          thread t=new thread();
                                                          thread t1=new thread();
                                                        system.out.println(t.getName);
                                                            t.setName(jarvis);
                                                            t.start();
                                                             t.start();
                                                             t.join();
                                                                   }
                                                                        } 

That's all for today guys we have seen threads and we ill see other topic in next session

No comments:

Post a Comment