Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialize Thread

 
Samanta Bes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Is it possible to serialize a thread?



Is it correct?
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes.
 
Samanta Bes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when i try to send the reference of thread there is an exception:
java.io.NotSerializableException: java.io.BufferedReader
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Samanta Bes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic