Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

thread priorities in java

// Java Program to Illustrate Priorities in Multithreading
// via help of getPriority() and setPriority() method
  
// Importing required classes
import java.lang.*;
  
// Main class
class ThreadDemo extends Thread {
  
    // Method 1
    // run() method for the thread that is called
    // as soon as start() is invoked for thread in main()
    public void run()
    {
        // Print statement
        System.out.println("Inside run method");
    }
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating random threads
        // with the help of above class
        ThreadDemo t1 = new ThreadDemo();
        ThreadDemo t2 = new ThreadDemo();
        ThreadDemo t3 = new ThreadDemo();
  
        // Thread 1
        // Display the priority of above thread
        // using getPriority() method
        System.out.println("t1 thread priority : "
                           + t1.getPriority());
  
        // Thread 1
        // Display the priority of above thread
        System.out.println("t2 thread priority : "
                           + t2.getPriority());
  
        // Thread 3
        System.out.println("t3 thread priority : "
                           + t3.getPriority());
  
        // Setting priorities of above threads by
        // passing integer arguments
        t1.setPriority(2);
        t2.setPriority(5);
        t3.setPriority(8);
  
        // t3.setPriority(21); will throw
        // IllegalArgumentException
  
        // 2
        System.out.println("t1 thread priority : "
                           + t1.getPriority());
  
        // 5
        System.out.println("t2 thread priority : "
                           + t2.getPriority());
  
        // 8
        System.out.println("t3 thread priority : "
                           + t3.getPriority());
  
        // Main thread
  
        // Displays the name of
        // currently executing Thread
        System.out.println(
            "Currently Executing Thread : "
            + Thread.currentThread().getName());
  
        System.out.println(
            "Main thread priority : "
            + Thread.currentThread().getPriority());
  
        // Main thread priority is set to 10
        Thread.currentThread().setPriority(10);
  
        System.out.println(
            "Main thread priority : "
            + Thread.currentThread().getPriority());
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: get SecretKey from String 
Java :: read many lines from stdn java 
Java :: resurce leak java 
Java :: interface in solidity 
Java :: model mapper to list stream 
Java :: how to read a table from text file in java 
Java :: date to yyMMdd conversion 
Java :: javafx list view does not update 
Java :: java stream map int to char 
Java :: java array merge 
Java :: difference between maven plugin and dependency 
Java :: org.springframework.security.oauth2.jwt.JwtEncoder 
Java :: remove selected text from string value android java 
Java :: android studio clear views of layout 
Java :: java add element to map 
Java :: 9999999999999 
Java :: retrofit post body 
Java :: array copy java 
Java :: The superclass "jakarta.servlet.http.HttpServlet" was not found on the Java Build Path 
Java :: difference between synchronized block and synchronized method example 
Java :: localbroadcastmanager android example 
Java :: firebase realtime database get key 
Java :: selenium java control + enter 
Java :: Java Creating an EnumMap 
Java :: java string replaceall for space 
Java :: java linked list swap elements 
Java :: java garbage collection 
Java :: super class tostring java 
Java :: each loop in java 
Java :: run specific test case junit 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =