• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stop Threads from a Thread that can Reads Keyboard Input  RSS feed

 
agustinus lumbantoruan
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello dear programmers

I am on my progress in implementing my thesis.
My thesis uses thread, that is why i come here to gain guidance and help

So recently I am having a small problem.

Let say I have a total 5 threads running together.
Thread #1 accept keyboard inputs from the users.
The other threads do it's own jobs.
These threads will keep on going round around in a loop.

So if Thread #1 reads the input "Stop" given from the users, i would like the other threads to stop it's job.

Could anyone gives me a guidance where i should look the solution from.

Thank you friends. I hope im not bothering all of you.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agustinus lumbantoruan wrote:
So if Thread #1 reads the input "Stop" given from the users, i would like the other threads to stop it's job.

Could anyone gives me a guidance where i should look the solution from.


Assuming that you don't want the program to end, the correct solution is to do it cooperatively. This means that the other threads needs to have some mechanism where it can be signaled, or where it checks some flag, for this exit condition -- and exits from the run() method. The first thread can then do the flag setting.

There is a stop() method, but it has been deprecated -- so don't use it. The reason it is deprecated is that it can stop a thread (actually, it unrolls the thread stack with the ThreadDeath object) in a way that libraries variables don't get clean up correctly -- even ones that are part of the core library.

Henry
 
agustinus lumbantoruan
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Henry

So the all threads must have a inter thread communication, am i right Henry?
Like using Wait(), Notify() and NotifyAll() and this example http://www.tutorialspoint.com/java/java_thread_communication.htm.

The link above, there is this part "System.out.println("Press Control-C to stop.");"
I am interested at that part. How does it work? it did not mention there.
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wait(), notify(), notifyall() are used when threads have to wait on a condition.
The strategy suggested by Henry is the best way to gracefully stop a thread.
The following post illustrates Henry's point. Hope, it is fairly clear now.
 
suraj august
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a thread which has the code to stop all theads (t1).Put that in wait state when keyboard is used.when it is stooped notify the thead t1 .
when t1 will awake it will notify all threads .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!