Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning Java, use of synchronized keyword

so i was testing with synchronized keyword. Here is an example that I tried:

public class MyTest {     static int i = 0;     public static void main(String[] args) {         new Thread(t1).start();         new Thread(t2).start();     }      private static void countMe(String name){         i++;         System.out.println("Current Counter is: " + i + ", updated by: " + name);     }      private static Runnable t1 = new Runnable() {         public void run() {             try{                 for(int i=0; i<5; i++){                     countMe("t1");                 }             } catch (Exception e){}          }     };      private static Runnable t2 = new Runnable() {         public void run() {             try{                 for(int i=0; i<5; i++){                     countMe("t2");                 }             } catch (Exception e){}        }     }; }   

When I run it, the output of calling countMe() method from two threads generates this output:

Current Counter is: 1 Current Counter is: 2 Current Counter is: 4 Current Counter is: 5 Current Counter is: 6 Current Counter is: 7 Current Counter is: 3 Current Counter is: 8 Current Counter is: 9 Current Counter is: 10   

And when I change the method countMe() to:

private synchronized static void countMe(){         i++;         System.out.println("Current Counter is: " + i); }   

I get this output:

Current Counter is: 1 Current Counter is: 2 Current Counter is: 3 Current Counter is: 4 Current Counter is: 5 Current Counter is: 6 Current Counter is: 7 Current Counter is: 8 Current Counter is: 9 Current Counter is: 10   

Although this gives me clear understanding the purpose of synchronized, I want to know is there any other reason as well, that we can use synchronized. Or what I have done here, is the only eason why we need the use of this synchronized keyword?

Thanks.

EDIT: Another thing that I am confused with is that in first output why the counter went to 3 after 7. It seems a bit impossible to me, but similar results do happen every time I try, is this normal?

like image 516
911TurboS Avatar asked Feb 08 '12 15:02

911TurboS


People also ask

What are the uses of synchronized keyword in Java?

1. Synchronized keyword in Java ensures that only a single thread can access shared data at a time. 2. Using Java synchronized keyword, we can only make a block or a method as synchronized.

Why we use synchronized keyword in Java write a program to illustrate the same?

To avoid such issues, Java provides us with the synchronized keyword, which acts like a lock to a particular resource. This helps achieve communication between threads such that only one thread accesses the synchronized resource and other threads wait for the resource to become free.

Why do we need synchronized?

We need to synchronize the shared resources to ensure that at a time only one thread is able to access the shared resource. If an Object is shared by multiple threads then there is need of synchronization in order to avoid the Object's state to be getting corrupted. Synchronization is needed when Object is mutable.

What is synchronized keyword in what situations you will use it?

Answer : Synchronization is the act of serializing access to critical sections of code. We will use this keyword when we expect multiple threads to access/modify the same data. To understand synchronization we need to look into thread execution manner.


Video Answer


1 Answers

Two things:

First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. (An important exception: final fields, which cannot be modified after the object is constructed, can be safely read through non-synchronized methods, once the object is constructed).

source: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

like image 119
vulkanino Avatar answered Sep 22 '22 02:09

vulkanino