Professional Documents
Culture Documents
LinkedBlockingQueue <E>
import java.util.concurrent.*;
public class Productor implements Runnable {
LinkedBlockingQueue<Integer> data;
Thread hilo;
public Productor(LinkedBlockingQueue<Integer> l) {
this.data = l;
Lanzando thread en el constructor
hilo = new Thread (this);
hilo.start();
}
public void run() {
try {
for(int x=0;;x++) {
data.put(new Integer(x)); //bloqueo si no hay hueco
System.out.println("Insertando "+x);
}
import java.util.concurrent.*;
}
public class Consumidor implements Runnable {
catch(InterruptedException e){}
LinkedBlockingQueue<Integer> data;
}
Thread hilo;
}
public Consumidor(LinkedBlockingQueue<Integer> l) {
this.data = l;
hilo = new Thread (this);
hilo.start();
Ejemplo de
productor/consumidor
usando
LinkedBlockkingQueue
}
public void run() {
try {
for(;;) //bloquea a hilo consumidor si no hay datos
System.out.println("Extrayendo "+data.take().intValue());
}
catch(InterruptedException e){}
}
}
SynchronousQueue<E>
import java.util.*;
import java.util.concurrent.*;
class Consumer implements Runnable{
class Producer implements Runnable{
private BlockingQueue<String> drop;
private BlockingQueue<String> drop;
List<String> messages = Arrays.asList(
Mensaje 1",
Mensaje 2",
"Mensaje 3",
"Mensaje 4");
public Producer(BlockingQueue<String> d) {
this.drop = d;
}
public void run() {
try
{
for (String s : messages)
drop.put(s);
drop.put("DONE");
}
catch (InterruptedException intEx)
{
System.out.println("Interrupted! " +
"Last one out, turn out the lights!");
}
}
}
public Consumer(BlockingQueue<String> d) {
this.drop = d;
}
public void run() {
try
{
String msg = null;
while (!((msg = drop.take()).equals("DONE")))
System.out.println(msg);
}
catch (InterruptedException intEx)
{
System.out.println("Interrupted! " +
"Last one out, turn out the lights!");
}
}
}
public class SynQApp{
public static void main(String[] args) {
BlockingQueue<String> drop =
new SynchronousQueue<String>();
(new Thread(new Producer(drop))).start();
(new Thread(new Consumer(drop))).start();
}
}
SynchronousQueue<E>
Exchanger<V>
class FillAndEmpty {
Exchanger<DataBuffer> exchanger = new Exchanger<DataBuffer>();
DataBuffer initialEmptyBuffer = ... a made-up type
DataBuffer initialFullBuffer = ...
class FillingLoop implements Runnable {
public void run() {
DataBuffer currentBuffer = initialEmptyBuffer;
try {
while (currentBuffer != null) {
addToBuffer(currentBuffer);
if (currentBuffer.isFull())
currentBuffer = exchanger.exchange(currentBuffer);
}
} catch (InterruptedException ex)
class EmptyingLoop implements Runnable {
{ ... handle ... }
public void run() {
}
DataBuffer currentBuffer = initialFullBuffer;
}
try {
while (currentBuffer != null) {
takeFromBuffer(currentBuffer);
if (currentBuffer.isEmpty())
currentBuffer = exchanger.exchange(currentBuffer);
}
} catch (InterruptedException ex) { ... handle ...}
}
}
void start() {
new Thread(new FillingLoop()).start();
new Thread(new EmptyingLoop()).start();
}
}
Exchanger<V>
Constructors
Constructor and Description
Exchanger()Creates a new Exchanger.
Methods
Modifier and Type
V
Ejemplo:
Tengo un thread maestro y varios esclavos. Los esclavos no pueden empezar hasta
que el maestro lo diga. Por otra parte, el maestro tiene que esperar a que todos los
esclavos terminen
}
}
java.util.concurrent.CyclicBarrier
public class CyclicBarrier
A synchronization aid that allows a set of threads to all wait for each other to reach
a common barrier point.
CyclicBarriers are useful in programs involving a fixed sized party of threads that
must occasionally wait for each other.
The barrier is called cyclic because it can be re-used after the waiting threads are
released.
A CyclicBarrier supports an optional Runnable command that is run once per barrier
point, after the last thread in the party arrives, but before any threads are released.
This barrier action is useful for updating shared-state before any of the parties
continue.
Sample usage: Here is an example of using a barrier in a parallel decomposition
design:
CyclicBarrier
class Solver {
final int N;
final float[][] data;
final CyclicBarrier barrier;
class Worker implements Runnable {
int myRow;
Worker(int row) { myRow = row; }
public void run() {
while (!done()) {
processRow(myRow);
try {
barrier.await();
} catch (InterruptedException ex) {
return;
} catch (BrokenBarrierException ex) {
return;
}
}
}
}
CyclicBarrier
Constructors
Constructor and Description
CyclicBarrier(int parties) Creates a new CyclicBarrier that will trip when the given number of parties
(threads) are waiting upon it, and does not perform a predefined action when the barrier is tripped.
CyclicBarrier(int parties, Runnable barrierAction)Creates a new CyclicBarrier that will trip when the
given number of parties (threads) are waiting upon it, and which will execute the given barrier action
when the barrier is tripped, performed by the last thread entering the barrier.
Methods
Modifier and Type
int
int
int
int
boolean
void