chevron_left

Problem:

Unfortunately, it giving exceptions pls help me.

Solution:

The issue is, what will awaken this string? In other words, how would you ensure that the other string won't call for?notify() before the main string calls foo.wait()? That is significant on the grounds that the foo item won't recall that it was informed if the tell call happens first. On the off chance that there's just one advice (), and on the off chance that it occurs before the pause(), at that point, the stand by() will stay away forever.

Here's the way pause and tell were intended to be utilized:

private Queue<Product> q = ...;
private Object lock = new Object();

void produceSomething(...) {
    Product p = reallyProduceSomething();
    synchronized(lock) {
        q.add(p);
        lock.notify();
    }
}

void consumeSomething(...) {
    Product p = null;
    synchronized(lock) {
        while (q.peek() == null) {
            lock.wait();
        }
        p = q.remove();
    }
    reallyConsume(p);
}

The main things to note in this model are that there is an unequivocal test for the condition (i.e., q.peek() != invalid), and that no one changes the condition without locking the lock.

On the off chance that the purchaser is called first, at that point it will discover the line unfilled, and it will stand by. There is no second when the maker can sneak in, add a Product to the line, and afterward inform the lock until the buyer is prepared to get that warning.

Then again, in the event that the maker is called first, at that point the shopper is ensured not to call pause().

The circle in the customer is significant for two reasons: One is that, on the off chance that there is more than one buyer string, at that point, it is workable for one shopper to get a notice, yet then another purchaser sneaks in and takes the Product from the line. The lone sensible thing for the clenched hand buyer to do all things considered is stand by again for the following Product. The other explanation that the circle is significant is that the Javadoc says Object. wait() is permitted to return in any event, when the item has not been informed. That is known as a "deceptive wakeup", and the right method to deal with it is to return and stand by once more.

Likewise note: The lock is private and the line is private. That ensures that no other assemblage unit will meddle with the synchronization in this arrangement unit.

Furthermore, note: The lock is an alternate item from the line itself. That ensures that synchronization in this arrangement unit won't meddle with whatever synchronization that the Queue execution does (assuming any).

NOTE: My model re-imagines a wheel to make a statement. In genuine code, you would utilize the put() and take() strategies for an ArrayBlockingQueue which would deal with the entirety of the sitting tight and telling for you.

More Posts

Exception in thread "main" java.lang.noclassdeffounderror: com/google/common/collect/immutablemap Alecxe01 - Nov 18
exception in thread "main" java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver SharadMagar450 - Sep 5
Java exception in thread "main" java.lang.nullpointerexception amna - Jul 13
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException Being human - Jun 4
Exception in thread main java.lang. nullpointerexception amna - May 18
Exception in thread "main" java.lang.noclassdeffounderror Hasnain_khan - Oct 11, 2020
Exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: -1. Farkhanda Athar - Aug 3, 2020
Attempt to invoke virtual method 'int.java.lang.Integer.intValue()' on a null object reference at Cast.writetoParce Sabbir Hossain - Mar 31
Exception in thread "main" java.lang.noclassdeffounderror: com/beust/jcommander/parameterexception Alecxe01 - Nov 11
Exception: java gateway process exited before sending the driver its port number Alecxe01 - Nov 6