• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronization  RSS feed

 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class syn extends Thread
{
StringBuffer letter;

public syn(StringBuffer letter)
{
this.letter=letter;
}

public syn(String name)
{
}

public synchronized void run()
{
System.out.println(getName());
for(int i=1;i<=2;i++)
{
System.out.println(letter);

}
System.out.println();

}

public static void main(String[]arf)
{
StringBuffer s= new StringBuffer("A");
syn a=new syn(s);
a.setName("Well");
a.start();


syn b= new syn(s);
b.setName("done");
b.start();

syn c = new syn(s);
c.setName("it");
c.start();
}
}
Output
Well
A
A

done
A
A

it
A
A


if i remove the synchronized keyword then also i am able to get the same result....I am basically very weak in thread.Somebody kindly help to improve on the concept of thread and synchronization by explaining this.
It will be great if you provide me with some link where i can get a good idea of threads and other concepts of threads.

Thanks in advance
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just because you are looping it twice . If you really want to see difference loop it around 100 or more times ...

If the code is not clear then reply , because I can help you ...
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rathi

When i changed it to 100 i am getting a different output from what i have said above.But my doubt is whether i use or not have used the synchronized keyword i am getting the same output.

Then what is the purpose of synchronizing the method.

When you synchronize the method only one thread will own the lock and the lock is released when that thread exit the synchronized method.....so accordingly the first thread should complete then only other thread can enter the method when the method is synchronized,but this is not happening.
Before the thread "Well" gets completed "done" thread is started and then immediately "it" is started.

hope i have made my question clear to you.If i am wrong anywhere please correct me.

Thanks
[ February 14, 2005: Message edited by: ramya jp ]
 
Subhash Bhushan C
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most important thing that you have to observe here, is the object on which the thread has acquired the lock.

In your program, you have 3 objects created. Each has a synchronized method of its own. But the important point here is that only one thread, at any point of time, will try to access the run method of that object.

In other words, if you have created ObjectA with a synchronized run method, and called its start(), then the thread which is created, will be the only one to rally for the run method. There is no other thread, which will try to access the same object's run method. All other threads have their own objects to run, in your program.

Here's an example, where different threads rally for the same object's method:


In the above program, remove the synchronized keyword for the run method, and observe how the output changes.

So, all threads locking on the same object(here, its the synObj) can enter a synchronized method on that object only in turns.

Hope this clarifies your doubt.
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Agani that was a good explanation.
As rathi said when i loop it for 5 times i am getting the same result even if i remove the synchronized key word but when i loop it for 10 or more times i am able to make out the difference of using and not using synchronized keyword.Why this is happening.
Thanks in advance.
 
Subhash Bhushan C
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads just work that way.

Java does not guarantee the sequence of execution of threads. Any thread from the runnable thread pool, may jump into execution. They might follow a neat round robin league structure, or there may be utter mayhem, with one thread running the show. Again, nothing is guaranteed about the order of thread execution in Java.

As you increase the number of loops, you will always see a difference between the outputs of successive runs. In fact, with the same number of loops (sufficiently large number), you will see output variations for the same program in successive runs.

So, you get different outputs because of the random order of execution of threads.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Let me explain this Agni please ...

Every object in Java has built-in lock facility , but it comes to play when it has synchronized code .

This is one object & has a lock that is free now .


This line of code is creating 4 threads for synObj object , it means they will execute run() method for that object when you will invoke start() method on thread .
.



This line of code is starting the threads , means they will execute run() method of Synchronizer object's . But because run() method is synchronized so thread needs lock before entering . First thread has got lock , it is executing run() method , at this time if second thread will try to aquire lock , it will not be possible because the lock of synObj's is in the hand of first thread . So they will execute in a sequence they will get lock .
[ February 14, 2005: Message edited by: rathi ji ]
 
Subhash Bhushan C
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This line of code is creating 4 threads for synObj object , it means they will execute run() method for that object.


Actually, no run() method will get executed. Just 4 plain threads are being initialized. And the Synchronizer object is being passed as the (Runnable target) parameter. 4 thread objects are created, and they are all in the "new" state.


This line of code is starting the thread , means they will execute run() method of Synchronizer object's .


Each thread, once the start() method is called on it, moves from the "new" state to the "runnable" state, and settles in the thread pool. This thread, once it reaches the runnable state, may be called in for execution at any time.... from the JVM. The order or sequence is not guaranteed. It may even happen that the Thread-4 may execute before Thread-1.
[ February 14, 2005: Message edited by: Agni Vartula ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!