Solved

Thread and sleep

Posted on 2004-10-13
15
292 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 54
nextBoolean(double p) for Random class 3 40
Why my table column Id is not passed to java object? 4 38
how to install java on RHEL image on EC2 4 26
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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 …

776 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