• 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

A hard question

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Another question related to join method:

Select the two correct answers.

1 The first number printed is 13.

2 The number 14 is printed before the number 22.

3 The number 24 is printed before the number 21.

4 The last number printed is 12.

5 The number 11 is printed before the number 23.

A: 2 and 4
why?
Thank you
[ July 19, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Edwin:

public class Joining {
static Thread createThread(final int i, final Thread t1) {
Thread t2 = new Thread() {
public void run() {
System.out.println(i+1);
try {
t1.join();
} catch (InterruptedException e) {
}
System.out.println(i+2);
}
};
System.out.println(i+3);
t2.start();
System.out.println(i+4);
return t2;
}

public static void main(String[] args) {
createThread(10, createThread(20, Thread.currentThread()));
}
}



You see, the currentThread (main thread) is being passed to the createThread method.
Inside createThred method a new thread t2 is created.
The run method of t2 is implemented such as it waits for thread t1 to finish, because of t1.join statement.
The t1 actually is main thread.
T2 will finish last, always.
The last print statement in T2 is 12.

Hope it helps.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a good question:

I think the sequence should be like this:
23 -> (21->14 or 14->21) -> 13 -> (11->14 or 14->11) -> 22 -> 12
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think Thread.join() is covered in SCJP exam. Am I wrong ?
 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic