?
Solved

Thread and sleep

Posted on 2004-10-13
15
Medium Priority
?
297 Views
Last Modified: 2012-06-27
Hi,

I would like to know how can I put a thread into sleep until I wake up this thread???



Xenia
0
Comment
Question by:xenia27
[X]
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
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 12304823
call wait()

then call notify() to wake it up
0
 

Author Comment

by:xenia27
ID: 12304826
If the thread I wanna put into sleep has another thread inside of it, should I put the insided thread into sleep first??
0
 
LVL 92

Expert Comment

by:objects
ID: 12304837
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 12304839
threads are each independant, one thread does not run inside another.
0
 

Author Comment

by:xenia27
ID: 12304882
why I try to use wait...and I got an exception?? "java.lang.IllegalMonitorStateException: current thread not owner"...how can I make wait() work?
0
 
LVL 92

Expert Comment

by:objects
ID: 12304895
you need to call it from a synchronized method/block

eg.

synchronized (this)
{
   wait();
}

see the tutorial above for more details.
0
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 200 total points
ID: 12305150
> If the thread I wanna put into sleep has another thread inside of it, should I put the insided thread into sleep first??

"Thread with another thread inside of it" makes little sense.  Once threads are spawned, they stand alone.
A parent thread knows of its children, can wait for its children to complete, but it can sleep while its children run.
[In fact "waiting for its children to complete" implies that.]

0
 

Author Comment

by:xenia27
ID: 12305423
OK...I'm confused now...@@...
Here is what I wanna do...I have three threads...mainThread, A_Thread, and B_Thread...
I create A_Thread from mainThread...
so I have this codes like this in the part of my mainThread...
A_Thread = new Thread();  <-- something like this, right?
and A_Thread creates B_Thread....

now, I would like stop A_Thread and B_Thread from mainThread...and wake them up in my mainThread, too...

What should I do?  I try to use wait()...and put the synchronized codes into my A_Thread...and call the method through mainThread, but it seems blocked when I call wait()...What is a better way to code?  Help please~~~~
0
 
LVL 92

Expert Comment

by:objects
ID: 12305672
you need to call wait() from the thread you want to stop.
0
 
LVL 92

Expert Comment

by:objects
ID: 12305674
why does your main thread need to stop the other threads?
0
 
LVL 3

Assisted Solution

by:RuadRauFlessa
RuadRauFlessa earned 200 total points
ID: 12305897
Follow objects wherever he goes and that means into the thread as well. Jip you need to call the wait from the thread that you wish to have waiting. You cannot do it with the main thread and that is propably why you got this:
       java.lang.IllegalMonitorStateException: current thread not owner

What you will need to do is to create a method within your threads that would set a flag to tell your run method to call wait().  This is because you have to have the wait() called as a result of the code within the public void run() method since that is all that is actually threaded. And it it needs to be within the same stack trace as the thread for that specific thread to be suspended.
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 800 total points
ID: 12307082
Hi xenia27

wait() & notify() must be called on a shared object. If you don't specify the object, 'this' is use.

mainThread

  Thread th_a = new ThreadA(); // subclass of Thread
...
  th_a.start();
...
  synchronized(th_a){ th_a.notify(); } // resume thread a


ThreadA:

  public void run()
  {
...
      synchronized(this) { wait(); } // pause the thread until notify is called
...
  }
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12307084
don't forget to catch InterruptedException thrown by the wait() method.
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12336228
xenia27, I am glad to note that you have managed to find a resolution to your problem.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12337049
:-)
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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