Solved

Using Thread.notify()

Posted on 2008-10-31
4
465 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now