Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By jim829

Thread: about lock and wait/signal

  1. #1
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default about lock and wait/signal

    Java Code:
    public int get() { 
            lock.lock();  
            try {  
                while (available == false) {  
                    System.out.println("no balls, wait..");  
                    noBull.await(); // wait();  
                }  
                System.out.println("consumer get" + packOdBalls + " balls");  
                available = false;  
                System.out.println("number consumers holding the lock: "+lock.getHoldCount());  
                fullBull.signal(); // notify();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            } finally {  
                lock.unlock();  
            }  
            
            return packOdBalls;  
        }  
    
    
       public synchronized void put(int packOdBalls) {  
            lock.lock();  
            try {  
                while (available) {  
                    System.out.println("producer!");  
                    fullBull.await(); // wait();  
                }  
                this.packOdBalls = packOdBalls;  
                available = true;  
                System.out.println("producer put " + packOdBalls + " balls");  
                noBull.signal(); // notify();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            } finally {  
                lock.unlock();  
            }  
      
        }
    in my opinion, if the get() thread is waiting due to noBull.await(), since this thread has obtained the lock by lock.lock(); , then no other thread can get the lock, especially the put() thread, then all threads are paused, where I'm wrong?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: about lock and wait/signal

    Can you show us some print out that shows what you are asking about?

    Also please make a small, complete program that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: about lock and wait/signal

    It all depends on what the methods lock(), unlock(), signal() and await() do; afaik, they're not part of the core classes and methods ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: about lock and wait/signal

    Look at the API doc for Interface Condition
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: about lock and wait/signal

    Quote Originally Posted by Norm View Post
    Look at the API doc for Interface Condition
    Oh dear; that interface has been with us since Java 1.5 and I've never even noticed it! I still use the old fashioned wait() and notify[All]() methods in a synchronized block ... I'm a senile old sod ...

    kind regards,

    grampah (<--- Korsakov is my friend ;-)
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: about lock and wait/signal

    I use the Index link at the top of the API doc page to find unknown methods and their classes/packages.
    I'm a senile old sod
    Welcome to the club. Things that used to take an hour now take a week.
    Last edited by Norm; 12-26-2015 at 03:00 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: about lock and wait/signal

    Quote Originally Posted by Norm View Post
    I use the Index link at the top of the API doc page to find unknown methods and the classes/packages.
    Welcome to the club. Things that used to take an hour now take a week.
    ... but that doesn't matter much, because time flies much faster nowadays.

    kind regards,

    Jos (<--- has a clock that goes 'vooooom' ;-)
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: about lock and wait/signal

    I started programming with Java 2 and never noticed that interface either (there are hundreds of core interfaces and classes that I am unfamiliar with). Which is why code snippets are pretty much useless (unless they are totally self contained). If the import statement and the initialization of lock had been provided, we wouldn't be having this discussion.

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Traffic signal light
    By rhymeinkpot in forum Threads and Synchronization
    Replies: 1
    Last Post: 07-31-2011, 04:07 PM
  2. Detect USB port signal
    By mine0926 in forum Advanced Java
    Replies: 4
    Last Post: 01-28-2011, 06:37 AM
  3. Signal processing API.
    By ivanloes in forum New To Java
    Replies: 2
    Last Post: 12-18-2010, 11:22 AM
  4. Using an EMG signal to create a mouse click
    By cmc419 in forum New To Java
    Replies: 1
    Last Post: 03-27-2009, 06:38 PM
  5. WiFi signal strength
    By islamfunny in forum CLDC and MIDP
    Replies: 1
    Last Post: 10-02-2008, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •