Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do I have to worry about synchronisation?

 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends

MY interface is sorting of an int array and returning the first highest 5 values as array

If the method just does the arrays.sort and all sorting is done local to the method and there is no instance variable in the implementation(current) class?,

Do I need to worry about synchronization at all.

This method will be used by a computational engine so kindly let me your thoughts method please?

Thanks
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11887
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume the array to be sorted / selected from is actually passed into your method from some other method? If so, what is to stop some other thread from modifying that array while you are working with it?
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I assume the array to be sorted / selected from is actually passed into your method from some other method?
If so, what is to stop some other thread from modifying that array while you are working with it?


Thanks for your reply, So basically do i need to perform syncrhonization based on the passed parameter Is this good enough?

So if my interface is i:


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Farouk wrote:do i need to perform syncrhonization based on the passed parameter


We don't know. Andrew suggested one possible reason why you might have to synchronize, but without knowing more about your overall context, nobody here can say for sure whether it will be necessary.


Is this good enough?

So if my interface is i:




That will be good enough to protect your sort if and only if every single access to that array synchronizes on the array. Remember, syncing doesn't magically stop other threads from "accessing an object" or anything like that. It only stops other threads that are trying to sync on that object from doing so. In other words, syncing is cooperative, and therefore only works when all threads involved play along.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic