aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about the join method in Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about the join method in Thread" Watch "Question about the join method in Thread" New topic
Author

Question about the join method in Thread

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, I worked on this question from the practice exam:

public class Checkout2 implements Runnable {
void doStuff() { }
synchronized void doSynch() {
try { Thread.sleep(1000); }
catch (Exception e) { System.out.print("e "); }
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
new Thread(new Checkout2()).start();
Thread t1 = new Thread(new Checkout2());
System.out.println("What is t1? "+ t1.getId());
t1.start();
try { t1.join(); }
catch (Exception e) { System.out.print("e "); }
System.out.println("elapsed: "
+ (System.currentTimeMillis() - start));
}
public void run() {
for(int j = 0; j < 4; j++) {
doStuff();
System.out.println(Thread.currentThread().getId());
try { Thread.sleep(1000); }
catch (Exception e) { System.out.print("e "); }
doSynch();
} } }

Which are true? (Choose all that apply.)
A. Compilation fails.
B. Elapsed time will be about eight seconds.
C. Elapsed time will be about nine seconds.
D. Elapsed time will be about 12 seconds.
E. Changing doSynch() to be unsynchronized will change elapsed by only a few
milliseconds.
F. Changing doSynch() to be unsynchronized will change elapsed by 450 or more
milliseconds.

The answer is :

Answer (for Objective 4.3):
B and E are correct. Even when doSynch() is synchronized, the two run() invocations
aren’t running against the same Checkout2 object. This code creates two distinct
Checkout2 objects, so there is no synchronization.
A, C, D, and F are incorrect based on the above.

My interpretation is that t1.join means the t1 thread will execute at a certain time and the CPU allocate its resources for t1 until it is done and the other thread will wait until t1 is done.
Therefore, I think the time elapse can be 8 second/thread * 2 thread = 16 seconds. Think of a scenario that t1 execute first and it takes 8 seconds and the other thread execute next and takes another 8 second.
I thought the answer B is not guaranteed.

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I thought about this question myself for a while. I guess the two threads can sleep at the same time and wake up at the same or more or less the same time.
Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27
t1.join() means current thread will join after t1 completes, and current thread is the main thread, no the other thread Checkout2.

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

How can you make t1 join onto the other thread? (assume the other thread is declared as t2)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about the join method in Thread
 
Similar Threads
A thread problem
a question about thread
Threads and Synchronization examples
Threads : calculating minimum time, does the SCJP have such questions?
Synchronizing between two threads, one implements Runnable the other extends Thread