• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

run()

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just started learning about threads so apologies if this is obvious to you. From what I have read, there are two main ways of using threads
a) define a class as a subclass of thread, override the run() method and call start() to start a thread (which invokes the overridden run() method)
b) define a class as implementing the runnable interface, define run().
From the example I saw, when you use method b), a timer was used but there was no call to start(). The thread seems to commence when you construct an object of your class. So where is the run() method being called from ?
[ January 02, 2004: Message edited by: Gillian Bladen-Clark ]
 
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
Using method b) something still has to call start(). If you subclass Thread, you can call start() in the constructor of that class to make an auto-starting thread. Anyway, if you see an example where start() is never called, then you probably just need to look a little deeper.
 
Gillian Bladen-Clark
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help, Ernest. I guess it must be something in the Timer class that is calling start() - I shall dig a bit deeper as you suggested.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want a java.util.Timer to execute a job for you, you have to schedule it. The job should be a java.util.TimerTask. The Timer then starts the job at the appropriate moment. If you add more than one TimerTask, they all run in the same Thread.
On the onther hand, the two ways to implement Threads that you learn as a beginner are:
1. extending Thread (call the start method) and
2. implementing Runnable (create a Thread passing the Runnable as a parameter, and call start on that Thread).
This last version has nothing to do with a Timer.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe I've seen the same thing as you have. In my example, I was missing the run() because I was using an applet. In that scenario, it seems, the start(), stop(), and run() methods of the applet over-ride the same methods of the Runnable interface ( I know stop() is deprecated...)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic