Aula prática de Concorrência
description
Transcript of Aula prática de Concorrência
![Page 1: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/1.jpg)
Aula prática de Concorrência
Equipe de Monitoria:Bruno Pereira - bpe
Davi Pires - dprGuilherme Barros – gbs2
Thiago Cavalcanti - trc
![Page 2: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/2.jpg)
Roteiro
MotivaçãoConcorrência em JavaComo podemos criar threads em JavaFormas de sincronização entre threadsConclusão Exercícios
![Page 3: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/3.jpg)
Motivação
Concorrência: Executa tarefas de controle de forma concorrente, onde diferentes partes do software estão rodando em paralelo. O paralelismo
pode ser real (quando existem vários processadores) ou simulado (por
sistemas operacionais tempo real nos quais as tarefas são escalonadas para
compartilhar o processador)
![Page 4: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/4.jpg)
Concorrência em JAVA
Garbage collectionInterface GráficaObjetivo
Mostrar como criar e controlar threads em uma aplicação Java
![Page 5: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/5.jpg)
Como podemos criar threads
Iremos mostrar como criar threads simples que imprimem mensagens em um buffer das duas formas. O primeiro exemplo consiste de duas classes que são descritas a seguir:
ThreadHeranca: Classe que representa uma thread criada a partir da herança da classe java.lang.ThreadPrincipal: Contem o método main que cria o buffer e as threads que irão acrescentar caracteres ao buffer
![Page 6: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/6.jpg)
ThreadHerancapublic class ThreadHeranca extends
java.lang.Thread{ private StringBuffer buf;private String texto; public ThreadHeranca(StringBuffer buf, String texto){
this.buf = buf; this.texto = texto; }
public void run(){ for(int i=0; i < 10; i++){
buf.append(texto); try {
sleep((long)(Math.random() * 100));
} catch (InterruptedException ie) { } } } }
![Page 7: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/7.jpg)
Principalpublic class Principal{ public static void main(String []args){
StringBuffer buf = new StringBuffer(); ThreadHeranca her1 = new ThreadHeranca(buf, "thread 1\n"); ThreadHeranca her2 = new ThreadHeranca(buf, "thread 2\n"); ThreadHeranca her3 = new ThreadHeranca(buf, "thread 3\n"); her1.start(); her2.start(); her3.start(); for(int i=0; i < Integer.MAX_VALUE/10; i++); System.out.println(buf.toString());
} }
![Page 8: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/8.jpg)
Como podemos criar threads
Bem falta agora mostrar a segunda forma de se criar uma thread em Java: através da interface java.lang.Runnable. Para isso basta fazermos pouquíssimas modificações no exemplo anterior. A classe ThreadInterface substitui a classe ThreadHeranca.
![Page 9: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/9.jpg)
ThreadInterface public class ThreadInterface implements Runnable{ private StringBuffer buf; private String texto; public ThreadInterface(StringBuffer buf, String texto){ this.buf = buf; this.texto = texto; } public void run(){ for(int i=0; i < 20; i++) { buf.append(texto); for(int j=0; j < Integer.MAX_VALUE/600; j++); } } }
![Page 10: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/10.jpg)
Principal2public class Principal2{ public static void main(String []args){
StringBuffer buf = new StringBuffer();Thread inter1 = new Thread(new ThreadInterface(buf, "thread 1\n"));Thread inter2 = new Thread(new ThreadInterface(buf, "thread 2\n"));Thread inter3 = new Thread(new Threadinterface(buf, "thread 3\n"));
inter1.start(); inter2.start();
inter3.start();
for(int i=0; i < Integer.MAX_VALUE/10; i++); System.out.println(buf.toString());
}}
![Page 11: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/11.jpg)
Sincronização
SemáforosMonitor (synchronized)Formas de sincronização
Competição – tentam utilizar o mesmo recurso compartilhadoCooperação – a comunicação entre as threads é necessária
![Page 12: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/12.jpg)
Sincronização por competição
Iremos mostrar agora como sincronizar várias threads que competem por um recurso compartilhado. A idéia é parecida com o exemplo anterior aonde várias threads adicionam um String num buffer compartilhado. Ao invés de usarmos um objeto StringBuffer como buffer, vamos criar o nosso próprio buffer através da classe Buffer.java que está descrita abaixo
![Page 13: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/13.jpg)
Buffer.javapublic class Buffer{ private StringBuffer buf; public Buffer(){ buf = new StringBuffer(); } public synchronized void add(String novo){ for(int i=0; i < novo.length(); i++) buf.append(novo.charAt(i)); } public synchronized void print(){ System.out.println(buf);}
![Page 14: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/14.jpg)
ThreadHeranca2.javapublic class ThreadHeranca2 extends Thread{ private Buffer buf; private String texto; public ThreadHeranca2(Buffer buf, String texto){ this.buf = buf; this.texto = texto; } public void run(){ for(int i=0; i < 4000; i++) buf.add(texto); } }
![Page 15: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/15.jpg)
Principal3.javapublic class Principal3{ public static void main(String []args){ Buffer buf = new Buffer(); ThreadHeranca2 her1 = new ThreadHeranca2(buf, "thread 1\n"); ThreadHeranca2 her2 = new ThreadHeranca2(buf, "thread 2\n"); ThreadHeranca2 her3 = new ThreadHeranca2(buf, "thread 3\n"); ThreadHeranca2 her4 = new ThreadHeranca2(buf, "thread 4\n"); ThreadHeranca2 her5 = new ThreadHeranca2(buf, "thread 5\n"); ThreadHeranca2 her6 = new ThreadHeranca2(buf, "thread 6\n");
...
![Page 16: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/16.jpg)
Principal3.java her1.start(); her2.start(); her3.start(); her4.start(); her5.start(); her6.start();
for(int i=0; i < Integer.MAX_VALUE/10; i++); buf.print();
} }
![Page 17: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/17.jpg)
Um teste simples
Retirem a palavra-chave synchronized do método add da classe BufferE executem!E ai? Alguma esquisitice? Use sabiamente essa palavra!
![Page 18: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/18.jpg)
Sincronização por cooperação
Imagine que a thread principal da aplicação (aquela que contém o método main) vai imprimir o conteúdo de um buffer, contudo ela apenas deve imprimir quando o buffer estiver cheio. Outras threads ficarão concorrentemente adicionando caracteres ao buffer até preenchê-lo. Primeiro vamos ver a classe Buffer2 que implementa o buffer que vamos utilizar
![Page 19: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/19.jpg)
Buffer2.java
public class Buffer2{ private char[] buf; private int tam; private final int MAX = 100;
public Buffer2(){ buf = new char[MAX];
tam = 0; }
![Page 20: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/20.jpg)
Buffer2.java public synchronized void inserir(char c){
if(tam==MAX) notify(); else buf[tam++] = c;
} public synchronized String esvaziar(){
if(tam < MAX) try{ wait(); }catch(InterruptedException ie){ } tam = 0; return new String(buf);
} }
![Page 21: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/21.jpg)
ThreadHeranca3.java
![Page 22: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/22.jpg)
ThreadHeranca3.javapublic class ThreadHeranca3 extends Thread{ private Buffer2 buf; private char c; public ThreadHeranca3(Buffer2 buf, char c){ this.buf = buf; this.c = c; } public void run(){
for(int i=0; i < 1000; i++){ buf.inserir(c);
try { sleep((long)5); } catch (InterruptedException ie) { }
} }
![Page 23: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/23.jpg)
Principal4.java public class Principal4{ public static void main(String []args){ Buffer2 buf = new Buffer2(); ThreadHeranca3 her1 = new ThreadHeranca3(buf,'1'); ThreadHeranca3 her2 = new ThreadHeranca3(buf,'2'); ThreadHeranca3 her3 = new ThreadHeranca3(buf,'3'); her1.start(); her2.start(); her3.start(); String result; for(int i=0; i < 10; i++){ result = buf.esvaziar(); System.out.println("\n\nImpressao # " + (i+1) + ":\n Buffer = " + result + " -> Tamanho: " + result.length()); } }}
![Page 24: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/24.jpg)
Ultimas considerações
wait() notify() E as 2 pilhas de JAVA
![Page 25: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/25.jpg)
Ciclo de vida de uma threadNascimento
Pronto
Executando
Esperando
Adormecido Morto Bloqueado
start
Inicia E/S
Conclusão de E/S
Tempo paraadormecer expira
notify ounotifyAll alocar um
processo
wait
sleep completo
![Page 26: Aula prática de Concorrência](https://reader036.fdocumentos.tips/reader036/viewer/2022081514/56815985550346895dc6c501/html5/thumbnails/26.jpg)
Aula prática de Concorrência
Equipe de Monitoria:Bruno Pereira - bpe
Davi Pires - dprGuilherme Barros – gbs2
Thiago Cavalcanti - trc