Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Presentation 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

SHRI 

VISHNU ENGENEERING COLLEGE FOR WOMEN

AUTONOMOUS

20B01A0594
20B01A05A5
20B01A05C9
TOPIC :  1 . CREATION OF THREADS
        
        2 . THREAD  PRIORITY
INTRODUCTION : 

THREAD :  Thread is an individual task that has independent flow


                   of  execution
                   Thread is in lang package.
ADVANTAGES : 
                    Reduces maintenance cost , Development Time. 
                    Parrallelized tasks. 
CREATION OF THREAD :
                     There are two ways to create thread.
                      1) By Extendiing Thread Class
                      2) By Implementing Runnable Interface.
                                                            
CREATION OF THREAD BY EXTENDING THREAD CLASS

RULES :                                

  Create  a Sub Class of a Thread Class.


  Class must Override run() method .
  it may override  other methods too.
 Create an Object of the Class that Extends Thread Class.
SYNTAX :
class MyThread  extends Thread {

          MyThread ( ) {
                     …...............; 
                     …...............;
     }
         public void run ( ) // must override
         {  
                     …...............; 
                     …...............;
     }
            …...............; 
}
public class Example {
          public static void main(String[] args) {
                      MyThread t=new MyThread ( ) ;
                      …...............; 
                      t.start( );
                      …...............;     }  }
A program to create a Thread by Extending class

class  MyThread extends Thread {


           public void run ( ) { OUTPUT :
                 try {
                 for ( int k=0;k<5;k++ ) { ChildThread : 0
                        System.out.println (" ChildThread : "+k );                                              MainThread : 0
                         Thread.sleep ( 1000 );  } ChildThread : 1
                  }             MainThread : 1
                 catch(InterruptedException e) { ChildThread : 2
                         System.out.println("ChildThread is Interrupted"); ChildThread : 3
class ThreadDemo { MainThread : 2
ChildThread : 4
          public static void main(String[] args) {
MainThread : 3
          MyThread t=new MyThread ( ) ; MainThread : 4
          t.start ( );
          try {
                 for ( int k=0;k<5;k++ ) {
                        System.out.println (" MainThread : "+k );
                         Thread.sleep ( 1000 );  }
                  }            
           catch(InterruptedException e) {
                         System.out.println("MainThread is Interrupted");  } } }
                    CRAETION OF THREAD BY IMPLEMENTS RUNNABLE

RULES :

• Create a thread class that will implement the runnable interface.

• In the thread class, write a function to override the run() method.

• Create an instance of the Thread class.

• The thread instance has a constructor which accepts the runnable object. Pass this object as a
parameter to the thread instance.

• Finally, call the start method of the thread instance.


SYNTAX:​
Class   MyThread  implements runnable Interface {​

                 public void run ( ) 
         {  ​
                     …...............; ​
                     …...............;​
          }​
          }​
public class Example {​
          public static void main(String[] args) {​
                     MyThread t=new MyThread();
                   Thread thread = new Thread(t); 
                      …...............; ​
                      t.start( );​
                       }  }​

               A program to create a Thread by Runnable interface

class SampleThread implements Runnable{ OUTPUT:


Thread about to
public void run() { start...
System.out.println("Thread is under Running..."); Thread is under
for(int i= 1; i<=10; i++) {
System.out.println("i = " + i);
Running...
} i=1
} i=2
} i=3
i=4
public class My_Thread_Test {
i=5
public static void main(String[] args) { i=6
SampleThread threadObject = new SampleThread(); i=7
Thread thread = new Thread(threadObject); i=8
System.out.println("Thread about to start..."); i=9
thread.start();
}
i = 10
}
Priority of a Thread:
1. Introduction :

  Here we'll discuss how the Java thread scheduler executes threads on a priority basis. 

  In Java, a thread's priority is an integer in the range 1 to 10. 

  The larger the integer, the higher the priority. 

  The thread scheduler uses this integer from each thread to determine which one should be allowed to execute. 
            
2.Types of Priorities :

            The Thread class defines three types of priorities:

               1.   MIN_PRIORITY            -            1

               2.   NORM_PRIORITY        -           5

               3.  MAX_PRIORITY             -           10

      NORM_PRIORITY is the default priority for a new Thread.


3) METHODS RELATED TO  THREAD PRIORITY :

• Java's Thread class provides methods for checking the thread’s priority and for modifying it.

• getpriority()

• Setpriority() 

•  The getPriority() instance method returns the integer that represents its priority.

• The setPriority() instance method takes an integer between 1 and 10 for changing the thread's
priority.   

• If we pass a value outside the 1-10 range, the method will throw an IllegalArgument Exception.           
        
Example:

import java.lang.*;  
public class ThreadPriorityExample extends Thread   

 public void run()  
{  
System.out.println("Inside the run() method");  
}  
public static void main(String argvs[])  
{  
ThreadPriorityExample th1 = new ThreadPriorityExample();  
ThreadPriorityExample th2 = new ThreadPriorityExample();  
ThreadPriorityExample th3 = new ThreadPriorityExample();  
System.out.println("Priority of the thread th1 is : " + th1.getPriority());  
System.out.println("Priority of the thread th2 is : " + th2.getPriority());   
System.out.println("Priority of the thread th2 is : " + th2.getPriority());  
th1.setPriority(6);  
th2.setPriority(3);  
th3.setPriority(9);   
System.out.println("Priority of the thread th1 is : " + th1.getPriority());      
System.out.println("Priority of the thread th2 is : " + th2.getPriority());    
System.out.println("Priority of the thread th3 is : " + th3.getPriority());    
System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName());    
System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());    
Thread.currentThread().setPriority(10);    
System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());  
}  
}  

    Output:

Priority of the thread th1 is : 5


Priority of the thread th2 is : 5
Priority of the thread th2 is : 5
Priority of the thread th1 is : 6
Priority of the thread th2 is : 3
Priority of the thread th3 is : 9
Currently Executing The Thread : main
Priority of the main thread is : 5
Priority of the main thread is : 10

You might also like