• 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

Simple Thread Question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between using the class object(line 1) and the Thread object(line 2) when executing start?
public class Borley extends Thread{
public static void main(String argv[]){
Borley b = new Borley(); //1
//Thread b = new Borley(); //2
b.start();
}
public void run(){
System.out.println("Running");
}
}
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you define:
Borley b = new Borley (...);
you may access every visible method (depending on priv/prot/pub/default) of Borley.
But if you say:
Thread t = new Borley (...);
your t 'sees' only the subset of Borley, which is a Thread.
This protects the user of doing more with this 't', as intended, and allows you, to put the 't' to every method, which expects a Thread (and never heard of Borley before).
 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic