• 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

Two similar questions about Thread in two mock exams

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would anyone shed a light on the following question? The answer given is B, but I think it should be C, considering the uncertainty of thread.

class A implements Runnable{
public int i=1;
public void run(){
this.i=10;
}
}
public class Test{
public static void main(String args){
A a=new A();
new Thread(a).start();
int j=a.i;
12)
}
}
what is the value of j at line 13? B
A. 1
B. 10
C. the value of j cannot be determined
D. An error at line 11 cause compilation to fail

A similar version of this question also appears in another mock exam as follows. The answer (C) to this question proves my idea to some extent, but
there is a difference in this question: a "thread.sleep(5000);" statement is posted in class A. I was wondering if this light difference will make the program running differently?

class A implements Runnable (
int i;
public void run () (
try (
thread.sleep(5000);
i= 10;
) catch(InterruptedException e) {}
)
)

public class Test {
public static void main (string args[]) (
try (
A a = new A ();
Thread t = new Thread (a);
t.start();
int j= a.i;
) catch (Exception e) {}
)
)

Which statement al line 17 will ensure that j=10 at line 19?
A. a.wait();
B. t.wait();
C. t.join();
D. t.yield();
E. t.notify();
F. a.notify();
G. t.interrupt();

Answer: C
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger, would you please change the order of your displayed name to "Roger Moon". The JavaRanch naming convention is <first or given name><space><family name>.

Thanks
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, in your first example, the argument to main should have type String[] not String.
Second you use a label 12) in the code, but the question refers to a line 13.

Running the corrected code, printing the value of j at line 12 (or 13), results in at least one case of 1 being printed, so the answer given is clearly wrong.

You cannot predict that the started thread will even execute run() before the main thread gets to line 12/13. However, it could do, sometime, however unlikely, depending on the JVM thread scheduling algorithm. The started thread is not a daemon thread so it will run to completion and the value of i will be set to 10 - sometime. Whether the main thread will "see" it with that value is unlikely, but not impossible.


The answer to the second example is correct when it says use join to cause the main thread to wait indefinitely for the started thread to complete.

(BTW: The second example is also a mess of syntax errors.)
[ July 24, 2005: Message edited by: Barry Gaunt ]
 
roger moon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Barry, thanks for reminding, I have changed the order.
 
roger moon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry, thanks again for your answer. Yes, these two questions are full of mess. I copied them directly from the mock exam without correction.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a good question to highlight Roger. I had to have a guilty look to check it was not one of my questions (it is not). In some of my early mock exams I had code that did not acknowledge that that the output of some types thread code is not determinable from reading it. It is important to understand that no matter how many times a piece of thread code generates a particular output on your computer or 1000 other computers it is no guarantee it will always generate that output. Threading is platform and implementation dependent.

Marcus
 
roger moon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus, thanks for your comments.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic