Results 1 to 5 of 5
- 08-06-2012, 04:04 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 1
- Rep Power
- 0
Thread synchronization and the synchronized modifier
Hello,
I am currently developing a program that makes use of three Vectors to store connection information. These Vectors are accessed by multiple Threads that modify their data and most importantly, use methods that remove information from one Vector, converts it and then puts it into a different one.
I do know that Vectors are synchronized, but since the methods contain multiple (synchronized) modifications in a row, I decided to synchronize these methods too in order to prevent everything getting mixed up.
What I'm wondering about is:
A) Is it really necessary to synchronize the entire method?
B) Does the synchronized method modifier do anything else except creating a synchronized block with a monitor on this around the content?
C) What is the point of allowing synchronized static methods then?
God, lots of synchronizeds in this post O_o
Edit: Urgh, sorry. I just noticed that there is a section for synchronization. Would be nice if someone could move it there.
- 08-06-2012, 06:14 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Thread synchronization and the synchronized modifier
a) maybe? if you want or if it`s required that the method is executed sequentially one after the other (atomic), then yes.
b) not really
c) you will lock the class and not an object!
Intrinsic Locks and Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)
- 08-07-2012, 10:15 AM #3
Re: Thread synchronization and the synchronized modifier
in the scjp book (p. 744) you can read
the moral here is that just because a class is described as "thread-safe" doesn't mean it is always thread-safe. The individual methods are synchronized, that may not be enough - you may be better off putting in synchronization at a higher level (i.e., put it in the block or method that calls the other methods).
- 08-07-2012, 06:58 PM #4
Re: Thread synchronization and the synchronized modifier
A) Not necessarily. You could also use synchronized blocks:
If you do this, you could use a Collections class instead of a Vector, which is recommended. Vectors are deprecated.Java Code:synchronized(myVector) { // several modifications }
C) synchronized static methods synchronize on the Class object representing the class itself, rather than on an instance object.Get in the habit of using standard Java naming conventions!
- 08-07-2012, 08:17 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Thread synchronization and the synchronized modifier
Just a clarification: Vector is not currently deprecated. But to quote the API from java 7:
The class does contain legacy code, as it was created prior to the Collections framework and adapter later to conform to it.If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.
List Implementations (The Java™ Tutorials > Collections > Implementations)
Similar Threads
-
Thread and Synchronization
By yolo in forum New To JavaReplies: 1Last Post: 04-25-2012, 03:21 AM -
Thread synchronization
By rajanis in forum Threads and SynchronizationReplies: 3Last Post: 02-18-2011, 12:30 PM -
Thread synchronization
By rajanis in forum Threads and SynchronizationReplies: 0Last Post: 01-07-2011, 07:38 AM -
Thread safe without using synchronization
By swetu.vc in forum Threads and SynchronizationReplies: 3Last Post: 01-20-2010, 08:06 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks