• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 1.6 and concurrency

 
William Smith II
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of questions:

(1) If I use the latest JDK 1.6 u3, do I have to use the concurrency package? I am using standard wait/notify and synchronized blocks. I just wanted to use the most recent JDK.

(2) If I use JDK 1.6, will I lose marks if I don't use some of the new constructs? For example, what if I pass back String[] from a method instead of ArrayList<String>?

(3) This doesn't have to do with 1.6. I use a HashSet for my locking manager. So long as I use syncronized blocks on the objects, what's the difference between these two statements. I've read the API, but I'm not 100% I understand. I think I'm okay so long as I use syncronized blocks on the HashSet object (using hs1).
HashSet hs1 = new HashSet();
HashSet hs2 = Collections.synchronizedSet(new HashSet());
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William,

My answers below

Originally posted by William Smith II:
A couple of questions:
(1) If I use the latest JDK 1.6 u3, do I have to use the concurrency package? I am using standard wait/notify and synchronized blocks. I just wanted to use the most recent JDK.


No, synchronized blocks are not deprecated or outdated. The java.util.conurrency package simply offers additional functionality rather than replace the old way of adding thread synchronization. Whether or not you prefer using Lock over synchronized blocks is more a matter of style than anything else (provided you dont need the additional functionality offered by the Lock classes).

Originally posted by William Smith II:

(2) If I use JDK 1.6, will I lose marks if I don't use some of the new constructs? For example, what if I pass back String[] from a method instead of ArrayList<String>?


I can imagine you may lose marks for choosing a sub optimal way of returning a list of values. But keep in mind that your two options aren't directly related to moving to 1.6 (or more accurately, 1.5 in the case of generics). The change would be from List to List<String>. If you're worried about what would be the best approach you can always start a new thread with that specific question as a topic.

Originally posted by William Smith II:

(3) This doesn't have to do with 1.6. I use a HashSet for my locking manager. So long as I use syncronized blocks on the objects, what's the difference between these two statements. I've read the API, but I'm not 100% I understand. I think I'm okay so long as I use syncronized blocks on the HashSet object (using hs1).
HashSet hs1 = new HashSet();
HashSet hs2 = Collections.synchronizedSet(new HashSet());


Creating a synchronized set means the individual methods of the set become atomic. This is not the same as synchronized blocks, for example :



Even though your collection is synchronized, another thread could still modify the collection and thus influence the result of the size() method between these two lines of code. The result then is that perhaps the wrong value is added to the collection.

This is a relatively useless example by the way
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic