Last active
August 29, 2015 14:02
-
-
Save steppat/3099a89609bf6fdd3ce4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.caelum.threads; | |
public class Banheiro { | |
private boolean sujo = true; | |
public void fazNumero1() { | |
String nomeAluno = Thread.currentThread().getName(); | |
System.out.println(nomeAluno + " tentando entrar no banheiro"); | |
synchronized (this) { | |
while(sujo) { | |
try { | |
System.out.println(nomeAluno + " tá sujo, vou esperar ..."); | |
this.wait(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
System.out.println(nomeAluno + " - entra no banheiro"); | |
System.out.println(nomeAluno + " - coisa rapida"); | |
System.out.println(nomeAluno + " - da descarga"); | |
System.out.println(nomeAluno + " - lavando a mao"); | |
System.out.println(nomeAluno + " - sai do banheiro"); | |
this.sujo = true; | |
} | |
} | |
public void fazNumero2() { | |
String nomeAluno = Thread.currentThread().getName(); | |
System.out.println(nomeAluno + " tentando entrar no banheiro"); | |
synchronized (this) { | |
while(sujo) { | |
try { | |
System.out.println(nomeAluno + " tá sujo, vou esperar ..."); | |
this.wait(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
System.out.println(nomeAluno + " - entra no banheiro"); | |
System.out.println(nomeAluno + " - coisa demorada"); | |
try { | |
Thread.sleep(10000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(nomeAluno + " - da descarga"); | |
System.out.println(nomeAluno + " - limpando-se"); | |
System.out.println(nomeAluno + " - lavando a mao"); | |
System.out.println(nomeAluno + " - sai do banheiro"); | |
this.sujo = true; | |
} | |
} | |
public void limpa() { | |
String faxineira = Thread.currentThread().getName(); | |
System.out.println(faxineira + " - tentando entrar no banheiro"); | |
synchronized (this) { | |
System.out.println(faxineira + " - limpando ..."); | |
this.sujo = false; | |
System.out.println(faxineira + " - limpou, notificando!!"); | |
this.notifyAll(); | |
} | |
} | |
} | |
--------------- | |
package br.com.caelum.threads; | |
public class TarefaLimpeza implements Runnable { | |
private Banheiro banheiro; | |
public TarefaLimpeza(Banheiro banheiro) { | |
this.banheiro = banheiro; | |
} | |
@Override | |
public void run() { | |
while(true) { | |
try { | |
Thread.sleep(10 * 1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
banheiro.limpa(); | |
} | |
} | |
} | |
--------------- | |
package br.com.caelum.threads; | |
public class TarefaNumero1 implements Runnable { | |
private Banheiro banheiro; | |
public TarefaNumero1(Banheiro banheiro) { | |
this.banheiro = banheiro; | |
} | |
@Override | |
public void run() { | |
banheiro.fazNumero1(); | |
} | |
} | |
--------------- | |
package br.com.caelum.threads; | |
public class TarefaNumero2 implements Runnable { | |
private Banheiro banheiro; | |
public TarefaNumero2(Banheiro banheiro) { | |
this.banheiro = banheiro; | |
} | |
@Override | |
public void run() { | |
banheiro.fazNumero2(); | |
} | |
} | |
--------------- | |
package br.com.caelum.threads; | |
public class ProgramaPrinicpal { | |
public static void main(String[] args) throws InterruptedException { | |
Thread.sleep(3000); | |
Banheiro banheiro = new Banheiro(); | |
TarefaNumero2 t2 = new TarefaNumero2(banheiro); | |
Thread aluno = new Thread(t2, "Paulo"); | |
aluno.setPriority(Thread.MAX_PRIORITY); | |
aluno.start(); | |
TarefaNumero1 t1 = new TarefaNumero1(banheiro); | |
Thread aluna = new Thread(t1, "Maria"); | |
aluna.start(); | |
TarefaLimpeza limpeza = new TarefaLimpeza(banheiro); | |
Thread faxineira = new Thread(limpeza, "Dona Marta"); | |
faxineira.setDaemon(true); | |
faxineira.start(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment