12.07.2015 Views

k19-k11-orientacao-a.. - Departamento de Sistemas e Computação

k19-k11-orientacao-a.. - Departamento de Sistemas e Computação

k19-k11-orientacao-a.. - Departamento de Sistemas e Computação

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

THREADS 174InterruptedExceptionUma thread que está “dormindo” po<strong>de</strong> ser interrompida por outra thread. Quando isso ocorrer,a thread que está “dormindo” recebe uma InterruptedException.1 try {2 Thread . sleep (3000) ;3 } catch ( InterruptedException e) {45 }Código Java C.8: Sleepjoin()Uma thread po<strong>de</strong> “pedir” para esperar o término <strong>de</strong> outra thread para continuar a execução atravésdo método join(). Esse método também po<strong>de</strong> lançar uma InterruptedException.1 TarefaImprimeMensagens tarefa = new TarefaImprimeMensagens (" K19 ");2 Thread thread = new Thread ( tarefa );3 thread . start ();45 try {6 thread . join ();7 } catch ( InterruptedException e) {89 }Código Java C.9: JoinExercícios <strong>de</strong> Fixação4 Altere a classe TarefaImprimeMensagens do projeto Threads, adicionando uma chamada aométodo sleep().1 class TarefaImprimeMensagens implements Runnable {23 private String msg ;45 public TarefaImprimeMensagens ( String msg ) {6 this . msg = msg ;7 }89 public void run () {10 for ( int i = 0; i < 100000; i ++) {11 System . out . println (i + " : " + this . msg );1213 if(i % 1000 == 0) {14 try {15 Thread . sleep (100) ;16 } catch ( InterruptedException e) {1718 }19 }20 }21 }22 }174 www.<strong>k19</strong>.com.br

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!