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 :)
I have just started learning threads and pretty new to it.I'm trying to print alphabets and numbers one after the other.I have synchronized them using a flag but of no use.
public class Alphabets {
public static void main(String[] args) {
AN an= new AN(false);
Thread t1=new Thread(new Runnable() {
#Override
public void run() {
try {
an.Alpha();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread t2= new Thread(new Runnable() {
#Override
public void run() {
try {
an.numbers();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
...
class AN
{
boolean flag;
AN(boolean flag)
{
this.flag=flag;
}
synchronized void Alpha() throws InterruptedException
{
if(flag==false)
{
for(char i='A'; i<='Z';i++)
{
System.out.println(+i);
notifyAll();
flag=true;
}
}
else
{
wait();
}
}
synchronized void numbers() throws InterruptedException
{
if(flag==true)
{
for(int i=1;i<=26;i++)
{
System.out.println(+i);
notifyAll();
flag=false;
}
}
else
{
wait();
}
}
}
My desired output is : a1b2c3d4....
My console output is : abcd...1234...
Can anybody point out the mistake since I'm unable to synchronize these two threads.
Change class AN to check the flag in while loop.
public class AN {
boolean flag;
AN(boolean flag) {
this.flag = flag;
}
synchronized void Alpha() throws InterruptedException {
for(char i = 'A'; i <= 'Z'; i++) {
while(flag == true) {
wait();
}
System.out.println(i);
notifyAll();
flag = true;
}
}
synchronized void numbers() throws InterruptedException {
for(int i = 1; i <= 26; i++) {
while(flag == false) {
wait();
}
System.out.println(i);
notifyAll();
flag = false;
}
}
Well, what you want is more or less pipelining, therefore the threads need to know when they are allowed to work.
I'd therefore use a Queue to await the input on one thread and wait for it to be set in the other thread.
Class AN:
import java.util.concurrent.BlockingQueue;
class AN
{
BlockingQueue<Boolean> input;
BlockingQueue<Boolean> output;
AN(BlockingQueue<Boolean> input, BlockingQueue<Boolean> output)
{
this.input = input;
this.output = output;
}
void Alpha() throws InterruptedException
{
for (char i = 'a'; i <= 'z'; i++) {
input.take();
System.out.print(i);
output.put(Boolean.TRUE);
}
}
void numbers() throws InterruptedException
{
for (int i = 1; i <= 26; i++) {
input.take();
System.out.print(i);
output.put(Boolean.TRUE);
}
}
}
Class Test (or where your main is):
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Test
{
public static void main(String[] args) throws InterruptedException
{
BlockingQueue<Boolean> input = new LinkedBlockingQueue<>();
BlockingQueue<Boolean> output = new LinkedBlockingQueue<>();
AN an1 = new AN(output, input);
AN an2 = new AN(input, output);
output.add(Boolean.TRUE);
Thread t1 = new Thread(new Runnable()
{
#Override
public void run()
{
try {
an1.Alpha();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable()
{
#Override
public void run()
{
try {
an2.numbers();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
Outputs: a1b2c3d4e5f6g7h8i9j10k11l12m13n14o15p16q17r18s19t20u21v22w23x24y25z26
You can achieve this by using BlockingQueue and Object's wait and notify methods.
public class AlphaNum {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
AtomicBoolean flag = new AtomicBoolean(Boolean.TRUE);
Object lock = new Object();
Thread t1 = new Thread(new Runnable() {
#Override
public void run() {
try {
for(int i=1;i<=26;i++){
synchronized (lock){
while (flag.get()){
lock.wait();
}
System.out.print(i);
flag.set(Boolean.TRUE);
lock.notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
#Override
public void run() {
try {
for(char c='A';c<='Z';c++){
synchronized (lock){
while (!flag.get()){
lock.wait();
}
System.out.print(c);
flag.set(Boolean.FALSE);
lock.notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
The above code prints a1b2c3d4.......z26
There are many ways to achieve this, Below one is one of them.
package interview;
public class TestAplhaAndNumberPrinterThreads {
// created object as static so we can access print method of this class from thread's run methods
public static TestAplhaAndNumberPrinterThreads output = new TestAplhaAndNumberPrinterThreads();
private final Object syncer = new Object();
private int state = 0;
public void print(char character) throws InterruptedException {
synchronized (syncer) {
while (true) {
if (state == 0) {
System.out.print(character + ", ");
state = 1;
syncer.notify();
return;
} else {
syncer.wait();
}
}
}
}
public void print(int number) throws InterruptedException {
synchronized (syncer) {
while (true) {
if (state == 1) {
System.out.print(number + ", ");
state = 0;
syncer.notify();
return;
} else {
syncer.wait();
}
}
}
}
public static void main(String[] args) {
NumberPrinter numberPrinter = new NumberPrinter();
AlphabetsPrinter alphabetsPrinter = new AlphabetsPrinter();
numberPrinter.start();
alphabetsPrinter.start();
}
}
class NumberPrinter extends Thread {
#Override
public void run() {
try {
for (int i = 1; i <= 26; i++) {
TestAplhaAndNumberPrinterThreads.output.print(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AlphabetsPrinter extends Thread {
#Override
public void run() {
try {
for (char i = 'a'; i <= 'z'; i++) {
TestAplhaAndNumberPrinterThreads.output.print(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// output: a, 1, b, 2, c, 3, d, 4, e, 5, f, 6, g, 7, h, 8, i, 9, j, 10, k, 11, l, 12, m, 13, n, 14, o, 15, p, 16, q, 17, r, 18, s, 19, t, 20, u, 21, v, 22, w, 23, x, 24, y, 25, z, 26,
I think the big things is for you to know, how to use break points to debug in IDE and why your code was not working. You can see the process of your code will run. I have post the picture at the last and this code which will work like your wish
class AN {
boolean flag;
AN(boolean flag) {
this.flag = flag;
}
synchronized void Alpha() throws InterruptedException {
if (flag == false) {
for (char i = 'A'; i <= 'Z'; i++) {
System.out.println(i);
flag = true;
notify();
wait();
}
}
}
synchronized void numbers() throws InterruptedException {
if (flag == true) {
for (int i = 1; i <= 26; i++) {
System.out.println(+i);
flag = false;
notify();
wait();
}
}
}
}
enter image description here
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();
}
I have program and I have set some debug point in it,but when control reached to the wait method in side the printEven method, control goes to till wait method and become hidden and nothing happened. Can any one explain how to debug wait. Control goes to wait method and never return.I am using eclipse to debug it.
public class PrintEvenOddTester
{
public static void main(String ... args)
{
Printer print = new Printer();
Thread t1 = new Thread(new TaskEvenOdd(print, 10, false));
Thread t2 = new Thread(new TaskEvenOdd(print, 10, true));
t1.start();
t2.start();
}
}
class TaskEvenOdd implements Runnable
{
private int max;
private Printer print;
private boolean isEvenNumber;
TaskEvenOdd(Printer print, int max, boolean isEvenNumber)
{
this.print = print;
this.max = max;
this.isEvenNumber = isEvenNumber;
}
public void run()
{
//System.out.println("Run method");
int number = isEvenNumber == true ? 2 : 1;
while(number<= max)
{
if(isEvenNumber)
{
//System.out.println("Even :"+ Thread.currentThread().getName());
print.printEven(number);
// number+=2;
}
else
{
//System.out.println("Odd :"+ Thread.currentThread().getName());
print.printOdd(number);
// number+=2;
}
number+=2;
}
}
}
class Printer
{
boolean isOdd= false;
synchronized void printEven(int number)
{
while(isOdd == false)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Even:"+number);
isOdd = false;
notifyAll();
}
synchronized void printOdd(int number)
{
while(isOdd == true)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Odd:"+number);
isOdd = true;
notifyAll();
}
}
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]);
}
}
}