Blogger

Delete comment from: Java67

Shyam Yadav said...

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class consumeprodue {
private static BlockingQueue queue = new ArrayBlockingQueue<>(10);
public static void main(String[] args) {
Thread p = new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println(getName() + " produced :" + i);
queue.put(i);
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

};
Thread c = new Thread() {

public void run() {

try {
while (true) {
System.out.println(getName() + "consume :" + queue.take());
}

} catch (InterruptedException e) {

e.printStackTrace();
}
}

};

p.start();
c.start();

}

}

Aug 24, 2021, 9:07:31 AM


Posted to How to Solve Producer Consumer Problem in Java using BlockingQueue [Example]

Google apps
Main menu