Solved

Thread and sleep

Posted on 2004-10-13
15
296 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 200 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 50 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 50 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 200 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 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…

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