Solved Some mulithreading help- pausing a thread until another resumes it.

Discussion in 'Plugin Development' started by Tecno_Wizard, Aug 22, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Tecno_Wizard

    I'm making an API that I do not want to leak the details of right now, but I need some multithreading assistance.

    I need to stop the execution of a thread until another thread calls a specific method. How would I do this? Thanks!
     
  2. Offline

    teej107

    @Tecno_Wizard Depends. Are you making the threads by hand or using an executor service?
     
  3. Offline

    CraftCreeper6

  4. Offline

    au2001

    @Tecno_Wizard Maybe Object#wait() and Object#notify() could help you?
     
  5. Offline

    Tecno_Wizard

    Geez that was quick...
    That's what I want to do but i've never done it before. Not asking for a spoonfeed, just an explanation of how to use it.

    One thread is an event from bukkit and one is mine. I want to make my thread wait for the event listener to wake it (Its a nested class so I have fairly easy access between the 2).

    Not really applicable, but thanks.
     
    Last edited: Aug 22, 2015
  6. Offline

    au2001

    @Tecno_Wizard Using obj.wait() will pause the current thread, until you use obj.notify() elsewhere.

    Note: I never used this either, so I'm not absolutely sure how this works...
     
  7. Offline

    Tecno_Wizard

    What confuses me about that is why it is called on the object and not the thread. If someone could clarify that I would have my head set straight on all of this.
     
  8. Offline

    au2001

    @Tecno_Wizard Well, I guess you have to make an Object (it can be any class, since all classes implement Object).
    At this moment, get the object, and use .wait() on it.
    Then, in the method you said, get the object you called the method on above, and call the .notify() method on it.
    The execution of the first thread will continue where it stopped.
     
  9. Offline

    Tecno_Wizard

    @au2001, i'll give it a shot. Thanks!
    So (With horrible syntax)

    Code:
    event e {
    WaitingClass.notify()
    }
    
    waitingClass {
    this.wait()
    }
    And I'm going to be throwing interrupted exceptions. Everyone's favorite! lol. But when a player logs off a player logs off.
     
    Last edited: Aug 22, 2015
  10. Offline

    au2001

    @Tecno_Wizard Well, I guess that would work... Never tried though
     
  11. Offline

    Tecno_Wizard

    @au2001, I'll tell you if it works when I test it later.
     
  12. Offline

    au2001

    @Tecno_Wizard Actually you can use Thread#wait() and Thread#notify()...
     
  13. Offline

    Tecno_Wizard

    @au2001 there is no thread.wait
     
  14. Offline

    au2001

Thread Status:
Not open for further replies.

Share This Page