Solved

Using Thread.notify()

Posted on 2008-10-31
4
466 Views
Last Modified: 2012-05-05
Hi,

I have a background thread which will sleep for 30 seconds at a time. I want to possibly wake it up prematurely from another thread. I thought I could do something like:

    Thread t1 = new Thread() {
        public void run() {
            while (m_run) {
                Thread.sleept(30000);
            }
        }
    }
    t1.start();

   // now main thread continues on, maybe wants to wake the thread up now:
   t1.interrupt();

Is that how you do it? What if the thread is already awake when interrupt() is called, or if it is already exiting sleep() just at the same moment the main thread is calling it? I see you can also do a wait()/notfiy() but then you need an additional object to act as the guard?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 2
  • 2
4 Comments
 

Author Comment

by:DJ_AM_Juicebox
ID: 22853447
To follow up, I may have multiple threads wanting to wake that one background thread up - so a request to wake up prematurely may come from a few different sources -
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 22853694
Hmm so using wait/notify works too, is this the correct usage?
if (m_thread == null) {
    m_waitGuard = new Object();
    
    m_thread = new Thread() {
        public void run() {
            while (m_run) {
                try { 
                    synchronized (m_waitGuard) {  
                        m_waitGuard.wait(30000);
                    }
                }
                catch (InterruptedException ex) {
                    Log.d("**", "I woke up!");
                }
            }
            Log.d("**", "Thread is exiting..");
        }
    };
    m_thread.start();
}
 
 
// Then on some event like a button press, I can prematurely wake the thread up:
public void onBtnPress()
{
    synchronized (m_waitGuard) {
        m_waitGuard.notifyAll();
    }
}

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 22855000
I see you can also do a wait()/notfiy() but then you need an additional object to act as the guard?

That's the way to do it in Java without using the java.util.concurrency package and it can be seen here:

http://technojeeves.com/joomla/index.php/free/61-wait-and-notify-in-java

but you don't *have* to have a separate lock object - it can be done using the objects you already have.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 22896643
:-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse argument 14 61
Why my table column Id is not passed to java object? 4 39
servlet web applications   metadata-complete="true" or false 3 40
Chrome and Firefox Java 5 34
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question