Results 1 to 5 of 5
  1. #1
    Anza Power's Avatar
    Anza Power is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default How to create a block method?

    I want to implement something that looks like this:

    Java Code:
    while(true){
    	waitForSignal();
    	//do the code
    }
    How do I make the waitForSignal() method? I want that method to block the current thread until another method calls a certain method to give it the signal (namely, a button being pressed in the GUI app)

    Is there a good up-to-date book/tutorial you'd recommend for getting good control of java concurrency?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: How to create a block method?

    Are you familiar with the wait/notify methods? Guarded Blocks (The Java™ Tutorials > Essential Classes > Concurrency)

  3. #3
    Anza Power's Avatar
    Anza Power is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to create a block method?

    ^ I came across that page while doing the search on my own before posting, it kept failing for me and giving me an IllegalMonitorStateException, silly me that was because I was calling the notify() method that belonged to the anonymous ActionListener class attached to the button and not the main class of the program on which I invoke the wait() method...

    Thanks for your help...:)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to create a block method?

    A wait or notifty(All) call on an object only works if the caller holds the 'monitor' for that object; so:

    Java Code:
    // in one thread:
    synchronized(anObject) { // get the monitor of anObject
       while (!aCondition()) anObject.wait(); // wait on anObject
    }
    
    // while in another thread:
    synchronize(anObject) { // get the monitor on the same 'anObject'
       // make aCondition return true
       anObject.notifyAll(); // wake up all waiting threads
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Anza Power's Avatar
    Anza Power is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to create a block method?

    ^ Yeah I know, my problem was that I wrote it like this:

    Java Code:
    myButton.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent ae){
    		startExecution = true;
    		notifyAll();
    	}
    }
    I didn't notice that I was calling the notifyAll() method that belonged to the ActionListener object and not the containing object on which I call the wait() method...

Similar Threads

  1. calling yield() method in synchronized block
    By Ash-infinity in forum New To Java
    Replies: 2
    Last Post: 12-04-2012, 05:35 PM
  2. Replies: 3
    Last Post: 08-21-2011, 02:25 AM
  3. Replies: 6
    Last Post: 02-12-2011, 06:56 AM
  4. When I create method...
    By isuru in forum AWT / Swing
    Replies: 6
    Last Post: 05-13-2010, 09:42 PM
  5. Modify/create AVI's INFO BLOCK
    By Agus211 in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 03:20 PM

Posting Permissions

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