Created
September 19, 2018 01:18
-
-
Save rodrigovilar/fb67b220302ffe5af323388bb45f46e4 to your computer and use it in GitHub Desktop.
Aula de Threads
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
public class AppBanco { | |
public static void main(String[] args) { | |
ContaBancaria a = new ContaBancaria("a", 1000); | |
ContaBancaria b = new ContaBancaria("b", 500); | |
LogicaBanco logica = new LogicaBanco(); | |
Thread transferir = new Thread(new Transferencia(logica, a, b, 700)); | |
Thread sacar = new Thread(new Saque(logica, a, 500)); | |
Thread depositar = new Thread(new Deposito(logica, a, 200)); | |
transferir.start(); | |
sacar.start(); | |
depositar.start(); | |
} | |
} |
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
public class ContaBancaria { | |
private String id; | |
private double saldo; | |
public ContaBancaria(String id, double saldo) { | |
this.id = id; | |
this.saldo = saldo; | |
} | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public double getSaldo() { | |
return saldo; | |
} | |
public void setSaldo(double saldo) { | |
this.saldo = saldo; | |
} | |
@Override | |
public String toString() { | |
String res = "ContaBancaria [id=" + id + ", saldo=" + saldo + "]"; | |
if (saldo < 0) { | |
System.err.println("Conta com saldo negativo :" + res); | |
} | |
return res; | |
} | |
} |
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
public class Contador extends Thread { | |
private String name; | |
public Contador(String name) { | |
this.name = name; | |
} | |
@Override | |
public void run() { | |
for (int i = 0; i < 1000; i++) { | |
System.out.println(name + ": " + i); | |
} | |
} | |
} |
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
public class ContadorInvertido implements Runnable { | |
@Override | |
public void run() { | |
for (int i = 1000; i >= 0; i--) { | |
System.err.println(i); | |
} | |
} | |
} |
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
public class Deposito implements Runnable { | |
private ContaBancaria destino; | |
private double valor; | |
private LogicaBanco logica; | |
public Deposito(LogicaBanco logica, ContaBancaria destino, double valor) { | |
this.logica = logica; | |
this.destino = destino; | |
this.valor = valor; | |
} | |
@Override | |
public void run() { | |
for (int i = 0; i < 100; i++) { | |
logica.depositar(destino, valor); | |
} | |
} | |
} |
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
public class LogicaBanco { | |
public synchronized void transferir(ContaBancaria origem, ContaBancaria destino, double valor) { | |
if (origem.getSaldo() >= valor) { | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
origem.setSaldo(origem.getSaldo() - valor); | |
destino.setSaldo(destino.getSaldo() + valor); | |
System.out.println("Transferiu " + valor + " da conta " + origem + " para " + destino); | |
} else { | |
System.out.println("Tentou transferir " + valor + " da conta " + origem); | |
} | |
} | |
public synchronized void sacar(ContaBancaria origem, double valor) { | |
if (origem.getSaldo() >= valor) { | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
origem.setSaldo(origem.getSaldo() - valor); | |
System.out.println("Sacou " + valor + " da conta " + origem); | |
} else { | |
System.out.println("Tentou sacar " + valor + " da conta " + origem); | |
} | |
} | |
public synchronized void depositar(ContaBancaria destino, double valor) { | |
destino.setSaldo(destino.getSaldo() + valor); | |
System.out.println("Despositou " + valor + " na conta " + destino); | |
} | |
} |
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
public class Main { | |
public static void main(String[] args) throws InterruptedException { | |
Contador c1 = new Contador("T1"); | |
Contador c2 = new Contador("T2"); | |
Thread t3 = new Thread(new ContadorInvertido()); | |
c1.start(); | |
c2.start(); | |
t3.start(); | |
} | |
} |
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
public class Saque implements Runnable { | |
private ContaBancaria origem; | |
private double valor; | |
private LogicaBanco logica; | |
public Saque(LogicaBanco logica, ContaBancaria origem, double valor) { | |
this.logica = logica; | |
this.origem = origem; | |
this.valor = valor; | |
} | |
@Override | |
public void run() { | |
for (int i = 0; i < 100; i++) { | |
logica.sacar(origem, valor); | |
} | |
} | |
} |
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
public class Transferencia implements Runnable { | |
private ContaBancaria origem; | |
private ContaBancaria destino; | |
private double valor; | |
private LogicaBanco logica; | |
public Transferencia(LogicaBanco logica, ContaBancaria origem, ContaBancaria destino, double valor) { | |
this.logica = logica; | |
this.origem = origem; | |
this.destino = destino; | |
this.valor = valor; | |
} | |
@Override | |
public void run() { | |
for (int i = 0; i < 100; i++) { | |
logica.transferir(origem, destino, valor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment