aspose file tools*
The moose likes Threads and Synchronization and the fly likes Do I have to worry about synchronisation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Do I have to worry about synchronisation?" Watch "Do I have to worry about synchronisation?" New topic
Author

Do I have to worry about synchronisation?

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
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

SCJP, SCWCD, SCBCD, SCEA 5
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

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?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249


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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do I have to worry about synchronisation?