• 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

scjp - Threads

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Java2 cert guide by kathy sirra.
in one of the test questions on threads, the code below was given

public class TestThread extends Thread {
public static void main(String [] args) {
TestThread t = new TestThread ();
t.start();
System.out.println("1");
t.start();
System.out.println("2");
}

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


The answer given was that An Exception occurs at runtime.
but on running it, it gave the following result... 1 Stack 2.
Pls can someone explain this to me.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code doesn't look like it woulde error but the output can not be determined.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should have gotten an IllegalThreadStateException. You can get some other output first like "1" or "Stack".

I tested the code repeatedly and got those results.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have got the same code to error on some runs and complete on others.

I think the point is that if the second start() is run on t while the previous thread is still running, it will error.

I tried adding for(int x=0; x < 10000000;x++); just before the second start() and the code runs sucessfully repeatedly. I presume the delay in the loop gives the initial thread time to end.

Cheers,

Si.
 
Kris Reid
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DOE! I should have picked that....IllegalThreadStateException for sure!
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output will be either:
1 2 Stack
Stack 1 2
1 Stack 2
1 Stack IllegalThreadStateException
Stack 1 IllegalThreadStateException
1 IllegalThreadStateException


I tried adding for(int x=0; x < 10000000;x++); just before the second start() and the code runs sucessfully repeatedly. I presume the delay in the loop gives the initial thread time to end.


This will just choke the VM executing the main thread, and the thread you have assumed to die probably (assuming optimal thread scheduler behaviour) will not execute at all. You might want to join() which will guarantee one of the three possibilities where IllegalThreadStateException occurs.

Moral of the story, never believe tech. books; they contain too many erroneous claims. Always refer to the relevant specification.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
Check out this link
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moral of the story, never believe tech. books; they contain too many erroneous claims. Always refer to the relevant specification.


I take a different lesson from this story. While try-it-and-see is good enough for SCJP prep, it is much better to refer to the JLS or API doc if the code is going into production.

There is always the chance that your code works only because of a system software bug and the bug will be fixed in the next release. I have seen the JLS changed to match the compiler implementation, but that is less frequent than compiler changes to match the JLS.

In this case, the Java thread code will be changed to match the API doc - the second invocation of start() on the same Runnable object will always cause an IllegalThreadStateException.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this case, the Java thread code will be changed to match the API doc - the second invocation of start() on the same Runnable object will always cause an IllegalThreadStateException.



The same Runnable object can be used multiple times for the starting of a Thread. I assume you mean "Thread", not "Runnable".
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic