This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Threading related question from khalid mughal's book

Riyazuddin Mohammed
Greenhorn
Posts: 6
Hi All,
I have a doubt regarding the execution sequence and the output of the following program. This review question 9.9 from Khalid mughal's book.

OUTPUT: 23 21 24 13 14 22 11 12

Why is 14 printed before 22 ? I think it should be the other way round.
Please throw some light on this.
riyaz
[ December 27, 2004: Message edited by: Barry Gaunt ]

Barry Gaunt
Ranch Hand
Posts: 7729
I have got the following outputs from this program:

23 24 13 14 21 22 11 12
23 24 13 14 21 11 22 12
23 24 13 21 14 22 11 12
23 24 21 13 14 22 11 12
23 24 21 13 14 11 22 12
23 21 24 13 14 22 11 12 <- I did not get yours

Why do you expect 22 to come before 14?

What was the original question that Mughal asked?

Riyazuddin Mohammed
Greenhorn
Posts: 6
Hi barry,
I do not have a definite answer for you getting different results.Maybe your scheduler uses a different scheduling strategy.

I expected 22 to come before 14. I reasoned like this. The inner createThread() call is evaluated first and will print 23 as the first number(at line 5). Execution of line 5 creates the thread t2 and prints 21(at line 2). Then it is blocked until the main thread completes(line 3). The main thread carries on execution at line 7 and prints 24. Then the blocked thread t2 wakens and 22(from line 4).

So the inner call should print 23 21 24 and 22.

Then the external createThread(10,t2) call is evaluated and should print 13 11 14 and 12 in that order.

So the complete answer should be 23 21 24 22 13 11 14 and 12. But the answer is different.Can anybody point out where I am going wrong?

This was the original question asked at Khalid's book.

Which statements are true about the following code?
public class Joining
{
{
{
public void run()
{
System.out.println(i+1);//2
try
{
t1.join(); //3
}catch(InterruptedException e){}
System.out.println(i+2); //4
}
};
System.out.println(i+3); //5
t2.start(); //6
System.out.println(i+4);//7
return t2;
}

public static void main(String[] args)
{
}
}
a)The first number printed is 13
b)The number 14 is printed before the number 22
c)The number 24 is printed before the number 21
d)The last number printed is 12
e)The number 11 is printed before the number 23

Riyaz

Barry Gaunt
Ranch Hand
Posts: 7729
In your scenario you have said that the thread created by the innermost createThread method can print 21 and then wait to join the main thread. But 14 is printed by the main thread as part of its execution of the "outer" createThread method. So the 14 must be printed before the 22.

Nasty this one

Notice that (c) occurred in all of my runs of the program, but not in yours.
[ December 27, 2004: Message edited by: Barry Gaunt ]

Harsh Singh
Greenhorn
Posts: 13
Threads should run something like this: (Not sure if this will display right)

Main-------23 (start t2)-24-----13----(Start 2nd t2)--14 (Done)
1st t2-----------Starts ---21(Blocked)---------------------------------------(Resumes) 22
2nd t2----------------------------------------Starts-----------11(Blocked)--(Resumes) 12

We can see that Main and first t2 thread are running parallal for a while which would give different combinations of 24 13 14 and 21.
One thing can be concluded for sure that 22 & 12 will print only after 14 is printed. 14 marks the end of the main thread and 22 and 12 are ends of the t2 thread instances.
[ December 27, 2004: Message edited by: Harsh Singh ]

Riyazuddin Mohammed
Greenhorn
Posts: 6
Thanks harsh.