Synchronization, When to or not to use?

前端 未结 6 661
后悔当初
后悔当初 2021-01-07 23:35

I have started learning concurrency and threads in Java. I know the basics of synchronized (i.e. what it does). Conceptually I understand that it provides mutually exclusive

相关标签:
6条回答
  • 2021-01-08 00:14

    I wouldn't use synchronized on single threaded code. i.e. where there is no chance an object will be accessed by multiple threads.

    This may appear obvious but ~99% of StringBuffer used in the JDK can only be used by one thread can be replaced with a StringBuilder (which is not synchronized)

    0 讨论(0)
  • 2021-01-08 00:26

    Assuming each thread passes a different array then no synchronization is needed, because the rest of the variables are local.

    If instead you fire off a few threads all calling sortA and passing a reference to the same array, you'd be in trouble without synchronized, because they would interfere with eachother.

    Beware, that it would seem from the example that the getList method returns a new List from an array, such that even if the threads pass the same array, you get different List objects. This is misleading. For example, using Arrays.asList creates a List backed by the given array, but the javadoc clearly states that Changes to the returned list "write through" to the array. so be careful about this.

    0 讨论(0)
  • 2021-01-08 00:27

    Synchronization as you have correctly figured has an impact on the throughput of your application, and can also lead to starving thread.

    All get basically should be non blocking as Collections under concurrency package have implemented.

    As in your example all calling thread will pass there own copy of array, getList doesn't need to be synchronized so is sortA method as all other variables are local.

    Local variables live on stack and every thread has its own stack so other threads cannot interfere with it.

    You need synchronization when you change the state of the Object that other threads should see in an consistent state, if your calls don't change the state of the object you don't need synchronization.

    0 讨论(0)
  • 2021-01-08 00:29

    Your static methods don't depend on any shared state, so need not be synchronized.

    0 讨论(0)
  • 2021-01-08 00:35

    Synchronization is usually needed when you are sharing data between multiple invocations and there is a possibility that the data would be modified resulting in inconsistency. If the data is read-only then you dont need to synchronize.

    In the code snippet above, there is no data that is being shared. The methods work on the input provided and return the output. If multiple threads invoke one of your method, each invocation will have its own input and output. Hence, there is no chance of in-consistency anywhere. So, your methods in the above snippet need not be synchornized.

    Synchronisation, if unnecessarily used, would sure degrade the performance due to the overheads involved and hence should be cautiously used only when required.

    0 讨论(0)
  • 2021-01-08 00:39

    There is no rule defined like when to use synchronized and when not, when you are sure that your code will not be accessed by concurrent threads then you can avoid using synchronised.

    0 讨论(0)
提交回复
热议问题