Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

JQPlus Q answer is wrong??

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// Question from JQPlus
public class TestRun extends Thread
{
String msg = "default" ;
public TestRun(String s)
{
msg = s;
}
public void run()
{
System.out.println(msg);
}
public static void main(String args[])
{
new TestRun("String1").start();
new TestRun("String2").start();
System.out.println("end");
}
}
/* Answer: Order of output cannot be determined. The JVM decides which threads to run.
I disagree.
When I run this program 20 times it prints "end" first.
The platform is the OS that needs rebooting constantly. */
// PLease can some-one tell confirm 100% that the JQPLUS answer is correct.
 
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

PLease can some-one tell confirm 100% that the JQPLUS answer is correct.


The essence of the Question is: can you 100% be sure about the order of the output ?
No. it is Implementation, machine dependent . And If you are 100 % sure of the order go ahead and mark it on the exam & c what SUN thinks about it.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the answer is correct..
bcoz in windows it
displays
"end"
"String1"
"String2"
so the output is JVM dependend
srinivas
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think unless join() is use for both start() we can't be sure if "end" will be call last. ie
try{
new TestRun("String1").start().join();
new TestRun("String2").start().join();
System.out.println("end");
}catch( ??? forgot which exception (help!!)){
}
Bob

Originally posted by Stephen Batsas:
new TestRun("String1").start();
new TestRun("String2").start();
System.out.println("end");

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephen Batsas:

When I run this program 20 times it prints "end" first.
// PLease can some-one tell confirm 100% that the JQPLUS answer is correct.


Did you run this 20 times on 20 different machines with different operating systems ( Unix, Linux, NT, windows ME etc.) and different JVMs installed ?
What model of multithreading does your machine support? Did you run this on a machine with multiple processors or single processor?
If you ran the program on one machine 20 times, how can you test for platform dependency ?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bob Tai:
I think unless join() is use for both start() we can't be sure if "end" will be call last. ie
try{
new TestRun("String1").start().join();
new TestRun("String2").start().join();
System.out.println("end");
}catch( ??? forgot which exception (help!!)){
}
Bob


Shouldn't that be:

??? ???
It seems to me that this would give a compiler error:

Correct?
Thanks,
Corey
 
I found a beautiful pie. And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic