I suggest you ...

Thread Question is wrong!

The following Thread question is wrong:
(following the question -- in question -- I have shown a the class with a brief alteration that show the answer should be "the results can not be predicted")
----------------------------------------------------------
Given the code. What is the result?
public class Cruiser implements Runnable {
public static void main(String[] args) throws InterruptedException {
Thread a = new Thread(new Cruiser());
a.start();

System.out.print("Begin");
a.join();
System.out.print("End");
}

public void run() {
System.out.print("Run");
}
}
A) Compilation fails.
B) An exception is thrown at runtime.
C) "BeginRunEnd" is printed.
D) "BeginEndRun" is printed.
E) "BeginEnd" is printed.
----------------------------------------------------------
//my suggested question change
----------------------------------------------------------

public class Cruiser2 implements Runnable {
static int lc = 100;
//desired effect is in order BeginRunEnd
public static void main(String[] args) throws InterruptedException {

Thread a = new Thread(new Cruiser2());
a.start();

for(int i = 0; i < lc; i++)
System.out.println("Begin Main");

a.join();
for(int i = 0; i < lc; i++)
System.out.println("End");
}

public void run() {
for(int i = 0; i < lc; i++)
System.out.println("Run Thread a");
}
}

25 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    anonymousanonymous shared this idea  ·   ·  Admin →

    8 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Petter HannevoldPetter Hannevold commented  · 

        I agree that the question needs revising (or removal), but your alternative isn't really that good. This basically screams "non predictable behavour", especially when that's an alternative. The question would be way easier this way.
        Another alternative could maybe be to put a small Thread.sleep(100) in the run()-method, and although this isn't a guarantee of the outcome either, it's very very likely. You could reformulate the question to reflect that somehow.

      • maratmarat commented  · 

        Yes, it is not predictable in general, but works sometime)

      • VivekVivek commented  · 

        totally agree

      Feedback and Knowledge Base