Java: ProducerConsumer - java

I don't see any problem with this code, but the result doesn't display in the console window in eclipse.
What's the problem?
class Buffer{
private int data;
private boolean empty = true;
public synchronized int get() {
while(empty) {
try {
wait();
} catch (InterruptedException e) {
}
}
empty = true;
notifyAll();
return data;
}
public synchronized void put(int data) {
while (!empty) {
try {
wait();
} catch (InterruptedException e) {
}
}
empty = false;
this.data = data;
notifyAll();
}
}
class Producer implements Runnable{
private Buffer buffer;
public Producer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
for (int i=0; i<0; i++) {
buffer.put(i);
System.out.println("Producer: " + i + "th cake produced.");
try {
Thread.sleep((int) (Math.random() * 100));
} catch (InterruptedException e) {
}
}
}
}
class Consumer implements Runnable{
private Buffer buffer;
public Consumer(Buffer drop) {
this.buffer = drop;
}
public void run() {
for(int i=0; i<10; i++) {
int data = buffer.get();
System.out.println("Consumer: " + data + "th cake comsumed.");
try {
Thread.sleep((int) (Math.random() * 100));
} catch (InterruptedException e) {
}
}
}
}
public class ProducerConsumerTest {
public static void main(String[] args) {
Buffer buffer = new Buffer();
(new Thread(new Producer(buffer))).start();
(new Thread(new Consumer(buffer))).start();
}
}

In the run() method of the producer you have bad for loop
for (int i=0; i<0; i++)
but it should be
for (int i=0; i<10; i++)
That was probably a typo :)

Related

Print Floyd triangle using multithreading in java

I want to use two threads to print Floyd triangle(say one thread prints the number and the other prints the number in the line) as below.
and so forth until the max number which is 15 in this case.
I tried following but it keeps on printing numbers one on each line
public class MyThread extends Thread{
static volatile int lineNumber = 1;
public static void main(String... args) {
PrintFloyd print = new PrintFloyd();
Thread t1 = new Thread(new TaskHandler(print, 10), "T1");
Thread t2 = new Thread(new TaskHandler(print, 10), "T2");
t1.start();
t2.start();
}
}
class TaskHandler implements Runnable {
static volatile int i = 1;
static volatile int lineCount = 1;
static volatile int lineNumber = 1;
private int max;
private PrintFloyd print;
TaskHandler(PrintFloyd print2, int max) {
this.print = print2;
this.max = max;
}
#Override
public void run() {
System.out.println(">>>>" + Thread.currentThread().getName());
while(i < max){
if (Thread.currentThread().getName().equals("T1")){
print.printNumber(i);
} else {
print.breakLine();
}
}
}
}
class PrintFloyd {
boolean isBreakPoint = false;
public void printNumber(int i) {
synchronized(this){
while (isBreakPoint == false) {
try {
wait();
} catch (InterruptedException ex) {
}
System.out.print(i++ + " ");
isBreakPoint = false;
notifyAll();
}
}
}
public void breakLine(){
synchronized(this){
while (isBreakPoint == true) {
try {
wait();
} catch (InterruptedException ex) {
}
}
System.out.println();
isBreakPoint = true;
notifyAll();
}
}
}
The following code would help:
public class PrintPatternWith2Threads {
final static int MAX = 15;
final static String itemWriterName = "itemWriter";
final static String newLineWriterName = "newLineWriter";
public static void main(String[] args) {
Printer print = new Printer(MAX);
Thread itemWriter = new Thread(new ItemWriter(print), itemWriterName);
itemWriter.start();
Thread newLineWriter = new Thread(new NewLineWriter(print), newLineWriterName);
newLineWriter.start();
}
}
class ItemWriter implements Runnable {
private Printer print;
ItemWriter(Printer print) {
this.print = print;
}
public void run() {
while (print.current <= print.MAX) {
print.printNumber();
}
}
}
class NewLineWriter implements Runnable {
private Printer print;
NewLineWriter(Printer print) {
this.print = print;
}
public void run() {
while (print.current <= print.MAX) {
print.printNewLine();
}
}
}
class Printer {
public final int MAX;
public int current = 1;
public int itemsInALine = 1;
Printer(int max) {
this.MAX = max;
}
public void printNumber() {
synchronized(this) {
for(int i = current; i < current + itemsInALine && i <= MAX; i++) {
System.out.print(i + " ");
}
this.current = current + itemsInALine;
itemsInALine++;
notifyAll();
try {
if(this.current < MAX) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void printNewLine() {
synchronized(this) {
System.out.println();
notifyAll();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Confused about how to use exchanger in java

As with basically every exchanger task, I have a producer filling up an empty buffer2, a consumer clearing a full buffer1 and when each thread is done, they should exchange their respective buffers.
I am really unsure about where and how to apply the exchange. I defined readyconsumer and readyproducer as booleans, so that a third thread can check whether it's time to exchange the buffers once both are true. This should solve the problem I had doing it with two threads, where the program was stuck with both threads at wait() (which it unfortunately still is).
This is what the code looks like at the moment. Can anyone help me in which class I have to exchange and at what point in the code? Thank you very much in advance!
class Buffer {
static boolean readyconsumer, readyproducer = false;
volatile int count; // number of put actions
static int max = 10;
Buffer() {
count = 0;
}
public synchronized void put() {
if (count == max) {
readyproducer = true;
System.out.println(" wait ");
try {
wait();
} catch (InterruptedException e) {
}
}
count++;
System.out.println("put " + count);
notifyAll();
}
public synchronized void get() {
if (count == 0) {
readyconsumer = true;
System.out.println(" wait");
try {
wait();
} catch (InterruptedException e) {
}
}
count--;
System.out.println("get " + count);
notifyAll();
}
}
class CheckandSwitch extends ProdCon {
public void run() {
while (true) {
if (Buffer.readyconsumer && Buffer.readyproducer) {
try {
ProdCon.buffer2 = exchanger.exchange(ProdCon.buffer1);
ProdCon.buffer1 = exchanger.exchange(ProdCon.buffer2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Buffer.readyconsumer = false;
Buffer.readyproducer = false;
buffer1.count = 0;
buffer2.count = 10;
notifyAll();
}
}
}
}
class Consumer extends ProdCon {
static Buffer buffer;
Consumer(Buffer b) {
super();
buffer = b;
b.count = 10;
}
public void run() {
while (true) {
consume();
buffer.get();
}
}
private void consume() {
System.out.println("consume");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
}
class Producer extends ProdCon {
static Buffer buffer;
Producer(Buffer b) {
super();
buffer = b;
b.count = 0;
}
public void run() {
while (true) {
produce();
buffer.put();
}
}
private void produce() {
System.out.println("produce ");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
import java.util.concurrent.*;
public class ProdCon extends Thread {
static Exchanger<Buffer> exchanger = new Exchanger<Buffer>();
static Buffer buffer1, buffer2 = null;
public static void main(String[] args) {
buffer1 = new Buffer();
buffer2 = new Buffer();
new Consumer(buffer1).start();
new Producer(buffer2).start();
new CheckandSwitch().start();
}
}
You could use an Exchanger.
Here's the code from the javadoc tweaked into a working example.
class DataBuffer<T> {
T data = null;
public boolean isFull() {
return data != null;
}
public boolean isEmpty() {
return data == null;
}
public T get() {
T d = data;
data = null;
return d;
}
public void put(T data) {
this.data = data;
}
}
class FillAndEmpty {
Exchanger<DataBuffer<Integer>> exchanger = new Exchanger<>();
DataBuffer<Integer> initialEmptyBuffer = new DataBuffer<>();
DataBuffer<Integer> initialFullBuffer = new DataBuffer<>();
int countDown = 10;
class FillingLoop implements Runnable {
#Override
public void run() {
DataBuffer currentBuffer = initialEmptyBuffer;
try {
while (currentBuffer != null && countDown > 0) {
addToBuffer(currentBuffer);
if (currentBuffer.isFull()) {
currentBuffer = exchanger.exchange(currentBuffer);
}
}
} catch (InterruptedException ex) {
}
}
private void addToBuffer(DataBuffer<Integer> currentBuffer) {
currentBuffer.put(countDown--);
}
}
class EmptyingLoop implements Runnable {
#Override
public void run() {
DataBuffer<Integer> currentBuffer = initialFullBuffer;
try {
while (currentBuffer != null) {
takeFromBuffer(currentBuffer);
if (currentBuffer.isEmpty()) {
currentBuffer = exchanger.exchange(currentBuffer);
}
}
} catch (InterruptedException ex) {
}
}
private void takeFromBuffer(DataBuffer<Integer> currentBuffer) {
System.out.println(currentBuffer.get());
}
}
void start() {
new Thread(new FillingLoop()).start();
new Thread(new EmptyingLoop()).start();
}
}
public void test() {
System.out.println("Hello");
new FillAndEmpty().start();
}

Trying to solve consumer-producer in java with multithreading

I'm trying to solve the producer consumer problem with threads in java, but the code won't run in parallell/concurrently. The producer always fills up the buffer completely before the consumer starts to consume, and I don't get why. The point is trying to do it using only synchronized blocks, wait() and notify().
Main :
String [] data = {"Fisk", "Katt", "Hund", "Sau", "Fugl", "Elg", "Tiger",
"Kameleon", "Isbjørn", "Puma"};
ProducerConsumer pc = new ProducerConsumer(5);
Thread[] thrds = new Thread[2];
thrds[0] = new Thread(new MyThread1(pc, data)); // producer
thrds[1] = new Thread(new MyThread2(pc)); // consumer
thrds[0].start();
thrds[1].start();
for(int i = 0; i < 2; i++) { // wait for all threads to die
try {
thrds[i].join();
}
catch (InterruptedException ie) {}
}
System.exit(0);
ProducerConsumer.java:
import java.util.LinkedList;
import java.util.Queue;
public class ProducerConsumer implements Runnable {
private int bufferSize;
private Queue<String> buffer;
public ProducerConsumer(int size) {
bufferSize = size;
buffer = new LinkedList<String>();
}
public void produce(String item) throws InterruptedException {
synchronized(buffer) {
while (buffer.size() >= bufferSize) {
try {
System.out.println("Full buffer. Waiting for consumer...");
buffer.wait();
}catch (Exception e) {}
}
buffer.add(item);
System.out.println("Producer is putting " + item + " in the buffer");
buffer.notify();
}
}
public void consume() throws InterruptedException {
synchronized (buffer) {
while (buffer.size() == 0) {
try {
System.out.println("Empty buffer. Waiting for production...");
buffer.wait();
}catch (Exception e) {}
}
System.out.println("Consumer is consuming " + buffer.remove() + ".");
buffer.notify();
}
}
#Override
public void run() {
}
}
MyThread1 :
/*
* PRODUCER - Thread
*/
public class MyThread1 implements Runnable {
private String [] data;
private ProducerConsumer pc;
public MyThread1(ProducerConsumer pc, String [] data) {
this.pc = pc;
this.data = data;
}
#Override
public void run() {
for (int i = 0; i < data.length; i++) {
try {
pc.produce(data[i]);
} catch (InterruptedException ex) {}
}
}
}
MyThread2:
//THE CONSUMER - Thread
public class MyThread2 implements Runnable{
private ProducerConsumer pc;
public MyThread2(ProducerConsumer pc) {
this.pc = pc;
}
//Run consume
#Override
public void run() {
while (true) {
try {
pc.consume();
Thread.sleep(2);
}
catch(InterruptedException e) {}
}
}
}
On recent machines, with short queues like this, you will never see actual multithreading effects like, in this case, producer and consumer taking turns unless you slow both of them down a bit. You only slowed down the consumer. Instead of using a short array, put a million Integers in a queue and see what happens.
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class ProduserConsumerDemo {
public static void main(String[] args) {
List<Integer> list = new CopyOnWriteArrayList<>();
int size = 5;
Producer producer = new Producer(list, size);
Consumer consumer = new Consumer(list);
Thread t1 = new Thread(producer, "Producer");
Thread t2 = new Thread(consumer, "Consumer");
t1.start();
t2.start();
}
}
class Producer implements Runnable {
private final List<Integer> list;
private final int size;
public Producer(List<Integer> list, final int size) {
this.list = list;
this.size = size;
}
public void run() {
try {
produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void produce() throws InterruptedException {
int i = 0;
while (i >= 0) {
synchronized (list) {
while (list.size() == size) {
System.out.println(
"List is full." + Thread.currentThread().getName() + " is waiting. Size:" + list.size());
list.wait();
}
System.out.println("Produce :" + i);
list.add(i++);
Thread.sleep(50);
list.notify();
}
}
}
}
class Consumer implements Runnable {
private final List<Integer> list;
public Consumer(List<Integer> list) {
this.list = list;
}
public void run() {
try {
consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void consume() throws InterruptedException {
while (true) {
synchronized (list) {
while (list.isEmpty()) {
System.out.println(
"List is empty. " + Thread.currentThread().getName() + " is waiting. Size:" + list.size());
list.wait();
}
System.out.println("Consumed item:" + list.remove(0));
Thread.sleep(50);
list.notify();
}
}
}
}

Java wait/notify not call in thread

I have a problem when scheduling two threads (ReadData and WriteData). It seems that I can't notify after wait.
Here is my class which I define and call:
Buffer: The buffer I use to read / write data. I synch this class.
public class Buffer {
// Size of buffer we use to store data
public static final int SIZE = 10;
// Data of buffer.
private int[] values;
// Count of element in data.
private int count;
// Instance of buffer, for singleton pattern
private static Buffer instance = null;
// A signal show data in use (busy) or not
private static Object mutex = new Object();
// Constructor of buffer
private Buffer(){
values = new int[SIZE];
count = 0;
}
// Get instance of buffer.
public static Buffer getInstance(){
if(instance == null){
synchronized (mutex) {
if(instance == null) instance = new Buffer();
}
}
return instance;
}
public void addValue(int value){
synchronized (mutex) {
if(count >= SIZE) return;
values[count++] = value;
}
}
// Return current data and then reset buffer.
public int[] getValues(){
synchronized (mutex) {
if(count == 0) return null;
int[] values = new int[count];
System.arraycopy(this.values, 0, values, 0, count);
count = 0;
return values;
}
}
public int getCount(){
synchronized (mutex) {
return count;
}
}
}
ReadData: This class I use to store data into the buffer.
import java.util.Random;
public class ReadData implements Runnable {
Buffer buffer = Buffer.getInstance();
#Override
public synchronized void run() {
Random random = new Random();
while(true){
if(buffer.getCount() >= Buffer.SIZE){
try {
System.out.println("Read: is waiting. . .");
mState.readIsWaiting();
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("Read: storing data");
buffer.addValue(random.nextInt(Buffer.SIZE) + 1);
}
}
}
public interface ReadState{
void readIsWaiting();
}
private ReadState mState;
public void setReadState(ReadState state){
mState = state;
}
public synchronized void makeNotify() {
notifyAll();
}
}
WriteData: This class I use to get data from the buffer
public class WriteData implements Runnable {
Buffer buffer = Buffer.getInstance();
#Override
public synchronized void run() {
while(true){
if(buffer.getCount() == 0){
try {
System.out.println("Write: is waiting. . .");
mState.writeIsWaiting();
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
int[] getdata = buffer.getValues();
System.out.println("first data: " + getdata[0]);
}
}
}
public interface WriteState{
void writeIsWaiting();
}
private WriteState mState;
public void setWriteState(WriteState state){
mState = state;
}
public synchronized void makeNotify() {
notifyAll();
}
}
MyThread: The thread I use to start read/write
package main;
import testing.ReadData;
import testing.WriteData;
public class MyThread implements ReadData.ReadState, WriteData.WriteState {
private ReadData read;
private WriteData write;
public void start(){
read = new ReadData();
write = new WriteData();
read.setReadState(this);
write.setWriteState(this);
new Thread(read).start();
new Thread(write).start();
}
#Override
public void writeIsWaiting() {
read.makeNotify();
}
#Override
public void readIsWaiting() {
write.makeNotify();
}
}
That's it. Sometimes it works, many times it stops and waits.
How do I solve this problem? Thanks
I think you've reversed the ReadData implementation with the WriteData. In the code shown, the ReadData thread will block if the buffer is full:
#Override
public synchronized void run() {
Random random = new Random();
while(true){
if(buffer.getCount() >= Buffer.SIZE){
try {
System.out.println("Read: is waiting. . .");
mState.readIsWaiting();
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("Read: storing data");
buffer.addValue(random.nextInt(Buffer.SIZE) + 1);
}
}
}
What you really need is:
#Override
public synchronized void run() {
Random random = new Random();
while(true){
if(buffer.getCount() == 0){
try {
System.out.println("Read: is waiting. . .");
mState.readIsWaiting();
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("Read: storing data");
buffer.addValue(random.nextInt(Buffer.SIZE) + 1);
}
}
}
Similarly, in the WriteData implementation, you should block if the buffer is full. This will happen if the reader hasn't had a chance to take the elements from the buffer. This should work for the WriteData code:
#Override
public synchronized void run() {
while(true){
if(buffer.getCount() >= Buffer.SIZE){
try {
System.out.println("Write: is waiting. . .");
mState.writeIsWaiting();
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
int[] getdata = buffer.getValues();
System.out.println("first data: " + getdata[0]);
}
}
}

Threads: How to put a check inside Thread Consumer for Data reciving check

I have this producer Consumer sample Program shown below
How can i put a Condition inside my Consumer Thread class so that if i didn't recivied the data from producer for 1 minute , i need to log that ??
This is my Producer Consumer Program
public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) {
}
}
contents = value;
available = true;
notifyAll();
}
}
class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
while(true)
{
for (int i = 0; i < 100000; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number + " put: " + i);
try {
sleep((int) (Math.random() * 2000));
} catch (Exception e) {
}
}
}
}
}
class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
while(true)
{
int value = 0;
for (int i = 0; i < 100000; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
}
Could anybody please help
You could use Object#wait(long timeout) and log from inside the get() method:
try {
wait(60 * 1000);
if (available == false) {
//log
}
} catch (InterruptedException e) {
}
Use System.currentTimeMilis() in your Consumer run method:
long before;
for (int i = 0; i < 100000; i++) {
before = System.currentTimeMilis();
value = cubbyhole.get();
if (System.currentTimeMilis() - before > 1000 * 60) {
System.out.println("Consumer waited for more than one minute");
}
System.out.println("Consumer #" + this.number + " got: " + value);
}

Categories