Solved

Thread and sleep

Posted on 2004-10-13
15
289 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

12 Experts available now in Live!

Get 1:1 Help Now