Do I have to [sync the access to
lastPacket
]?
No, you do not have to do that: since you are interested in writing out the last packet approximately as of the time the timer has fired, and because the receiver thread does not mutate an existing packet, the synchronization is unnecessary.
Making lastPacket
volatile should be sufficient to implement the logic:
private volatile Packet lastPacket;
Note: the above assumes that fromClient.readObject();
returns an entirely new object, which is independent of the lastPacket
object.