简体   繁体   中英

Does java have Transferable Objects (Multi Threading Concept)

Javascript has Transferable Objects used to transfer an object to a web worker without copying for thread safety. Copying is not needed since the application no longer has a reference to the object, once the web worker has it. In other words the reference is either held by the worker or the application, but not both of them at the same time. Is there a similar mechanism in Java?

No, java does not have Transferable Objects as per the definition, because there is nothing in the JVM that can prevent two reference values from referring to the same instance.

However, there exist various kinds of builders, (see Builder Pattern, Wikipedia ) which can be used to construct an object, and then build an immutable instance out of it, which is then thread safe by virtue of being immutable. The most famous builder is StringBuilder . I do not know of any other builders that java comes with out of the box, but I know there are many libraries that have builders, for example Guava (Wikipedia) , see ImmutableMap.Builder<K,V> ( Google guava documentation ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM