This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is wrong in this program involving thread  RSS feed

 
pras
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??
 
Bill Shirley
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
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
 
Jorge Delgado
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!