// teste da funcionalidade de notify e notifyAll class Compartilhada { public synchronized void para() { try { this.wait(); } catch (InterruptedException ie) { System.out.println(ie); } } public synchronized void liberaUm() { this.notify(); } public synchronized void liberaTodos() { this.notifyAll(); } } class T1 extends Thread { private String nome; private Compartilhada c; T1(String _nome, Compartilhada _c){ nome = _nome; c = _c; }; public void run() { System.out.println(nome+" para"); c.para(); System.out.println(nome+" continua"); } } public class TesteThreadsNotify { public static void main(String []args) { T1 [] ts = new T1[10]; Compartilhada c = new Compartilhada(); for(int i=0; i<10; i++) { ts[i]=new T1("T"+i, c); // teste da influencia da prioridade ts[i].setPriority(i+1); // prioridade maior para threads criadas depois // qual o comportamento ? ts[i].start(); } try { Thread.sleep(3000); } catch(InterruptedException ie){ System.out.println(ie); } System.out.println("Vou notificar"); // trechos equivalentes ? // -------------- este: for(int i=0; i<10; i++) { c.liberaUm(); try{ Thread.sleep(300); } catch (InterruptedException ie){ } } // // ------------ e este: // c.liberaTodos(); } // fim main } // fim classe