Results 1 to 3 of 3
  1. #1
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Pausing Resuming threads from an Monitor thread

    I have a main application. The application starts up multiple infinity looping threads which run independently, but all require an external application to be running to work.

    Currently, I have a while loop in each class that checks that the application is running before doing anything, if its not running, it just keeps looping. I understand this to be highly in efficient. I've been reading up on synchronization and wait() / notify() to handle pausing the threads all together until the condition changes, but this concept just isn't setting in. I'm trying to follow various tutorials, but I'm just not getting it.

    Its my understanding that I should create another thread that monitors the application and sets a lock on an object and notifies that other threads that the object has changed, but I can't figure out how.

    This is where my thought is so far:
    Java Code:
    public class MainProgram {
       Thread monitor = new Thread(new Monitor());
       Thread loop1 = new Thread (new MessageLoop());
    
       monitor.start();
       loop1.start();
    }
    Java Code:
    public class Monitor implements Runable {
    public void run() {
        while(true){
            if(programNotRunning){
                  // notify other threads to sleep
            }
            else {
                  // notifiy threads to run
             }
        }
    }
    Java Code:
    public class MessageLoop(){
        while(true){
             //sleep if notified
    
             // do stuff
        }
    
    }

    Thank you for any help

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

    Default Re: Pausing Resuming threads from an Monitor thread

    First, there may be JDK classes in the java.util.concurrent package that lend themselves to what you want. To find examples of wait and notify
    check out the concurrency section in the Java tutorials (see my signature for a link).

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

  3. #3
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Re: Pausing Resuming threads from an Monitor thread

    Quote Originally Posted by jim829 View Post
    First, there may be JDK classes in the java.util.concurrent package that lend themselves to what you want.
    Even better, just what I needed. Thank you!

Similar Threads

  1. Java thread assigner, remover and monitor advice needed.
    By heatblazer in forum Advanced Java
    Replies: 7
    Last Post: 05-21-2013, 09:56 AM
  2. Pausing a method till Threads finish
    By Sutured in forum New To Java
    Replies: 0
    Last Post: 03-27-2013, 06:40 PM
  3. Replies: 1
    Last Post: 10-26-2011, 11:46 PM
  4. Pausing a Thread
    By Lord Voldemort in forum New To Java
    Replies: 14
    Last Post: 05-22-2011, 04:45 AM
  5. how to wrk with twa threads then compile both to 1 thread
    By dmotah in forum Threads and Synchronization
    Replies: 0
    Last Post: 02-04-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
  •