Solved

Thread and sleep

Posted on 2004-10-13
15
290 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 133
Securing Jmx Console and web console 2 65
Java - Why doesn't this JFrame work 3 44
github account with ecipse 1 42
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

895 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