• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

A hard question

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
I dont think Thread.join() is covered in SCJP exam. Am I wrong ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!