Is clone() thread safe?

If I call clone() in one thread on object X and another thread changes the state of X, what happens?Do I need to sync around clone()?
[147 byte] By [jordanza] at [2007-10-3 10:09:57]
# 1
You'll need to sync around clone and around every operation that can modify the state of the object.
jverda at 2007-7-15 5:29:47 > top of Java-index,Core,Core APIs...
# 2
Thanks. The main reason I'm asking is because Object.clone() is native. I'm wondering what happens inside of a native method if another Java thread is altering the object.
jordanza at 2007-7-15 5:29:47 > top of Java-index,Core,Core APIs...
# 3

I've never done any JNI and don't know much about native methods. However, native code will not honor Java's synchronization, so any native method that could potentially be interfering with another thread must be called from within a proper sync block, so that the method isn't invoked until the lock is obtained.

jverda at 2007-7-15 5:29:47 > top of Java-index,Core,Core APIs...
# 4

clone() is not thread-safe. All it does is basically memcpy the fields of the object. It is up to your program to make clone() safe if your class is designed for concurrent use.

Native code that accesses shared objects must either use the JNI monitor methods were appropriate or else only be called with the appropriate locks held at the Java level. Note that native methods can be declared synchronized.

davidholmesa at 2007-7-15 5:29:47 > top of Java-index,Core,Core APIs...