Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

what is wrong in this program involving thread  RSS feed

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!