Hi Mohammad,
here it deals with three different threads:
1) the main
thread (commented with // #1 and // #2 ),
2) the thread named "thread" and
3) the thread named "thread2", both using an implementer of the Runnable interface.
(both implementer override the run() method, incrementing the static member variable x)
Due to the fact that nearly nothing is predictable concerning thread execution, we can't say which of the above-mentioned thread is executed first.
(or better to say: the main thread is always executed first, but we can't determine when exactly another thread pushes the main thread from the running to runnable state).
Even thread2 - System.out.print(x + " zz "); - can be executed first - it's up to the thread scheduler how to handle the execution orders (besides, I've checked the outputs with eclipse).
So there are several ouput possibilities:
10 (#1) 10 (#2) 11 zz 12 sh
11 sh 11 (#1) 11 (#2) 12 zz
10 (#1) 11 sh 11 (#2) 12 zz
etc.
Please take into account that lines #1 and #2 belong to the main thread, so x is not incremented.
For sake of readability, I've put "#1" and "#2" into the output, too.
Hope this helps!