The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A hard question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A hard question" Watch "A hard question" New topic
Author

A hard question

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
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 ]

SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
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.


Devender Thareja
SCEA, SCBCD, SCJP
Byron Foo
Greenhorn

Joined: Jul 20, 2005
Posts: 4
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
Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
I dont think Thread.join() is covered in SCJP exam. Am I wrong ?


SCJP 1.5<br />SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A hard question