Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the weakReference.get() method safe to call from multiple threads at the same time?

Is the weakReference.get() method safe to call from multiple threads at the same time?

The documentation says that

Once an object has been determined to be garbage collectable at that time it will atomically clear all weak references to that object

like image 273
small Avatar asked Dec 17 '25 17:12

small


1 Answers

WeakReference.get() when updated by the GC or it couldn't be updated by the GC thread. Otherwise, there would be a risk you could see an object which has previously been collected.

Note: As @Pillar may be suggesting, there is one operation which is not thread safe clear(), it is possible another thread might still get() the value after this is called.

like image 172
Peter Lawrey Avatar answered Dec 19 '25 05:12

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!