It's not a secret anymore!
The moose likes Java in General and the fly likes Can Threads affect Date Objects ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can Threads affect Date Objects ??" Watch "Can Threads affect Date Objects ??" New topic

Can Threads affect Date Objects ??

Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Hi All,

Hoping someone might be able to offer a suggestion as to why I'm getting some very odd behaviour... In short it seems when I access a Date object through a seperate Thread.. the Date is being modified... somehow...??/

Basically my scenario is that I'm developing a JSF based app that uses multiple Threads to seperate the processing of my data to give response back to the user.. I have a ArrayList<MyObject> and MyObject contains a java.util.Date, say called myDate.... I have a kind of wizard approach going on where the user can upload a text file for parsing.. I then show the results which they can modify certain parts, and then they can run another process to process and persist the data to the database.

All is good until I get to the final stage where I'm about to persist the data.. The first set of objects (about 20) in my ArrayList 'lose' their Time information from the Date.. i.e the time will be 0:00:00..

Now I've got a Test package which calls the routine that generates this ArrayList of objects, and after the test I go through the list outputting a toString() on each object.. All the dates are there and correct.. So the passes...

So back to testing using the webapp... As I said I'm using JSF and I've also done a test where as soon as the user clicks on the process button.. I again go through the List and output each item as its toString() and thats OK.. BUT the next part is where I create a new Thread and immediately, actually in the constructor of the Thread (Runnable inner class) ... I AGAIN go through the list... and lo and behold the first 30 or so objects in the exact same list seem to have had their Time data stripped from the Data..

Heres an example of my toString() output...


All those are incorrect... The following are what I expect, and what is output both in my Test package AND at the moment before I setup a Thread...

[SCHEDULE IMPORT ELEMENT] Sun Aug 03 05:00:00 BST 2008
[SCHEDULE IMPORT ELEMENT] Sun Aug 04 05:00:00 BST 2008
[SCHEDULE IMPORT ELEMENT] Sun Aug 05 05:00:00 BST 2008

Any thoughts here greatly appreciated - I must admit this is quite beyond me as to whats happening...

Regards, Dave Brown
SCJP 6 - [url][/url] - Check out Grails Forum
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

So it isn't just loosing the time information, it is also printing out in the wrong format and with the wrong dates?

Can you make a simple multi-threaded application (a SSCCE) that duplicates the behavior - leaving JSF out of it. If you can post that code then we can see the issue.

Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Sorry chaps, its not the threading.. , the problems are down to the JSF side.. specifically the Converters...

So thanks for reading.. Sorry for wasting anyones time...
I agree. Here's the link:
subject: Can Threads affect Date Objects ??
It's not a secret anymore!