GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Serialization and Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Serialization and Thread" Watch "Serialization and Thread" New topic
Author

Serialization and Thread

Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Hello, I am using Wicket but I think this is more of a Java question. I am using a List to display the process of a batch job. A thread spawns off and will process the csv file. Each row in the CSV file is a job to process and each finished row will be stored into a BatchLine object which is then added to the List. The problem is for some reason if this page is running and I open a new instance of the Wicket application, it serializes the object and then deserializes it. Well the thread loses focus on the list during this and stops updating the List that is attached to the webPage.. Now I know we cannot serialize a thread, but during the serialization and deserialization process.. how do I keep the List between the webPage class, and thread using it the same upon deserialization? I am also using Collection.Synchronization like so:

List batchLines = Collections.synchronizedList(new CopyOnWriteArrayList<BatchLine>());

I currently pass the object around as parameters in the method, and also into the constructor of the runnable class.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I am pretty sure this is not a standard Java / synchronization issue. I don't know what a BatchLine object is, or what Wicket is, or why it would be serializing and deserializing things behind your back, but it must be Wicket-specific because that is not a Thread or synchronization behavior. Once you serialize and deserialize the List, it is a completely different instance of the List. If you need to use the new instance of the List in the old Thread instance then you will need to inject the List back into the Thread, perhaps using a volatile variable and some key synchornization points where the swap-out can take place. For example, before using the List, the Thread always calls a List getData() method which returns the List. Then whenever a new List instance is created it calls a setData(List) method which stores the List in the shared volatile variable. Of course, that relies on there being some Object which the Thread and the Wicket thingy shares as the same instance (that does not get serialized and deserialized).

A few things to consider:
1) Are you sure the List is serialized and deserialized? How do you know?
2) What does 'launch a new instance of the Wicket application' mean? Is it a new process (if so, then the problem can't be solved in Java, it must be solved with a cross-process resource such as a database).


Steve
Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Steve Luke wrote:
A few things to consider:
1) Are you sure the List is serialized and deserialized? How do you know?
2) What does 'launch a new instance of the Wicket application' mean? Is it a new process (if so, then the problem can't be solved in Java, it must be solved with a cross-process resource such as a database).


Hey Steve, thanks for your response.

2) It mean if I have the application running in one tab, and open it under the same session in a new tab.

1) In the documentation it states there is a pageMap class that stores all the current pages for the user in their session. When new tab is opened it basically serializes all the stored pages in the pageMap and then deserializes (Not sure 100% why, but it does).

Wicket has a class (Application class) that allows you to store and share data to all users who access the application. What I was thinking after reading your response is to store my runnable class into a Hashmap or a List of some sort so I can get back in contact with the original runnable object during serialization/deserialization per each user.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

So wicket is some sort of Web app framework? Is it server side or client side?
Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Yea it's been in development since 2005 I believe. It's a nice powerful framework to help separate front end(Html/CSS) from the back end (Java). Kind of like C# and ASP.net, but (In my opinion) much better. It is server side.

You can find out more info here:

http://wicket.apache.org/



P.S. I think the new logic is working, but I am still in the process of re-working the code. Thanks again!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So from my quick look at a Wicket example: it runs on the server, not on the client. The example of a "Wicket application" I saw was implemented as a JEE Filter; this means that "launch a new instance of the Wicket application' is a highly misleading way of saying anything. Whatever you do on the client side, there's only going to be one instance of the "Wicket application".

As for

In the documentation it states there is a pageMap class that stores all the current pages for the user in their session. When new tab is opened it basically serializes all the stored pages in the pageMap and then deserializes


this sounds quite unlikely to me, because it isn't possible from the server side to tell whether a request comes from a new tab. I would like to see the actual documentation which says that.
 
GeeCON Prague 2014
 
subject: Serialization and Thread