• 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: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,

I am not understanding the concept of run() getting overridden while a class is implementing thread. please help me.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

maybe you could be a bit more specific?

class Thread inherits from the interface Runnable (which declares a method run()). The class Thread has a default implementation of the method run(). When you want to do specific actions within your thread, you can override the run() method.

Does this answer your question?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, run() is where you put exactly what your thread should do when it runs.

At the risk of oversimplifying, you might think of run as being like a "main" method for a thread. When a Java program is executed, the JVM automatically invokes main behind the scenes, and this is the entry point for what the program does. In a similar fashion, when a thread is started (by calling start), the thread scheduler automatically invokes run behind the scenes, and this is the "entry point" for what the thread does.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Paul pointed out, Runnable is an interface with only one method: run(). The Thread class implements Runnable.

So you can define your own thread by extending the Thread class (which already implements Runnable) and overriding run. However, the preferred way is to define your own class that implements Runnable, and then create a thread by passing your Runnable to Thread's constructor. This keeps what your thread does (the Runnable) separate from how it does it (the Thread).
 
What are you doing? You are supposed to be reading this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic