Using Thread.notify()

Posted on 2008-10-31
Last Modified: 2012-05-05

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) {

   // now main thread continues on, maybe wants to wake the thread up now:

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?

Question by:DJ_AM_Juicebox
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Author Comment

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 -

Author Comment

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) {  
                catch (InterruptedException ex) {
                    Log.d("**", "I woke up!");
            Log.d("**", "Thread is exiting..");
// Then on some event like a button press, I can prematurely wake the thread up:
public void onBtnPress()
    synchronized (m_waitGuard) {

Open in new window

LVL 86

Accepted Solution

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:

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

Expert Comment

ID: 22896643

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 68
Running JavaFX on the Raspberry Pi 27 299
Netbeans and org.apache.commons.lang3 issue 3 55
Firefox and firebub 5 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

759 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