• 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

what is wrong in this program involving thread

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class YouThread extends Thread {

public void run(){
for(int i=5; i>0;i++){
System.out.println(i);
// Thread.sleep(1000);
}
}

public static void main(String args[]) {

YouThread yT=new YouThread();
yT.start();
}
}

/*********what is wrong?why is'nt the new thread starting up?*********/
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey i Wrote a java File and named it Thread.java and to my surprise it compiled and executed?? how can it be possible? because when we have a inbuilt Thread class it shouldn't have allowed this right??
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prasanna sheregar:
Hey i Wrote a java File and named it Thread.java and to my surprise it compiled and executed?? how can it be possible? because when we have a inbuilt Thread class it shouldn't have allowed this right??



the Java Thread class is in java.lang

you likely created a class in a custom package or the default package
 
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
As shown, this program will print about 2 billion integers, each on its own line, and then stop. What did you expect to happen?
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
the problem was i had earlier written a Thread.java file in the default package so when i extended my program it extended from the Thread File which i developed instead of extending from java.lang.Thread


So how does the Java system decided to Extend from my developed Thread class instead of java.lang .Thread

suppose i create a new dummy Thread class in the default package and write one more class which extends Thread . Now which one will Java system take
1> the one which i wrote
2> java.lang.Thread
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasanna ... Thread is not a reserved word for Java , then it's possible to create a class named Thread .. Why is YouThread extending from your class Thread ? Because you put it in the same package where YouThread.java is and the classes in the package (default or others ) have precedence over other class sources..including the API.

Then , YouThread finds Thread in the same package and extends it . If you move your class Thread to other location your program will compile and run well . I tested it ... by the way , check out your for cicle .. it will print a lot of numbers as Mr. Friedman-Hill said ..


regards Jorge
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic