In this paper, we present a mechanism to capture and reestablish the state of Java threads. We achieve this by extracting a thread's execution state from the application code that is executing in this thread. This thread serialization mechanism is implemented by instrumenting the original application code at the byte code level, without modifying the Java Virtual Machine. We describe this thread serialization technique in the context of middleware support for mobile agent technology. We present a simple execution model for agents that guarantees correct thread migration semantics when moving an agent to another location. Our thread serialization mechanism is however generally applicable in other domains as well, such as load balancing and checkpointing.