public class Recurso { //classe dos monitores cesto e cabine private int nr; public Recurso ( int nr ) { this.nr = nr; } public synchronized void Pega() { while ( nr == 0 ) { try { wait(); } catch (InterruptedException e ) { System.err.println( e ); } } nr--; } public synchronized void Larga() { nr++; notify(); } } public class Nadador extends Thread{ Recurso cesto; Recurso cabine; int nid; public Nadador( int nid, Recurso cesto, Recurso cabine) { this.nid = nid; this.cesto = cesto; this.cabine = cabine; } public void run() { while ( true ) { try { System.out.println(nid+" CAMINHANDO PARA PISCINA "); sleep ((long ) (Math.random() * 16000)); cesto.Pega(); cabine.Pega(); System.out.println(nid+" TIRANDO A ROUPA "); sleep ((long ) (Math.random() * 14000)); cabine.Larga(); System.out.println(nid+" NADANDO "); sleep ((long ) (Math.random() * 20000)); cabine.Pega(); System.out.println(nid+" COLOCANDO A ROUPA "); sleep ((long ) (Math.random() * 16000)); } catch ( InterruptedException e ) { System.err.println( e ); } cabine.Larga(); cesto.Larga(); } } } public class Main { public static void main (String args[] ) { Nadador nadador[]; Recurso cesto; Recurso cabine; cesto = new Recurso ( 15 ); cabine = new Recurso ( 5 ); nadador = new Nadador[25]; for (int i = 0; i < 25 ; i++ ) { nadador[i] = new Nadador ( i , cesto, cabine ); nadador[i].start(); } } }