Diferencia entre revisiones de «Exemplo de uso de prioridades con fíos»
Ir a la navegación
Ir a la búsqueda
| Línea 16: | Línea 16: | ||
DemoPrioridadGet t3 = new DemoPrioridadGet(); | DemoPrioridadGet t3 = new DemoPrioridadGet(); | ||
| − | System.out.println("Prioridad del hilo t1 : " + | + | System.out.println("Prioridad del hilo t1 : " + t1.getPriority()); // Por defecto 5 |
| − | + | System.out.println("Prioridad del hilo t2 : " + t2.getPriority()); // Por defecto 5 | |
| − | System.out.println("Prioridad del hilo t2 : " + | + | System.out.println("Prioridad del hilo t3 : " + t3.getPriority()); // Por defecto 5 |
| − | |||
| − | System.out.println("Prioridad del hilo t3 : " + | ||
| − | |||
t1.setPriority(2); | t1.setPriority(2); | ||
| Línea 27: | Línea 24: | ||
t3.setPriority(8); | t3.setPriority(8); | ||
| − | // t3.setPriority( | + | // t3.setPriority(11); arrojará IllegalArgumentException |
| − | System.out.println("Prioridad del hilo t1 : " + | + | System.out.println("Prioridad del hilo t1 : " + t1.getPriority()); //2 |
| − | + | System.out.println("Prioridad del hilo t2 : " + t2.getPriority()); //5 | |
| − | System.out.println("Prioridad del hilo t2 : " + | + | System.out.println("Prioridad del hilo t3 : " + t3.getPriority());//8 |
| − | |||
| − | System.out.println("Prioridad del hilo t3 : " + | ||
| − | |||
// Hilo Principal (Main thread) | // Hilo Principal (Main thread) | ||
System.out.print(Thread.currentThread().getName()+": "); | System.out.print(Thread.currentThread().getName()+": "); | ||
| − | System.out.println("Prioridad del hilo Main : " | + | System.out.println("Prioridad del hilo Main : " + Thread.currentThread().getPriority()); |
| − | |||
// La prioridad del hilo principal se establece en 10 | // La prioridad del hilo principal se establece en 10 | ||
Thread.currentThread().setPriority(10); | Thread.currentThread().setPriority(10); | ||
| − | System.out.println("Prioridad del hilo Main : " | + | System.out.println("Prioridad del hilo Main : " + Thread.currentThread().getPriority()); |
| − | |||
} | } | ||
} | } | ||
Revisión del 16:58 2 nov 2021
Enunciado 1
Responde as preguntas sobre o seguinte código:
1 class DemoPrioridadGet extends Thread 2 { 3 public void run() 4 { 5 System.out.println("Dentro del método run"); 6 } 7 8 public static void main(String[]args) 9 { 10 DemoPrioridadGet t1 = new DemoPrioridadGet(); 11 DemoPrioridadGet t2 = new DemoPrioridadGet(); 12 DemoPrioridadGet t3 = new DemoPrioridadGet(); 13 14 System.out.println("Prioridad del hilo t1 : " + t1.getPriority()); // Por defecto 5 15 System.out.println("Prioridad del hilo t2 : " + t2.getPriority()); // Por defecto 5 16 System.out.println("Prioridad del hilo t3 : " + t3.getPriority()); // Por defecto 5 17 18 t1.setPriority(2); 19 t2.setPriority(5); 20 t3.setPriority(8); 21 22 // t3.setPriority(11); arrojará IllegalArgumentException 23 System.out.println("Prioridad del hilo t1 : " + t1.getPriority()); //2 24 System.out.println("Prioridad del hilo t2 : " + t2.getPriority()); //5 25 System.out.println("Prioridad del hilo t3 : " + t3.getPriority());//8 26 27 // Hilo Principal (Main thread) 28 System.out.print(Thread.currentThread().getName()+": "); 29 System.out.println("Prioridad del hilo Main : " + Thread.currentThread().getPriority()); 30 31 // La prioridad del hilo principal se establece en 10 32 Thread.currentThread().setPriority(10); 33 System.out.println("Prioridad del hilo Main : " + Thread.currentThread().getPriority()); 34 } 35 }
a) entre os fíos t1, t2 e t3... cal cres que terá maior probabilidade de execución? b) cal é a prioridade por defecto do fío main? c) cal é a prioridade por defecto dos fíos que herdan de main? d) Se dous fios teñen a mesma prioridade, cal pensas que se executará primeiro? e) Se lle asignamos a un fío prioridade 11... qué acontece?