Java threads (3) Gruppi di thread e Timer emanuele lattanzi isti information science and technology institute 1/12 Gruppi di Thread emanuele lattanzi isti information science and technology institute 2/12 1
Gruppi di thread I thread possono essere raggruppati in gruppi di thread Si possono creare gerarchie di gruppi di thread Un gruppo di thread puo essere usato per: tenere traccia del numero di thread attivi Rimuovere threads in blocco Cambiare la priorita di threads in blocco Interrompere l intero gruppo di threads emanuele lattanzi isti information science and technology institute 3/12 ThreadGroup (1) I gruppi di thread sono implementati dalla classe java.lang.threadgroup. Quando un thread viene creato e possibile selezionare esplicitamente il gruppo in cui inserirlo altrimenti la JVM lo inserirà in un gruppo di default. Il thread è un membro permanente del gruppo in cui è inserito all atto della creazione; non è quindi possibile spostare un thread in un nuovo gruppo dopo la sua creazione. emanuele lattanzi isti information science and technology institute 4/12 2
ThreadGroup (2) Costruttori della classe Thread per definire il gruppo: public Thread(ThreadGroup group, Runnable runnable) public Thread(ThreadGroup group, String name) public Thread(ThreadGroup group, Runnable runnable, String name) E possibile creare un proprio gruppo di thread: ThreadGroup mythreadgroup = new ThreadGroup("My Group"); Thread mythread = new Thread(myThreadGroup,"a thread"); emanuele lattanzi isti information science and technology institute 5/12 Metodi della classe ThreadGroup Metodi di gestione Metodi che gestiscono il ThreadGroup Metodi che operano sul ThreadGroup set o get degli attributi del ThreadGroup Metodi che operano su tutti I thread all interno del gruppo Metodi che compiono delle azioni come interrupt() o resume(), su tutti I thread emanuele lattanzi isti information science and technology institute 6/12 3
Esempio public class TestGroup { public static void main(string[] args) { ThreadGroup gruppo1 = new ThreadGroup("Gruppo uno"); ThreadExample1 thread1, thread2, thread3; thread1 = new ThreadExample1 ( gruppo1, "thread1", "Emanuele Lattanzi"); thread2 = new ThreadExample1 ( gruppo1, "thread2", "Francesco Morelli"); thread3 = new ThreadExample1 ( gruppo1, "thread3", "Giorgia Cavallanti"); thread1.start(); thread2.start(); thread3.start(); int numthreads = gruppo1.activecount(); System.out.println("I thread attivi sono "+numthreads); System.out.println("Fanno parte del gruppo: "+gruppo1); Thread[] listofthreads = new Thread[numThreads];; gruppo1.enumerate(listofthreads); for (int i = 0; i < numthreads; i++) { System.out.println("Thread #" + i + " = " + listofthreads[i].getname()); emanuele lattanzi isti information science and technology institute 7/12 Methods that Operate on the Group The methods that get and set ThreadGroup attributes operate at the group level. They inspect or change the attribute on the ThreadGroup object, but do not affect any of the threads within the group. The following is a list of ThreadGroup methods that operate at the group level: getmaxpriority and setmaxpriority getdaemon and setdaemon getname getparent and parentof tostring emanuele lattanzi isti information science and technology institute 8/12 4
Timer emanuele lattanzi isti information science and technology institute 9/12 La classe Timer Fornisce un meccanismo utilizzabile dai thread per schedulare un task da essere eseguito in futuro in un thread a parte Il task può essere schedulato per essere eseguito una sola volta oppure in modo periodico ad intervalli regolari Ad ogni Timer corrisponde un thread eseguito in background emanuele lattanzi isti information science and technology institute 10/12 5
La classe TimerTask E una clase astratta che contiene un metodo astratto run() E necessario definire una classe concreata che implementi il metodo run(). Tale classe deve essere passata ad un oggetto timer. L oggetto timer eseguirà il metodo run dell oggetto TimeTask al tempo desiderato. emanuele lattanzi isti information science and technology institute 11/12 public class TimerReminder { Timer timer; Esempio public TimerReminder(int seconds) { timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000); class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); timer.cancel(); //Terminate the timer thread public static void main(string args[]) { System.out.println("About to schedule task ); new TimerReminder(5); System.out.println("Task scheduled"); emanuele lattanzi isti information science and technology institute 12/12 6