Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

Thread and sleep

Hi,

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



Xenia
0
xenia27
Asked:
xenia27
  • 6
  • 3
  • 3
  • +2
4 Solutions
 
objectsCommented:
call wait()

then call notify() to wake it up
0
 
xenia27Author Commented:
If the thread I wanna put into sleep has another thread inside of it, should I put the insided thread into sleep first??
0
 
objectsCommented:
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
threads are each independant, one thread does not run inside another.
0
 
xenia27Author Commented:
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
 
objectsCommented:
you need to call it from a synchronized method/block

eg.

synchronized (this)
{
   wait();
}

see the tutorial above for more details.
0
 
brettmjohnsonCommented:
> 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
 
xenia27Author Commented:
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
 
objectsCommented:
you need to call wait() from the thread you want to stop.
0
 
objectsCommented:
why does your main thread need to stop the other threads?
0
 
RuadRauFlessaCommented:
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
 
WebstormCommented:
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
 
WebstormCommented:
don't forget to catch InterruptedException thrown by the wait() method.
0
 
RuadRauFlessaCommented:
xenia27, I am glad to note that you have managed to find a resolution to your problem.
0
 
WebstormCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now