0

I have one class, which is instantiated only once. This class has a method, which handles a queue asynchroneously. And this method must be threadsafe. I read about several possibilities and decided to use synchronize as the one to go on with. Either with sync blocks or syncing entire method by usage of the synchronize keyword.

Did I make a proper choice ? If one wants to verify this in detail, I can also provide some code.

icbytes
  • 1,831
  • 1
  • 17
  • 27

2 Answers2

0

Did I make a proper choice ?

Your choice is a good one. Unless you are dealing with a situation where a lot of lock contention is anticipated, it does matter a great deal which of the (correct) approaches that you use.

If one wants to verify this in detail, I can also provide some code.

Of you want someone to check your code, it would be more appropriate to post a Question to the http://codereview.stackexchange.com site.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Ok, seems as if i was right in my decision. Before sync i tried ReentrantLock. So, i will use sync. – icbytes Nov 11 '13 at 18:46
0

Using synchronized block would be more flexible since it can compete for the associated lock of any object, often a member variable.

Try below link : Is there an advantage to use a Synchronized Method instead of a Synchronized Block?

Plz provide the code for more precize answer.

Community
  • 1
  • 1
  • Ok, i will use sync, i got no need to work with any threads or manage any threadpool, i just must ensure, that some lines of code are not processed by one async thread, while the ui thread is processing them and vice versa. – icbytes Nov 11 '13 at 18:54