• Post Reply Bookmark Topic Watch Topic
  • New Topic

Really simple threading question  RSS feed

 
Sam Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried to understand threading from ten different tutorials, but it makes my head spin because all the tutorials include so much extra stuff that I don't know what's important.

I've got a piece of code here with a gui, which contains a progress bar. I'd like the progress bar to update in a timely fashion, but not take up much of the main thread's recources.

I've cut the code down to it's barest minimum. I'd really appreciate it someone were to tell me how to edit this code so that it implements threading? Thanks so much!


[ June 09, 2005: Message edited by: Sam Smith ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, crudely:

- Make EvolutionEngine extend Thread.
- Rename your "start()" method to "run()".
- After calling setGUI, call engine.start();

Note this last step is not a typo. Thread has a magic method named "start()" which arranges to have the Thread's run() method called from a different thread of execution.
 
Sam Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the progress bar doesn't need to be in its own thread? Or, when calling engine.start() it creates two threads automatically?

Either way, that's all I have to do? Wow, tutorials make things so complicated.

I can't test it now, have to run, but assuming it works, thanks very much.
 
Sam Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey wait, that is all it took!

Thanks!
 
Sam Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait, not all is peachy.

The run() method works great, and threads well with the progress bar.

However, I can't seem to start it up again after the run() method has completed (i.e. pressing "run" on my gui, which calls engine.start() does nothing).

The method run() has definately quit, and, as a test, I threw in a stop() command for good measure. Nothing.

When I switch the threaded method for my original method, everything works fine (except it doesn't thread), so it's obviously something to do with the thread.

Any suggestions?

Thanks!
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thread cannot be restarted using start(), once it's stopped using stop() or the run() has completed. To have it restarted, create a new instance of that thread and call start() on it. Incidentally, the stop() method was deprecated as "inherently unsafe", so don't use it. Instead, have you run() check for some condition. Once that condition is met, the run should complete and the thread will die its natural death.

For example:

[ June 09, 2005: Message edited by: John Smith ]
 
Sam Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'd have to create a brand-new instance of the class?

Hmmm... that doesn't sound possible. That class is the very first thing that gets created - everything else depends on it being around.

Would it still work if I switched the thread class to being the gui in my example above, instead of the EvolutionEngine as was suggested?

If not, I guess I can live with just running the experiment once, and restarting the program if I need to run another... Actually, that's a question that I've never wondered about: how can I restart the program from within it? Can I just call MeGA.main(String[] args) from within some random method?
[ June 09, 2005: Message edited by: Sam Smith ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of making Evolution extend Thread you can make it implement Runnable. The code inside probably won't change one bit, but we can start things a little differently and keep one Evolution around all day.

Note that we didn't try to restart a Thread. You found that won't work. Instead we ran a brand new thread with the same Evolution.

See if that will work for you. If not, post fresh code and we'll try again.
[ June 09, 2005: Message edited by: Stan James ]
 
Sam Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Instead of making Evolution extend Thread you can make it implement Runnable. The code inside probably won't change one bit, but we can start things a little differently and keep one Evolution around all day.

Note that we didn't try to restart a Thread. You found that won't work. Instead we ran a brand new thread with the same Evolution.

See if that will work for you. If not, post fresh code and we'll try again.

[ June 09, 2005: Message edited by: Stan James ]



Yayy!!!

No more questions about this, I prormise. Thanks very much all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!