• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Put a reference onto a thread

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I'm new to threads and I've written a small application which sends
ping commands to the spefified host! The class responsible for that
is like this one

private class PingMachine extends Thread{

PingMachine(String machine)
{
host = machine;
}

public void run(){
while(true){
try{
Process p = Runtime.getRuntime().exec("ping -t " + host);
InputStream in = p.getInputStream();
InputStreamReader rin = new InputStreamReader(in);
BufferedReader bin = new BufferedReader(rin);
String s;
System.out.println(new Date());
while ((s=bin.readLine()) != null) {
System.out.println(s);
}

}catch(Exception ex){
ex.printStackTrace();
}

}

}

}

For each button click I start a new thread

PingMachine pinger = new PingMachine(hostField.getText().trim());
pinger.start();

What I want to ask is that how can I keep refernces to each newly created thread and destroy it when no londer needed?
Best Regards, Fedai!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can keep references to them the same way you'd keep references to anything else; when you create the PingMachine object, store it into an ArrayList member variable that you keep just for this purpose.

To make the threads destroy-able, you'll need to change them a little bit. Add a boolean member variable to PingMachine, and add it to the loop condition in run -- i.e., run only while it's true. Then add a "stopPinging()" method that sets this variable to false. Also in the run method, after the "while" loop, add a Process.destroy() call to kill the ping process.
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would really help me very much!
But this is what comes out of this

private class PingMachine extends Thread{

private boolean isRunnable = false;

PingMachine(String machine)
{
host = machine;
}

private void startPinging(){
isRunnable = true;
}
private void stopPinging(){
isRunnable = false;
}

public void run(){
while(isRunnable){
try{
Process p = Runtime.getRuntime().exec("ping -t " + host);
InputStream in = p.getInputStream();
InputStreamReader rin = new InputStreamReader(in);
BufferedReader bin = new BufferedReader(rin);
String s;
System.out.println(new Date());
while ((s=bin.readLine()) != null) {
System.out.println(s);
}

}catch(Exception ex){
ex.printStackTrace();
}

}

}

}

And I have two buttons
//On start button
pinger = new PingMachine("localhost");
pinger.start();
//On stop button
//This is the same pinger
pinger.stopPinging();
But when the method "stopPinging()"
have this form
private void stopPinging(){
isRunnable = false;
}
the thread stops
Why assigning "false" to the variable isRunnable
doesnt stop the thread?
Thank you!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you've got two while loops; you need to combine them into one:

while (isRunning && ((s=bin.readLine()) != null)) ...

Also, note that since the variable isRunning() is being read and written by more than one thread, access to it should be synchronized, or it's possible the read loop might not see the changes. Just make stopPinging() synchronized, and create a synchronized isPinging() method that returns the value of the variable and use that in the above -- i.e.,

while (isPinging() && ((s=bin.readLine()) != null)) ...
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That really helped me much!
Thanks!
 
Something about .... going for a swim. With this tiny ad ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic