I have three classes, one that is meant to represent a pile of urls
private Queue<String> queue = new LinkedList<String>();
public Queue<String> getQueue() {
return queue;
}
private int limit = 5;
private int stillParsing;
public synchronized String getNextString() throws InterruptedException {
while (queue.isEmpty()||stillParsing > limit) {
System.out.println("no for you "+ queue.peek());
wait();
}
System.out.println("grabbed");
notify();
stillParsing++;
System.out.println(queue.peek());
return queue.remove();
}
public synchronized void doneParsing() {
stillParsing--;
}
}
A thread class whose run method is
public void run(){
try {
sleep(30);
for(;;){
String currenturl = pile.getNextString();
//(do things)
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
pile.doneParsing();
}
}
And a mapper that actually adds objects into the pile of urls using this snipet
while (urls.hasMoreTokens()) {
try{
word.set(urls.nextToken());
String currenturl = word.toString();
System.out.println(currenturl);
pile.getQueue().add(currenturl);
From debugging what I think happens is that all of the threads try to get from the queue at once before the mapper has a chance to populate it and they get stuck waiting. Unfortunately all of the threads waiting is causing my program to hang up and not add more urls to the queue. How should I go about taking care of this issue? Preferably while still using wait notify.
while (urls.hasMoreTokens()) {
try {
word.set(urls.nextToken());
String currenturl = word.toString();
System.out.println(currenturl);
pile.getQueue().add(currenturl);
In the above code, you're breaking the encapsulation of the pile by adding something to its queue without going though a method of the pile. You should not have a getQueue() method in this class: all the accesses to this shared data structure should be synchronized on the same lock. You should thus add a synchronized method allowing to add a URL to the queue. And this method should also call notify() (or better: notifyAll()), in order to wake up the threads that are waiting for some element to be in the queue:
public synchronized void addUrl(String url) {
queue.add(url);
notifyAll();
}
Even without reading all lines of your code and explanations I can say that your usage of wait and notify are buggy.
Method wait() is blocking. It exits only when notify() on the same monitor is called. This means that you cannot put both wait() and notify() from the same thread. You simply never arrive to notify() because wait() is blocked forever.
Other version of wait(): wait(timeout) is blocked but is limited by specified timeout.
Moreover wait/notify pair work only if they are written into synchronized block:
// thread-1
synchronoized(obj) {
obj.wait();
}
// thread-2
synchronoized(obj) {
obj.notify();
}
Thread-1 will exit wait when thread-2 calls notify.
Related
Can I make a static function that notifyAll threads that are waiting in any instance of this class?
(The logic behind here is that I have queues that have threads waiting in them because they are empty and I want to finish the run of the program when calling this function)
queue = new Vector<T>();
public synchronized T extract(){
while(queue.isEmpty())
try {
this.wait();
if(!active) {return null;}
}catch(InterruptedException e) {}
T t = queue.elementAt(0);
queue.remove(0);
return t;
}
This is the extract method.
when the queue is empty the threads go into waiting.
I want to make the boolean "active" to false and notifyAll.
Then I will make sure the threads will not call this method anymore
The logic behind here is that I have queues that have threads waiting in them because they are empty and I want to finish the run of the program when calling this function
IMO, a better solution to your problem would be to continue using per-instance locks, but submit a poison pill to each queue when it's time to shut all of them down.
final T poison_pill = new T(...);
public synchronized T extract(){
while(queue.isEmpty())
try {
this.wait();
}catch(InterruptedException e) {}
T t = queue.elementAt(0);
if (t == poison_pill) {
...My preference would be to raise an exception here, but...
return NULL; // ...this was in your original example.
}
else {
queue.remove(0);
return t;
}
}
If you really want to be able to notify() all of the different instances at the same time, then you'll have to make all of them wait() on the same global lock object.
final Object global_lock = new Object();
public T extract(){
synchronized(global_lock) {
while(queue.isEmpty())
try {
global_lock.wait();
if(!active) {return null;}
}catch(InterruptedException e) {}
T t = queue.elementAt(0);
queue.remove(0);
return t;
}
}
And then somewhere else:
synchronized(lock) {
...
global_lock.notifyAll();
}
Semaphore:
public void enter() throws InterruptedException {
synchronized (block) {
++current;
if (current > permits) {
try {
block.wait();
} catch (InterruptedException e) {
throw e;
}
}
}
}
public void realese() {
synchronized (block) {
--current;
block.notify();
}
}
How to make a queue in the semaphore? I want threads to be executed in the order of calling the enter().
Here's one way:
Have the release() method wake up every waiting thread (i.e., block.notifyAll())
In the enter() method, have each waiting thread look at the head of the queue.
If the thread at the head of the queue is the current thread, pop the queue and complete the enter() call, -OR-
Go back to waiting otherwise.
But there's a subtle problem that I'll leave to you to work out how to fix:
If thread A wakes up, finds itself at the head of the queue, pops the queue and returns,
Then, thread B could wakes up, finds its self at the head of the queue, and it also pops the queue and returns.
And there you have one release() call which just allowed two threads to enter().
That would be bad. So, you'll need to figure out a way to guarantee that no more than one thread can complete the enter() call each time release() is called.
If your using a newer java you can just add a fairness setting to the constructor.
Semaphore(int permits, boolean fair)
like this
private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
I have a FileReader class which is like this
public class FileReader extends Thread
{
private final Object lock = new Object();
public FileReader(String path, FileReaderCallback callback)
{
super(path);
this.path = path;
this.callback = callback;
}
#Override
public void run()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String info;
while ((info = reader.readLine()) != null)
{
synchronized (lock)
{
callback.onDone(path, info);
try
{
lock.wait();
}
catch (Exception ignored)
{
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void next()
{
synchronized (lock)
{
try
{
lock.notify();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
And I have two instance of this FileReader because I want to read two file line by line simultaneously. The problem is my code only reads one line from both file and then it's going to pause.
I Call the function on my callback like this
public void onDone(String path, String info)
{
reader1.next();
reader2.next();
}
So what's the problem?!
Thanks in advance
Your lock object which you synchronize the next() method to is also used within your while loop in the run method. Therefore, the code of your next() method cannot be called from within another thread.
Just assume the following program flow:
You start reader1 thread
You start reader2 thread
At some time one of those two threads start. Let's assume reader1 thread starts first:
It syncs to its lock object
It reads a line from the file
It calls its callback, i.e. calls next() on reader1 and reader2. This call is successful (but actually a no-op)
It calls wait on its lock object. And waits...
At some later time the reader2 thread starts
It syncs to its lock object
It reads a line from the file
It calls its callback, however, when calling reader1.next() it tries to synchronize to the reader1 its lock object from a different thread, thus putting your program into a deadlock state.
To solve this problem I would really suggest to overwork the concept of how you perform the line-by-line synchronization. An easy fix would probably be to use a different lock variable for your next() method.
You are calling listener call back while holding the lock on the same object lock. This will allow notify to be invoked before a wait is invoked. This will make your thread wait forever.
You should,
Use java.util.CountDownLatch for this problem.
Use ThreadPool. Extending from thread is old way of doing it and prone to errors.
You are facing a classic deadlock scenario. Let the first lock be lock1 and the second lock be lock2. In your first instance, the lock status can be expressed as follows:
synchronized (lock1) {
// start of onDone
synchronized (lock1) {
}
synchronized (lock2) {
}
// end of onDone
}
and in second one, it is like this:
synchronized (lock2) {
// start of onDone
synchronized (lock1) {
}
synchronized (lock2) {
}
// end of onDone
}
You should refine your logic, as other answers suggest.
Another flaw in your design is; you are also not considering possible spurious wakeups. Generally, you should put your wait() calls in a while loop.
Class clazz has two methods methodA() and methodB().
How to ensure that methodB is "blocked" if some threads are in methodA in Java (I am using Java 8)?
By "blocking methodB", I mean that "wait until no threads are in methodA()". (Thanks to #AndyTurner)
Note that the requirement above allows the following situations:
Multiple threads are simultaneously in methodA.
Multiple threads are in methodB while no threads are in methodA.
Threads in methodB does not prevent other threads from entering methodA.
My trial: I use StampedLock lock = new StampedLock.
In methodA, call long stamp = lock.readLock()
Create a new method unlockB and call lock.unlockRead(stamp) in it.
In methodB, call long stamp = lock.writeLock() and lock.unlockWrite(stamp).
However, this locking strategy disallows the second and the third situations above.
Edit: I realize that I have not clearly specified the requirements of the synchronization between methodA and methodB. The approach given by #JaroslawPawlak works for the current requirement (I accept it), but not for my original intention (maybe I should first clarify it and then post it in another thread).
I think this can do the trick:
private final Lock lock = new ReentrantLock();
private final Semaphore semaphore = new Semaphore(1);
private int threadsInA = 0;
public void methodA() {
lock.lock();
threadsInA++;
semaphore.tryAcquire();
lock.unlock();
// your code
lock.lock();
threadsInA--;
if (threadsInA == 0) {
semaphore.release();
}
lock.unlock();
}
public void methodB() throws InterruptedException {
semaphore.acquire();
semaphore.release();
// your code
}
Threads entering methodA increase the count and try to acquire a permit from semaphore (i.e. they take 1 permit if available, but if not available they just continue without a permit). When the last thread leaves methodA, the permit is returned. We cannot use AtomicInteger since changing the count and acquiring/releasing permit from semaphore must be atomic.
Threads entering methodB need to have a permit (and will wait for one if not available), but after they get it they return it immediately allowing others threads to enter methodB.
EDIT:
Another simpler version:
private final int MAX_THREADS = 1_000;
private final Semaphore semaphore = new Semaphore(MAX_THREADS);
public void methodA() throws InterruptedException {
semaphore.acquire();
// your code
semaphore.release();
}
public void methodB() throws InterruptedException {
semaphore.acquire(MAX_THREADS);
semaphore.release(MAX_THREADS);
// your code
}
Every thread in methodA holds a single permit which is released when the thread leaves methodA.
Threads entering methodB wait until all 1000 permits are available (i.e. no threads in methodA), but don't hold them, which allows other threads to enter both methods while methodB is still being executed.
You can't really prevent that methodA or methodB is called (while other threads are inside the other method) but you can implement thread intercommunication in such a way so that you can still achieve what you want.
class MutualEx {
boolean lock = false;
public synchronized void methodA() {
if (lock) {
try {
wait();
}catch (InterruptedException e) {
}
}
//do some processing
lock = true;
notifyAll();
}
public synchronized void methodB() {
if (!lock) {
try {
wait();
}catch (InterruptedException e) {
}
}
//do some processing
lock = false;
notifyAll();
}
}
Now, for this to work any Thread object you create should have a reference to the same instance of MutualEx object.
Why not using an kind of external orchestrator?
I mean another class that will be responsible to call the methodA or methodB when it allowed.
Multi-thread can still be handle via locking or maybe just with some AtomicBoolean(s).
Please find below a naive draft of how to do it.
public class MyOrchestrator {
#Autowired
private ClassWithMethods classWithMethods;
private AtomicBoolean aBoolean = = new AtomicBoolean(true);
public Object callTheDesiredMethodIfPossible(Method method, Object... params) {
if(aBoolean.compareAndSet(true, false)) {
return method.invoke(classWithMethods, params);
aBoolean.set(true);
}
if ("methodA".equals(method.getName())) {
return method.invoke(classWithMethods, params);
}
}
}
In very simple terms what you all need is ENTER methodB only if no thread inside methodA.
Simply you can have a global counter, first initialized to 0 to record the number of threads that are currently inside methodA(). You should have a lock/mutex assigned to protect the variable count.
Threads entering methodsA do count++.
Threads exiting methodA do count-- .
Threads that are entering methodB first should check whether count == 0.
methodA(){
mutex.lock();
count++;
mutex.signal();
//do stuff
mutex.lock();
count--;
mutex.signal();
}
methodB(){
mutex.lock();
if(count != 0){
mutex.signal();
return;
}
mutex.signal();
//do stuff
}
You would need an int to count threads in methodA, and ReentrantLock.Condition to signal all threads waiting in methodB once there are no threads in methodA:
AtomicInteger threadsInMethodA = new AtomicInteger(0);
Lock threadsForMethodBLock = new ReentrantLock();
Condition signalWaitingThreadsForMethodB = threadsForMethodBLock.newCondition();
public void methodA() {
threadsInMethodA.incrementAndGet();
//do stuff
if (threadsInMethodA.decrementAndGet() == 0) {
try {
threadsForMethodBLock.lock();
signalWaitingThreadsForMethodB.signalAll();
} finally {
threadsForMethodBLock.unlock();
}
}
}
public void methodB() {
try {
threadsForMethodBLock.lock();
while (!Thread.isInterrupted() && threadsInMethodA.get() != 0) {
try {
signalWaitingThreadsForMethodB.await();
} catch (InterruptedException e) {
Thread.interrupt();
throw new RuntimeException("Not sure if you should continue doing stuff in case of interruption");
}
}
signalWaitingThreadsForMethodB.signalAll();
} finally {
threadsForMethodBLock.unlock();
}
//do stuff
}
So each thread entering methodB will first check if nobody in methodA, and signal previous waiting threads. On the other hand, each thread entering methodA will increment counter to prevent new threads doing work in methodB, and on decrement it will release all the threads waiting to do stuff in methodB if no threads left inside methodA.
I am trying to implement standard Producer Consumer problem using java.
I done some code to do it.
Here is the code:
Producer Class:
class Producer implements Runnable
{
public Producer()
{
new Thread(this,"Producer").start();
}
public synchronized void put()
{
while(Q.valueset)
{
try
{
wait();
}
catch(Exception e)
{
System.out.println(e);
}
}
Q.valueset=true;
Q.i++;
System.out.println("Put:"+Q.i);
notify();
}
public void run()
{
while(true)
{
put();
}
}
}
Consumer class:
class Consumer implements Runnable
{
public Consumer()
{
new Thread(this,"Consumer").start();
}
public synchronized void get()
{
while(!Q.valueset)
{
try
{
wait();
}
catch(Exception e)
{
System.out.println(e);
}
}
Q.valueset=false;
notify();
System.out.println("Get:"+Q.i);
}
public void run()
{
while(true)
{
get();
}
}
}
Another class for Static variables:
class Q
{
static boolean valueset=false;
static int i;
}
I am having one more class which only contains main and creates the instance of Producer And Consumer.
Now when i am trying to run this program it gives following output:
Put:1
put:2
Got:1
Got:2
I am having misconception related to Wait() and notify() that how it works and how object enters and out's from the monitor.i wanted to clear that concept.
Here the problem is also arising due to Wait and notify().
I know this is very basic question related to Multithreading but these these will help me to clear my misconception.
And i also wanted to understand what is the problem in my code.
I already gone through the following link:
producer - consumer multithreading in Java
You need to wait() and notify() on some shared object. What you are doing now by using synchronized is waiting on the respective objects themselves, i.e. the Producer is waiting on the Producer and the Consumer on the Consumer object. You need to wait on something in Q.
From the Javadoc:
notify(): Wakes up a single thread that is waiting on this object's monitor.
wait(): The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
This object's monitor in your case is this, which is your Producer in the put() case and the Consumer in the get() case. But in order for the notify to notify the other Thread, they need to have the same monitor, i.e. they need to wait() on the same object. This object can be e.g. a Object variable in your Q.
To get you started, this is what I mean:
class Q
{
static boolean valueset=false;
static int i;
static Object myLock = new Object();
}
public void put() {
synchronized (Q.myLock) {
while (Q.valueset) {
try {
Q.myLock.wait();
} catch (Exception e) {
System.out.println(e);
}
}
Q.i++; //you forgot this as well
System.out.println("Put:" + Q.i);
Q.valueset = true;
Q.myLock.notify();
}
}
You can fill in the Consumer class yourself...
Put:1
Get:1
Put:2
Get:2
Put:3
Get:3
Put:4
Get:4
This post explains the relationship between notify, wait, and other things you are looking for.
Difference between wait() and sleep()