Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Java Threading Question (http://www.chiefdelphi.com/forums/showthread.php?t=143514)

seg9585 09-02-2016 16:15

Java Threading Question
 
Question to those of you who use Threading (class MyClass extends Thread implements Runnable) for various scripting:

We wrote a Thread class that carries out a script upon button press (in our case, performing about 5 mechanical timed functions in sequence to shoot our boulder). The script itself is in our void run(){} method and gets invoked upon callout of our shooterclass.start();
The script in run() is not an infinite loop -- it would merely end and I would *think* the thread itself would terminate.

So, when the script is complete and sometime in the future I press the same button, calling out MyClass.start(); a second time throws an exception. The only way I've gotten around this was to generate a new Thread (shooterclass = new MyClass(); ) before calling out shooterclass.start(); again.
Even though this works, I'm worried I'm just creating a ton of new threads that will bog down the roboRio eventually. What is the correct way to terminate a Thread? the API says Thread.stop() is deprecated
Should I just assume the thread is always running, allowing me to specifically call out shooterclass.run() in the 2nd iteration and beyond?

Jared 09-02-2016 16:45

Re: Java Threading Question
 
What you have now (creating a new thread each time) is the right thing to do.
Your thread will stop once your run method returns, so you don't need to worry about cleaning up after it. As you've seen, you can't start it again, and you need to make a new thread.

seg9585 09-02-2016 23:52

Re: Java Threading Question
 
Excellent, thanks for the feedback! Will keep it as is.


All times are GMT -5. The time now is 00:43.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi