Created
June 12, 2015 05:59
-
-
Save ranisalt/47cd7a58de231715307e to your computer and use it in GitHub Desktop.
Problema dos Comedodes-Dadeiros
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
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.Semaphore; | |
public class Main { | |
private Semaphore empty, full, readers, writers; | |
private List<Double> buffer; | |
private List<Thread> threads; | |
public class Reader implements Runnable { | |
private int id; | |
public Reader(int id) { | |
this.id = id; | |
System.out.println("Leitor " + id + " criado."); | |
} | |
@Override | |
public void run() { | |
for (int i = 0; i < 10; ++i) { | |
try { | |
Thread.sleep((int) (Math.random() * 100)); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
try { | |
full.acquire(); | |
readers.acquire(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Leitor " + id + " começa a escrever no buffer..."); | |
//buffer.add(Math.random()); | |
try { | |
Thread.sleep((int) (Math.random() * 100)); | |
} catch (InterruptedException e) { | |
} | |
System.out.println("Leitor " + id + " termina de escrever no buffer..."); | |
readers.release(); | |
empty.release(); | |
} | |
} | |
} | |
public class Writer implements Runnable { | |
private int id; | |
public Writer(int id) { | |
this.id = id; | |
System.out.println("Escritor " + id + " criado."); | |
} | |
@Override | |
public void run() { | |
for (int i = 0; i < 10; ++i) { | |
try { | |
Thread.sleep((int) (Math.random() * 100)); | |
} catch (InterruptedException e) { | |
} | |
try { | |
empty.acquire(); | |
writers.acquire(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Escritor " + id + " começa a ler do buffer..."); | |
try { | |
Thread.sleep((int) (Math.random() * 100)); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Escritor " + id + " termina de ler do buffer..."); | |
writers.release(); | |
full.release(); | |
} | |
} | |
} | |
public Main() { | |
int num_leitores = (int) (Math.random() * 30) + 5, | |
num_escritores = (int) (Math.random() * 30) + 5, | |
tam_buffer = 10; | |
empty = new Semaphore(tam_buffer); | |
full = new Semaphore(0); | |
readers = new Semaphore(1); | |
writers = new Semaphore(1); | |
threads = new ArrayList<>(num_escritores + num_leitores); | |
for (int i = 0; i < num_leitores; ++i) { | |
Reader r = new Reader(i); | |
threads.add(new Thread(r)); | |
} | |
for (int i = 0; i < num_escritores; ++i) { | |
Writer w = new Writer(i); | |
threads.add(new Thread(w)); | |
} | |
for (Thread t : threads) { | |
t.start(); | |
} | |
for (Thread t : threads) { | |
try { | |
t.join(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
Main m = new Main(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment