A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Aula prática de Concorrência Equipe de Monitoria: Bruno Pereira - bpe Davi Pires - dpr Guilherme Barros – gbs2 Thiago Cavalcanti - trc.

Apresentações semelhantes


Apresentação em tema: "Aula prática de Concorrência Equipe de Monitoria: Bruno Pereira - bpe Davi Pires - dpr Guilherme Barros – gbs2 Thiago Cavalcanti - trc."— Transcrição da apresentação:

1 Aula prática de Concorrência Equipe de Monitoria: Bruno Pereira - bpe Davi Pires - dpr Guilherme Barros – gbs2 Thiago Cavalcanti - trc

2 Roteiro Motivação Concorrência em Java Como podemos criar threads em Java Formas de sincronização entre threads Conclusão Exercícios

3 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)

4 Concorrência em JAVA Garbage collection Interface Gráfica Objetivo Mostrar como criar e controlar threads em uma aplicação Java

5 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.Thread Principal: Contem o método main que cria o buffer e as threads que irão acrescentar caracteres ao buffer

6 ThreadHeranca public 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) { } } } }

7 Principal public 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()); }

8 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.ThreadInterface

9 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++); }

10 Principal2 public 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()); }

11 Sincronização Semáforos Monitor (synchronized) Formas de sincronização Competição – tentam utilizar o mesmo recurso compartilhado Cooperação – a comunicação entre as threads é necessária

12 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

13 Buffer.java public 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); }

14 ThreadHeranca2.java public 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); }

15 Principal3.java public 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");...

16 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(); }

17 Um teste simples Retirem a palavra-chave synchronized do método add da classe Buffer E executem! E ai? Alguma esquisitice? Use sabiamente essa palavra!

18 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

19 Buffer2.java public class Buffer2{ private char[] buf; private int tam; private final int MAX = 100; public Buffer2(){ buf = new char[MAX]; tam = 0; }

20 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); }

21 ThreadHeranca3.java

22 public 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) { }

23 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()); }

24 Ultimas considerações wait() notify() E as 2 pilhas de JAVA

25 Ciclo de vida de uma thread Nascimento Pronto Executando Esperando Adormecido Morto Bloqueado start Inicia E/S Conclusão de E/S Tempo para adormecer expira notify ou notifyAll alocar um processo wait sleep completo

26 Aula prática de Concorrência Equipe de Monitoria: Bruno Pereira - bpe Davi Pires - dpr Guilherme Barros – gbs2 Thiago Cavalcanti - trc


Carregar ppt "Aula prática de Concorrência Equipe de Monitoria: Bruno Pereira - bpe Davi Pires - dpr Guilherme Barros – gbs2 Thiago Cavalcanti - trc."

Apresentações semelhantes


Anúncios Google