Ho provato come avete suggerito voi, ma questa sleep si trova nel metodo run di un thread, e il metodo run non prevede clausole throws.
praticamente... sto facendo l'esercizio uscito all'esame ieri.
Devo realizzare un thread chiamato Interruptor che accetta un thread e il numero di secondi, e interrompa l'esecuzione del thread ricevuto come parametro dopo che sono passati tanti secondi quanto quelli passati come argomento. Il codice di questo thread è :
codice:
package threads;
public class Interruptor extends Thread {
private Thread t;
private int seconds;
public Interruptor(String name,Thread t,int seconds) {
super(name);
this.t=t;
this.seconds=seconds;
start();
}
public void run() {
try {
sleep(seconds*1000);
}
catch(InterruptedException e) {}
t.interrupt();
}
}
Sto provando questo codice con un altro thread che avevo creato come esempio e che possiede quella sleep(). Soltanto che siccome c'è il catch dell'eccezione, essa viene ingnorata dal mio thread di prova.
Non posso fare
codice:
public void run() throws InterruptedException
che da errore, e non posso neppure inserire il throw all'interno del catch.... come fare? Vorrei se possibile una metodo che funzioni sempre con qualsiasi thread che voglio terminare ma che utilizzi comunque interrupt()