• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread  RSS feed

 
suman deb
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
How many threads are created when this programm executed?
What would be the right answer? 1 as the main thread. or 0 as we dint call the start method?
Thanks for your help
Suman
class Cardigans extends Thread{

public void run ( ) {
System . out . println ( " In run ! " ) ;
}

public static void main ( String ka [ ] ) {
Thread th = new Cardigans ( ) ;
th . run ( ) ;
}
};

Thanks Guys..

[ July 24, 2008: Message edited by: suman deb ]
[ July 25, 2008: Message edited by: suman deb ]
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably not 0 if the application actually runs. It probably won't the way you have written it; I can see at least one compiler error (the ; at the very end) just looking at it, but runs all right when that is corrected. Please use the code button and use ctrl-c ctrl-v for quoting code.
Please quote where you got this question for, to confirm it is not a "real" exam question.

Tell us how many you think.
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell: believe it or not, but a ; after a class definition is allowed. Therefore, this code will compile.
(In fact, you can also put a ; between methods and fields. No idea why since that should be regarded as an empty statement - and you can't have statements outside classes or outside methods and static / initializer blocks.)

Suman: any Java program that is running will have at least one thread - the main thread. If the program also uses AWT and/or Swing it will also have the Event Dispatcher Thread.
You are right however that your code will not have a second thread - you really need to call start() on a thread to start a new thread. By calling run() instead the thread's code is executed in the current thread.
[ July 24, 2008: Message edited by: Rob Prime ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66152
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the source of this question?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!