> The following code produces this output always :
> A.The number 14 is printed before number 22
[quoted text clipped - 3 lines]
> Please explain how the code works to ensure the output behaviour...
> I 've tried hours to understand it but I cannot.
No wonder, with such an unreadable layout...
Anyway, the order of events is as follows:
1. create(20, Thread.currentThread());
2. System.out.println(i + 3); --> 20 + 3 = 23
3. in no particular order: System.out.println(i + 1); and System.out.println(i
+ 4); --> 21 and 24
4. create(10, create(20, Thread.currentThread()));
5. System.out.println(i + 3); --> 10 + 3 = 13
6. in no particular order: System.out.println(i + 1); and System.out.println(i
+ 4); --> 11 and 14
7. the main thread dies, the thread created at (1) wakes up
8. System.out.println(i + 2); --> 20 + 2 = 22
9. the thread created at (1) dies, the thread created at (2) wakes up
10. System.out.println(i + 2); --> 10 + 2 = 12
> //==============CODE
>
[quoted text clipped - 22 lines]
> create(10,create(20,Thread.currentThread
> ())); }}
Patricia Shanahan - 30 Jul 2005 14:27 GMT
>>The following code produces this output always :
>>A.The number 14 is printed before number 22
[quoted text clipped - 19 lines]
> 9. the thread created at (1) dies, the thread created at (2) wakes up
> 10. System.out.println(i + 2); --> 10 + 2 = 12
I agree with this analysis.
Refactoring for clarity, rather than compactness, as well as formatting,
can help with understanding messy code. I haven't fixed the choices of
identifiers, because I wanted to keep the relationship to the existing code.
import java.util.ArrayList;
public class cc {
ArrayList arl = new ArrayList();
boolean flag = false;
static Thread create(final int i, final Thread t1) {
Thread t2 = new Thread(new T2(i,t1));
System.out.println(i + 3);
t2.start();
System.out.println(i + 4);
return t2;
}
static class T2 implements Runnable{
Thread t1;
int i;
T2(int i, Thread t1){
this.t1 = t1;
this.i = i;
}
public void run() {
System.out.println(i + 1);
try {
t1.join();
} catch (InterruptedException e) {
}
System.out.println(i + 2);
}
}
public static void main(String[] args) {
Thread thread0 = Thread.currentThread();
Thread thread1 = create(20,thread0);
Thread thread2 = create(10,thread1);
}
}
Thomas G. Marshall - 30 Jul 2005 16:34 GMT
Patricia Shanahan coughed up:
>>> The following code produces this output always :
>>> A.The number 14 is printed before number 22
[quoted text clipped - 23 lines]
>
> Refactoring for clarity,
Bah. Clarity and K&R brace placement is a conflict in terms.
:-P''''''''
lol
> rather than compactness, as well as
> formatting, can help with understanding messy code. I haven't fixed
[quoted text clipped - 38 lines]
> }
> }

Signature
If I can ever figure out how, I hope that someday I'll
succeed in my lifetime goal of creating a signature
that ends with the word "blarphoogy".
> Please can someone explain me the output of the following code.
> Please explain how the code works to ensure the output behaviour...
> I 've tried hours to understand it but I cannot.
Please rewrite your program to use qualtity (i.e. readable) identifiers.
Then repost on this group. No professional is going to donate/waste his/her
time analysing cryptic code like this. Sorry.