How can I implement a run() method of thread if I create a Thread Global?
I mean If I create a Thread Globally then can I implement its run() method {” public void run()”} anywhere in my Application?
In the run() method I have to write the code to perform some action.
IF I can do it then please can anyone show me briefly how to do it particularly.
I am a bit confused!!!!!
Thanks,
david
A thread is represented by a Thread object. You create a Thread object as an anonymous inner class or by subclassing Thread with your own class that implements run(). Here is the anonymous version.
Here is the subclass version
Typically you use an anonymous class for a quick and dirty operation and you create a subclass if the thread has a payload (parameters, result etc.)
Note that these snippets just declare the thread. To actually run the thread you must call start():
That’s it. When the thread starts, it invokes the run() on the new thread. The main thread and the new thread run in parallel.
More advanced topics like thread synchronisation should really be tackled when you’ve got the basics worked out.