Como ya sabemos, Java, que está completamente orientado a objetos, funciona dentro de un entorno de subprocesos múltiples en el que el programador de subprocesos asigna el procesador a un subproceso en función de la prioridad del subproceso. Cada vez que creamos un hilo en Java, siempre tiene asignada alguna prioridad. JVM puede dar prioridad al crear el hilo o el programador puede darla explícitamente.
Prioridades en subprocesos es un concepto en el que cada subproceso tiene una prioridad que, en lenguaje sencillo, se puede decir que cada objeto tiene prioridad aquí, que se representa mediante números que van del 1 al 10.
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()); } }
Java
// Java program to demonstrate that a Child thread // Getting Same Priority as Parent thread // Importing all classes from java.lang package import java.lang.*; // Main class // ThreadDemo // Extending Thread class class GFG extends Thread { // Method 1 // run() method for the thread that is // invoked as threads are started public void run() { // Print statement System.out.println("Inside run method"); } // Method 2 // Main driver method public static void main(String[] args) { // main thread priority is set to 6 now Thread.currentThread().setPriority(6); // Current thread is accessed // using currentThread() method // Print and display main thread priority // using getPriority() method of Thread class System.out.println( "main thread priority : " + Thread.currentThread().getPriority()); // Creating a thread by creating object inside // main() GFG t1 = new GFG(); // t1 thread is child of main thread // so t1 thread will also have priority 6 // Print and display priority of current thread System.out.println("t1 thread priority : " + t1.getPriority()); } }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA