Results 1 to 6 of 6
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Creating Blocking Methods

    How do you write a method that blocks until a condition is fulfilled to continue, other than using a while loop to wait for the condition.
    e.g.
    Java Code:
    public void myBlockingMethod(){
         when(condition.isFullfilled());
    }
    I've been googling it, but I haven't been able to find any good articles. Any links/hints/old code is appreciated.

    Thanks,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    public final Object waitObj = new Object();
    
    public void myBlockingMethod() {
        synchronized(waitObj) {
            while (!condition.isFullfilled())
                waitObj.wait(); // throws exception, cba to add it here
        }
    }
    As soon as you full fill the condition, use:
    Java Code:
    synchronized(waitObj) {
        waitObj.notifyAll();
    }

    ~Matt
    I die a little on the inside...
    Every time I get shot.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... was hoping to avoid creating numerous threads, though it may be unavoidable.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I'm sorry, but how would this "condition be fulfilled" if another thread did not fulfill it? Or are you trying to "wait" until an FTP is finished, or something like that?

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    It's a server/client program. I'm trying to create an extension of ServerSocket that allows threads to request sockets from a specified InetAddress, so that they can retrieve new connections by their client. (The Server "talks" to the client through multiple Sockets)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  3. [SOLVED] Blocking Queues - how?
    By sebo in forum New To Java
    Replies: 4
    Last Post: 12-08-2008, 01:12 PM
  4. [SOLVED] Site Blocking
    By Mir in forum Networking
    Replies: 12
    Last Post: 07-03-2008, 07:04 AM
  5. Non Blocking Network
    By mathias in forum Networking
    Replies: 1
    Last Post: 08-07-2007, 07:49 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
  •