Hey friends , i have this problem with Thread Synchronization. please look at the following class where i created two threads by implementing Runnable interface. in here i can only get the results ABAB, Because
I am using two different threads with two different runnable targets. Am i correct. I only want to know my theory is correct? please somebody tell me.
and The second class , I can get two results as "ABAB" , and "AABB" . am i right? It is because i am using the same runnable target for different Threads . Is my theory correct. or please somebody tell me if the way i understand is wrong. and please forgive my bad use of English.
It is just the opposite. First example uses two different Runnable targets for the two threads. These two threads can concurrently execute the run() method. Hence, the output can be AABB or ABAB.
The second example uses the same Runnable object while creating two threads. The 'synchronized' keyword actually comes into play here.Once a thread is inside the run() method, the other thread has to wait for the former to complete. Therefore, the output has to be ABAB.
Joined: Feb 04, 2011
Wow, I got it now. Thanks a lot Sebanti Sanyal for your Short and Sweet answer. You are awesome