Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calling start twice ...  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,


why not the ouput is :
Inside Run
Exception

thanks .
[ January 29, 2005: Message edited by: rathi ji ]
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I believe this was discussed few months back. I am not able to find the link to that thread.

This is a bug in j2sdk1.4 and has been fixed in tiger release.

I will try to find that thread.. Meanwhile others can share expert comments on this
 
Vincent Brabant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone could explain me why, when I compile that code with JDK 5, I am unable to execute it with JDK 1.4 ?

IMHO, that code is not using new JDK 1.5 features, so ByteCode would work with JDK 1.4.

Am I wrong ?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to compile for JDK 1.4 using a JDK 1.5 compiler, you need to use the -target option when you use javac. The class file format has changed slightly with most majore releases, including 1.5, which means that the class file needs to have a field which specifies which format it's using. If you use the standard 1.5 format, a 1.4 compiler obviously cannot be guaranteed to understand it correctly (even if it could probably understand most of the file correctly), because 1.5 was unknown when 1.4 was created. If you want the class file to be usable by a 1.4 JVM, you need to specify this at compile time.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the original question:

The main thread calls t.start() once. From that moment on, there are two threads which may be active, and we don't know for sure which will happen first. The main thread may remain active, and call t.start() again, which should result in an IllegalStateException being thrown. This has no effect on the new thread though, which would then proceed to run. So the output would be:

Exception
Inside Run

Alternately, after t.start() is called the first time, the new thread may run immediately. After it completes, the main thread may resume and call t.start(0 a second time, which will again result in error. The output in this case would be:

Inside Run
Exception

Either of these is possible, and we have no way of knowing which will happen.

[Jay]: This is a bug in j2sdk1.4 and has been fixed in tiger release.

Mmm, I think you're thinking of the fact that previous JDKs did not correctly throw IllegalThreadStateException, as documented here. On older JDKs you might see only

Inside Run

However this was definitely a bug, and has been fixed now. And it wouldn't really account for the output Rathi is asking about.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Either of these is possible, and we have no way of knowing which will happen."
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is sure that output will not be :

Inside Run
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the exam: no, the output will never be:

Inside Run

However, in the real world, if you use JDK 1.3 or 1.4, you may see output

Inside Run

because JDK 1.3 and 1.4 had a bug which resulted in incorrect behavior. This incorrect behavior will never, ever be on the exam; I only mentioned it because Jay mentioned a bug in 1.4.2, and I think this is what he meant.
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry Jim, I didn't understand, can you repeat that (call start) again?



Mark
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!