aspose file tools*
The moose likes Threads and Synchronization and the fly likes Serialize Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Serialize Thread" Watch "Serialize Thread" New topic
Author

Serialize Thread

Samanta Bes
Greenhorn

Joined: Jun 06, 2011
Posts: 5
Hi! Is it possible to serialize a thread?



Is it correct?
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


Yes.
Samanta Bes
Greenhorn

Joined: Jun 06, 2011
Posts: 5
But when i try to send the reference of thread there is an exception:
java.io.NotSerializableException: java.io.BufferedReader
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

Have you declared a BufferedReader in your class? If you make a class serializable, all the member variables need to be serializable as well unless you specifically mark them as not to be serialized, and BufferedReader isn't.

I'd also question why you'd want to serialize a Thread object, but maybe we should sort out this problem first.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

What would it mean to serialize a thread? When you deserialize it, what do expect from it? To continue where it was when you serialized it? That's certainly not going to happen.

There are a few entities that are highly volatile - they only have meaning within the current JVM. Threads, input streams, output streams, connections - these are all things that should not be serialized.

If you want the thread's logic to be serialized, don't extend Thread but implement Runnable instead:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Samanta Bes
Greenhorn

Joined: Jun 06, 2011
Posts: 5
Goooood! Thank you! The problem was BufferReader!! I didn 't know that those made a problem!!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Take a look at the source code for Thread - you will see that it is LOADED with native methods.

These connections to native methods can not possibly be serialized.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialize Thread