aspose file tools*
The moose likes Beginning Java and the fly likes completely lost in terms of trying to understand Collections.synchronizedList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "completely lost in terms of trying to understand Collections.synchronizedList" Watch "completely lost in terms of trying to understand Collections.synchronizedList" New topic
Author

completely lost in terms of trying to understand Collections.synchronizedList

Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
Hi I keep running into an error in my code. Whenever I add an Activity object into my Activity array (e.g. priority.add(activity)) it gets added but the next time I add another activity the previous activity (or activities) all get rewritten as the current activity.

I declared "List<Activity> priority = new ArrayList();" and I also did "List list = Collections.synchronizedList(priority);" I am PRETTY sure my problem comes from a synchronization issue but I don't understand how I am suppose to fix it. I've included my code below (kinda long sorry) but if you run it and enter in two separate activities then you'll notice that previous activities are being overwritten. I nearly lost my mind this afternoon wondering what I did wrong until I realized it was probably a syncing issue.

Could someone please guide me in the proper direction? I read the API doc on this but 1) I don't know where to include the iteration part and 2) I still don't understand it that well even if I did know where to put the iteration code

Main code:


2 additional classes needed:


Samrat Som
Ranch Hand

Joined: Feb 04, 2009
Posts: 40
Hey can you just explain a bit ...what you are trying to achieve with a small example rather than putting such a long jittery of code...

It is always better to make a simple code replicating the problem rather than this which couls help to solve it faster and efficiently ....


SCJP 1.6
SCWCD 1.5 (Preparing...)
Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
yeah i will do the small example in the morning ... i keep forgetting about all the code people have to read
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Maybe everything is working correctly ... but you're just reading from the wrong end of the list (e.g. adding to the end ... and then reading from the end, instead of the beginning). Just a thought - be sure to consider the "simple" possibilities, first.

Another hint: be sure to check "size()". I suspect it's probably incrementing by "1" each add()...


Paul M. Santa Maria, SCJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39810
    
  28
please. Make sure you have the actual type parameter <Activity> in. Use an ArrayList, or the synchronized List, but not both. Do you require access from several Threads (I don't think you do)? If so, you need a synchronized List, if not, then use the ordinary ArrayList.

If you want a synchronized List, you want something likeThat way it is impossible to enter anything into the List before it is synchronized.

You will find details of how to iterate a List in the Java™ Tutorials (look under interfaces->Collection and interfaces->List).
Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
Samrat Som wrote:Hey can you just explain a bit ...what you are trying to achieve with a small example rather than putting such a long jittery of code...

It is always better to make a simple code replicating the problem rather than this which couls help to solve it faster and efficiently ....


So I simplified it as much as I could. It is in one class file now and it highlights the main issue I keep running into. If you set the date (click the button) it will add the date into an array. If you change the date and then click the button again it is SUPPOSE to add the new date to the array.

I setup a showdialog to loop through the array and show the contents. It should be showing two different dates if you press the button (once with one date and another time with a different date) but it is not.

Can someone help me identify the problem? If you have already commented and told me the problem then it went over my head, could you simplify it? I am still a newbie :-)

Here is the new simplified code:



pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Your problem is that you have one Calendar instance, dueDate that is never reconstructed, and so whenever you change the state of dueDate, you change the state of all Calendar objects that refer to this same instance. Solution: reconstruct a new Calendar object each time you need it:

Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
pete stein wrote:Your problem is that you have one Calendar instance, dueDate that is never reconstructed, and so whenever you change the state of dueDate, you change the state of all Calendar objects that refer to this same instance. Solution: reconstruct a new Calendar object each time you need it:



Thanks Pete, even now I was going over the Calendar class but I don't think I wouldn't have spotted that without your help. Thanks so much, works great now :-)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Sebastion Hill wrote:Thanks Pete, even now I was going over the Calendar class but I don't think I wouldn't have spotted that without your help. Thanks so much, works great now :-)

You're entirely welcome. I also agree with the poster above in that if you are going to be adding to your collection only from within Swing's EDT or event dispatch thread, then there's no need to have the overhead of a synchronized collection.

Best of luck,

Pete
Sebastion Hill
Ranch Hand

Joined: Jul 10, 2009
Posts: 48
pete stein wrote:
Sebastion Hill wrote:Thanks Pete, even now I was going over the Calendar class but I don't think I wouldn't have spotted that without your help. Thanks so much, works great now :-)

You're entirely welcome. I also agree with the poster above in that if you are going to be adding to your collection only from within Swing's EDT or event dispatch thread, then there's no need to have the overhead of a synchronized collection.

Best of luck,

Pete


I'll fix that ... when I kept running into the problem I started thinking of solutions (aka I just started guessing) ... one of those happened to be the synchronized collection but yeah glad that wasn't the issue :-) thanks again EVERYONE
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: completely lost in terms of trying to understand Collections.synchronizedList