Strumenti Utente

Strumenti Sito


lpr-b-2007-2008:threadtermina
package threadPoolConTerminazione;
 
public class ComputerThread<T,S> extends Thread {
 
	Repository<T> tasks = null; 
	Repository<S> results = null; 
	Compute<T,S> function = null; 
 
	public ComputerThread(Repository<T> tasks, Repository<S> results, Compute<T,S> function) {
		this.tasks = tasks; 
		this.results = results; 
		this.function = function; 
		return;
	}
 
	public void run() {
		while(true) {
			T task = null; 
			try {
				task = tasks.extract();
			} catch (EndOfStreamException e) {
				System.out.println("Thread "+this.getName()+" ricevuto EoS");
				return; // fine lavori
			} catch (InterruptedException e) {
				System.out.println("Estrazione task interrotta");			
			}
			try {
				int random =  ((int)(Math.random() * 5000.00));
				System.out.println("Thread "+this.getName()+" inizio calcolo");
				sleep(random);
				S result = function.compute(task); 
				System.out.println("Thread "+this.getName()+" fine calcolo");
				results.insert(result);
			} catch(InterruptedException e) { System.out.println("Calcolo del thread "+this.getName()+" interrotto!");}
 
			System.out.println("Thread "+this.getName()+" computed task "+task);
 
		}
	}
 
}
lpr-b-2007-2008/threadtermina.txt · Ultima modifica: 19/09/2008 alle 14:08 (16 anni fa) (modifica esterna)