I use semaphore and I want when list size is zero thread waiting for other thread But Why doesn't the semaphore stop thrading? Doesn't the semaphore work like notify and wait?
result:
add
remove
add
Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
ArrayList<String> list = new ArrayList<>();
Semaphore semaphore = new Semaphore(0);
new Producer(list, semaphore).start();
new Producer(list, semaphore).start();
new Customeer(list, semaphore).start();
new Customeer(list, semaphore).start();
//////////
static class Customeer extends Thread {
private List<String> list;
private Semaphore semaphore;
public Customeer(List<String> list, Semaphore semaphore) {
this.list = list;
this.semaphore = semaphore;
}
#Override
public void run() {
synchronized (list) {
if (list.size() == 0) {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.remove(0);
System.out.println("remove");
}
}
}
static class Producer extends Thread {
private Semaphore semaphore;
private List<String> list;
public Producer(List<String> list, Semaphore semaphore) {
this.list = list;
this.semaphore = semaphore;
}
#Override
public void run() {
synchronized (list) {
list.add("hello");
semaphore.release();
System.out.println("add");
}
}
}
}
you seem to be confused with semaphores and synchronization. semaphores is used when you want to allow n threads to access the same resource at a time. [n can be 1] while synchronization is used if you want to allow only 1 thread to access the resource.
solution using semaphores
// Java implementation of a producer and consumer
// that use semaphores to control synchronization.
import java.util.concurrent.Semaphore;
class Q {
// an item
int item;
// semCon initialized with 0 permits
// to ensure put() executes first
static Semaphore semCon = new Semaphore(0);
static Semaphore semProd = new Semaphore(1);
// to get an item from buffer
void get()
{
try {
// Before consumer can consume an item,
// it must acquire a permit from semCon
semCon.acquire();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
// consumer consuming an item
System.out.println("Consumer consumed item : " + item);
// After consumer consumes the item,
// it releases semProd to notify producer
semProd.release();
}
// to put an item in buffer
void put(int item)
{
try {
// Before producer can produce an item,
// it must acquire a permit from semProd
semProd.acquire();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
// producer producing an item
this.item = item;
System.out.println("Producer produced item : " + item);
// After producer produces the item,
// it releases semCon to notify consumer
semCon.release();
}
}
// Producer class
class Producer implements Runnable {
Q q;
Producer(Q q)
{
this.q = q;
new Thread(this, "Producer").start();
}
public void run()
{
for (int i = 0; i < 5; i++)
// producer put items
q.put(i);
}
}
// Consumer class
class Consumer implements Runnable {
Q q;
Consumer(Q q)
{
this.q = q;
new Thread(this, "Consumer").start();
}
public void run()
{
for (int i = 0; i < 5; i++)
// consumer get items
q.get();
}
}
// Driver class
class PC {
public static void main(String args[])
{
// creating buffer queue
Q q = new Q();
// starting consumer thread
new Consumer(q);
// starting producer thread
new Producer(q);
}
}
solution using synchronized
// Java program to implement solution of producer
// consumer problem.
import java.util.LinkedList;
public class Threadexample {
public static void main(String[] args)
throws InterruptedException
{
// Object of a class that has both produce()
// and consume() methods
final PC pc = new PC();
// Create producer thread
Thread t1 = new Thread(new Runnable() {
#Override
public void run()
{
try {
pc.produce();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable() {
#Override
public void run()
{
try {
pc.consume();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Start both threads
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
}
// This class has a list, producer (adds items to list
// and consumber (removes items).
public static class PC {
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
// Function called by producer thread
public void produce() throws InterruptedException
{
int value = 0;
while (true) {
synchronized (this)
{
// producer thread waits while list
// is full
while (list.size() == capacity)
wait();
System.out.println("Producer produced-"
+ value);
// to insert the jobs in the list
list.add(value++);
// notifies the consumer thread that
// now it can start consuming
notify();
// makes the working of program easier
// to understand
Thread.sleep(1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true) {
synchronized (this)
{
// consumer thread waits while list
// is empty
while (list.size() == 0)
wait();
// to retrive the ifrst job in the list
int val = list.removeFirst();
System.out.println("Consumer consumed-"
+ val);
// Wake up producer thread
notify();
// and sleep
Thread.sleep(1000);
}
}
}
}
}
read the resources for more clarity
semaphore: https://www.geeksforgeeks.org/producer-consumer-solution-using-semaphores-java/
synchronized: https://www.geeksforgeeks.org/producer-consumer-solution-using-threads-java/
Related
I've been trying to make a simple implementation of Thread-Pool using Active Objects.
Here is my Main:
public static void main(String[] args){
MyThreadPool tp = new MyThreadPool(100,3);
tp.execute(()->{
try { Thread.sleep(5*1000); } catch (InterruptedException e) {}
System.out.println("42");
});
tp.shutDown();
}
The shutDown method is usually called first through the Main and therefore keeps the Active Objects "alive" unwantedly, but sometimes I get the wanted outcome.
Any idea why there is uncertainty about the result?
Below you can see the rest of the classes:
public class MyThreadPool {
ArrayBlockingQueue<Runnable> q;
ArrayBlockingQueue<ActiveObject> activeObjects;
volatile boolean stop;
AtomicInteger count;
Thread t;
Runnable stopTask;
public MyThreadPool(int capacity, int maxThreads) {
activeObjects = new ArrayBlockingQueue<>(maxThreads);
q = new ArrayBlockingQueue<>(capacity);
count = new AtomicInteger(0);
stopTask = ()->stop = true;
t=new Thread(()->{
//System.out.println("Thread-Pool Started");
while(!stop){
//if queue is empty it is gonna be a blocking call
try {
Runnable task = q.take();
if(task==stopTask)
stopTask.run();
else
//size() is atomic integer
if (count.get() < maxThreads) {
ActiveObject a = new ActiveObject(capacity);
activeObjects.put(a);
count.incrementAndGet();
a.execute(task);
}
//we will assign the next task to the least busy ActiveObject
else {
int minSize = Integer.MAX_VALUE;
ActiveObject choice = null;
for (ActiveObject a : activeObjects) {
if (a.size() < minSize) {
minSize = a.size();
choice = a;
}
}
choice.execute(task);
}
} catch (InterruptedException e) { }
}
//System.out.println("Thread-Pool Ended");
});
t.start();
}
//execute returns right away - just puts into the queue
public void execute(Runnable r ){
// if capacity is full it is gonna be a blocking call
if(!stop)
try { q.put(r); } catch (InterruptedException e) { }
}
public void shutDownNow(){
activeObjects.forEach(a->a.shutDownNow());
stop = true;
t.interrupt();
}
public void shutDown(){
activeObjects.forEach(a->a.shutDown());
execute(stopTask);
}
public class ActiveObject {
ArrayBlockingQueue<Runnable> q;
volatile boolean stop;
Thread t;
public ActiveObject(int capacity) {
q = new ArrayBlockingQueue<>(capacity);
t=new Thread(()->{
//System.out.println("Active Object Started");
while(!stop){
//if queue is empty it is gonna be a blocking call
try {
q.take().run();
} catch (InterruptedException e) { }
}
//System.out.println("Active Object Ended");
});
t.start();
}
//execute returns right away - just puts into the queue
public void execute(Runnable r ){
// if capacity is full it is gonna be a blocking call
if(!stop)
try { q.put(r); } catch (InterruptedException e) { }
}
public void shutDownNow(){
stop = true;
t.interrupt();
}
public void shutDown(){
execute(()->stop=true);
}
public int size(){
return q.size();
}
}
In your main method you create a thread pool (which also creates and starts tp.t thread), enqueue a task into tp.q, and then call tp.shutDown():
MyThreadPool tp = new MyThreadPool(100, 3);
tp.execute(() -> {...});
tp.shutDown();
Imagine that tp.shutDown() in the main thread is executed before the MyThreadPool.t thread processes the enqueued task:
activeObjects.forEach(a -> a.shutDown());
execute(stopTask);
here activeObjects is empty, you enqueue stopTask into tp.q, and main thread finishes.
Now we only have MyThreadPool.t thread, let's see what it does:
while (!stop) {
try {
Runnable task = q.take();
if (task == stopTask)
stopTask.run();
else
if (count.get() < maxThreads) {
ActiveObject a = new ActiveObject(capacity);
activeObjects.put(a);
count.incrementAndGet();
a.execute(task);
}
else {
...
}
} catch (InterruptedException e) {
}
}
At this point q contains 2 tasks: a normal task and stopTask.
In the first loop iteration the normal task is taken from q, and is given for processing to a newly created ActiveObject:
ActiveObject a = new ActiveObject(capacity);
activeObjects.put(a);
count.incrementAndGet();
a.execute(task);
new ActiveObject() also creates and starts its own internal ActiveObject.t thread.
The second loop iteration processes stopTask:
if (task == stopTask)
stopTask.run();
which sets stop = true.
As a result, the next check while (!stop) returns false and MyThreadPool.t thread finishes.
Now we only have ActiveObject.t thread, which hasn't been stopped:
while (!stop) {
try {
q.take().run();
} catch (InterruptedException e) {
}
}
here the thread will keep waiting on q.take() forever.
I have this piece of code and I want a good method to stop the consumer threads:
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
public class Foo {
private final Queue<Object> queue;
private final AtomicBoolean doneReading;
private final int numberOfThreads = 4, N = 100;
public Foo() {
queue = new ArrayDeque<>();
doneReading = new AtomicBoolean(false);
}
public void execute() throws InterruptedException {
Thread[] threads = new Thread[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
threads[i] = new Thread(() -> {
try {
synchronized (queue) {
while (!doneReading.get() || !queue.isEmpty()) {
if (queue.isEmpty()) {
queue.wait();
if (!queue.isEmpty()) {
Object element = queue.remove();
// Do stuff
}
}
else {
Object element = queue.remove();
// Do stuff
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
threads[i].start();
}
for (int i = 0; i < N; i++) {
synchronized (queue) {
queue.add(new Object());
queue.notifyAll();
}
}
doneReading.set(true);
synchronized (queue) {
queue.notifyAll();
}
for (Thread thread : threads) {
thread.join();
}
}
}
Basically, when I have read all the data that needs to be processed, I want the consumer threads to stop. I tried while(!doneReading.get()) but this does not guarantee that there aren't any leftover items in the queue. I added !queue.isEmpty(), but in this case some threads keep on waiting even though they won't receive any notification. So I managed that I should call notifyAll() once more. This does seem to work. I also thought of adding a null in the queue, and whenever the consumer reads a null, it exits the while. Which method is better, or are there any better ideas?
One usual method is a "poison pill". Put a special value in the queue that when read kills the consumer threads. This allows them to process all of the values and not stop until they read past the final value and read the poison pill.
Some more info: https://java-design-patterns.com/patterns/poison-pill/
I also like these websites, they often have thoughtful information on Java programming:
https://mkyong.com/java/java-blockingqueue-examples/
https://www.baeldung.com/java-blocking-queue
class LimitedQueue<T> {
ArrayDeque<T> queue = new ArrayDeque<>();
boolean done = false;
synchronized void add (T item) {
queue.add(item);
notifyAll();
}
synchronized void done()
done=true;
notifyAll();
}
// most complex method
// waits until next item or done signal is put
synchronized boolean isDone() {
for (;;) {
if (!queue.isEmpty(){
return false;
}
if (done) {
return true;
}
wait();
}
}
syncronized T remove() {
return deque.remove();
}
}
LimitedQueue<Object> queue = new LimitedQueue<>();
class ConsumerThread extends Thread {
public void run(){
while (!queue.isDone()) {
Object element = queue.remove();
// do stuff
}
}
}
class ProducerThread extends Thread {
public void run() {
for (int i = 0; i < N; i++) ,{
queue.add(new Object());
}
queue.done();
}
}
I have two threads, A and B. A has to create messages and store them in a list in memory and B has to read the first message from the same list in memory, remove it from the list and do something with it.
A and B start with a main thread.
My question is how can I make a list that is shared by two different threads?
You should read this: https://www.geeksforgeeks.org/producer-consumer-solution-using-threads-java/
And the Java sample they have there should help with some modifications for your scenario.
// Java program to implement solution of producer
// consumer problem.
import java.util.LinkedList;
public class Threadexample
{
public static void main(String[] args)
throws InterruptedException
{
// Object of a class that has both produce()
// and consume() methods
final PC pc = new PC();
// Create producer thread
Thread t1 = new Thread(new Runnable()
{
#Override
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable()
{
#Override
public void run()
{
try
{
pc.consume();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Start both threads
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
}
// This class has a list, producer (adds items to list
// and consumber (removes items).
public static class PC
{
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
// Function called by producer thread
public void produce() throws InterruptedException
{
int value = 0;
while (true)
{
synchronized (this)
{
// producer thread waits while list
// is full
while (list.size()==capacity)
wait();
System.out.println("Producer produced-"
+ value);
// to insert the jobs in the list
list.add(value++);
// notifies the consumer thread that
// now it can start consuming
notify();
// makes the working of program easier
// to understand
Thread.sleep(1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
// consumer thread waits while list
// is empty
while (list.size()==0)
wait();
//to retrive the ifrst job in the list
int val = list.removeFirst();
System.out.println("Consumer consumed-"
+ val);
// Wake up producer thread
notify();
// and sleep
Thread.sleep(1000);
}
}
}
}
}
As others have advised, I think you should spend some time understanding threading and producer consumer pattern.
First create an instance of the list.
Second pass the list as argument to the constructor of both runnables.
public static void main(String args[]){
List<MyType> myList = new ArrayList<>();
Runnable r1 = new MyRunnable(myList);
Runnable r2 = new MyRunnable(myList);
new Thread(r1).start();
new Thread(r2).start();
}
public class MyRunnable implements Runnable {
List list;
public MyRunnable(List list) {
this.list = list;
}
#Override
public void run() {}
}
public class SemaphoreWithQueues implements Semaphore {
private List<Object> queue;
private AtomicInteger current = new AtomicInteger(0);
private int permits;
public SemaphoreWithQueues(int permits) {
this.permits = permits;
this.queue = Collections.synchronizedList(new LinkedList<>());
}
#Override
public void enter() throws InterruptedException {
if (current.get() < permits) {
current.incrementAndGet();
} else {
Object block = new Object();
synchronized (block) {
queue.add(block);
block.wait();
current.incrementAndGet();
}
}
}
#Override
public void leave() {
if(queue.size() != 0) {
Object block = queue.get(0);
queue.remove(0);
synchronized (block) {
block.notify(); //Unblock quenue
}
}
current.decrementAndGet();
//current lessen and current thread have time come in block if(...)
// in enter() faster then another thread increased current
}
}
> The program usually output:
>
> 1 1 2 2 1 1 2 2 1 2
**Where run() of both threads is almost the same, such as:**
public void run(){
for (int i = 0; i <5; i++) {
try {
semaphore.enter();
} catch (InterruptedException e) {
System.err.println(e);
}
System.out.println(2);
semaphore.leave();
}
}
There are 2 threads using this semaphore. When 1 thread increases the queue, the second is waiting, the problem is that if we extracted the object from quene and unblocked it, then the thread that finished leave() start enter() faster and again increments the counter, while the awaked thread also increments the counter, current = 2, and the list is empty.
SORRY FOR BAD ENGLISH
There are many problems in the code.
Synchronization: Synchronization should be done for a shareable
resource. Why it is done for a local object which has scope only for
that method.
Object block = new Object();
synchronized (block) {
Both current and queue are independent properties, they should be
synchronized together.
Now let's come to point If you really want to create a semaphore using Queue. You do not need all this logic. You can use existing Java class e.g. BlockingQueue. Here is the implementation
class SemaphoreWithQueues implements Semaphore{
private BlockingQueue<Integer> queue;
public SemaphoreWithQueues(int permits) {
if(queue == null){
queue = new ArrayBlockingQueue<>(permits);
}
}
public void enter() {
queue.offer(1);
System.out.println(Thread.currentThread().getName() + " got a permit.");
}
public void leave() throws InterruptedException {
queue.take();
System.out.println(Thread.currentThread().getName() + " left the permit.");
}
}
And Task to use the semaphore
class Task implements Runnable {
private SemaphoreWithQueues semaphore;
public Task(SemaphoreWithQueues semaphore){
this.semaphore = semaphore;
}
public void run(){
for (int i = 0; i <5; i++) {
semaphore.enter();
try {
semaphore.leave();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
SemaphoreWithQueues semaphoreWithQueues = new SemaphoreWithQueues(5);
Thread th1 = new Thread(new Task(semaphoreWithQueues));
Thread th2 = new Thread(new Task(semaphoreWithQueues));
Thread th3 = new Thread(new Task(semaphoreWithQueues));
th1.start();
th2.start();
th3.start();
}
}
But personally I do not like using Queue to create Semaphores, as it wastes memory unnecessary by creating elements in queue. Despite of this you can create a semaphore using single shareable object with permits using wait and notify mechanism. You can try with this approach. If you would like.
I'm trying consumer producer problem with int itemHolder with only one entry. I dont know why the consumer thread is not notifying the producer thread when it has put the item, The expected behaviour is that the consumer thread waits till the producer puts the item in itemHolder.
On the other hand when I use locking on an external mutax object it works perfectly.
public class ProducerConsumer {
public static void main(String... args) {
new ProducerConsumer().execute();
}
private volatile int itemHolder = -1; // -1 value represent that ItemHolder is empty
private void execute() {
final Thread producer = new Thread(new Runnable() {
#Override
public void run() {
for (int i = 1; i < 5; i++) {
synchronized (this){
while (itemHolder != -1){ // ItemHolder is full
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
itemHolder = i;
notify();
System.out.println(String.format("producer: ItemHolder has value, Consumer notified..."));
}
}
}
}, "Producer-thread");
final Thread consumer = new Thread(new Runnable() {
#Override
public void run() {
while (true){
synchronized (producer){
try {
while (itemHolder == -1){ // Don't consume if itemHolder don't have a value
producer.wait();
}
System.out.println(String.format("CONSUMER: consuming %s...", itemHolder));
itemHolder = -1; // re-initialize the itemHolder
producer.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}, "Consumer-thread");
consumer.start();
producer.start();
}
With locking on external Mutex
This works correctly as expected.
public class ProducerConsumerWithMutex {
public static void main(String... args) {
new ProducerConsumerWithMutex().execute();
}
private final String mutex = "";
private volatile int itemHolder = -1;
private void execute() {
final Thread producer = new Thread(new Runnable() {
#Override
public void run() {
for (int i = 1; i < 5; i++) {
synchronized (mutex){
while (itemHolder != -1){ // itemHolder is full
try {
mutex.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
itemHolder = i;
System.out.println(String.format("producer: producing %s...", i));
mutex.notify();
System.out.println(String.format("producer: Consumer notified, itemHolder has item..."));
}
}
}
}, "Producer-thread");
final Thread consumer = new Thread(new Runnable() {
#Override
public void run() {
while (true){
synchronized (mutex){
try {
while (itemHolder == -1){
System.out.println("CONSUMER: itemHolder is empty, waiting...");
mutex.wait();
}
System.out.println(String.format("CONSUMER: consuming %s...", itemHolder));
itemHolder = -1;
mutex.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}, "Consumer-thread");
consumer.start();
producer.start();
}
Because in the first producer, you synchronize on this, which is the Runnable, not the producer itself.
So you would need to use synchronized(producer) instead, except that it won't compile because producer is not available yet on that line.
Alternatively you could name your Runnable:
Runnable producerRunnable = ...; //synchronized on this
and in your consumer:
synchronized(producerRunnable) {...}
But your second approach with a separate mutex is preferable, except that locking on "" is an extremely bad idea as this is a global constant (the empty string is in the string pool). You should prefer something like this instead:
private final Object mutex = new Object();
Wait notify works when both the threads are using the same object/ class lock. In your case the locks used for wait/notify are different as mentioned here:
synchronized(producer) // lock on producer object
synchronized(this) // Runnable object.
After supportig assylias' answer here is another option.
private Object lock = new Object();
to lock
synchronized (lock){
lock.wait();
}
to unlock
synchronized (lock){
lock.notify();
}