Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization and Thread

 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So wicket is some sort of Web app framework? Is it server side or client side?
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic