thread-confinement

Thread Confinement

不羁岁月 提交于 2019-12-28 03:39:07
问题 I am reading Java Concurrency in Practice and kind of confused with the thread confinement concept. The book says that When an object is confined to a thread, such usage is automatically thread-safe even if the confined object itself is not So when an object is confined to a thread, no other thread can have access to it? Is that what it means to be confined to a thread? How does one keep an object confined to a thread? Edit: But what if I still want to share the object with another thread?

Thread Confinement

六眼飞鱼酱① 提交于 2019-11-27 18:59:27
I am reading Java Concurrency in Practice and kind of confused with the thread confinement concept. The book says that When an object is confined to a thread, such usage is automatically thread-safe even if the confined object itself is not So when an object is confined to a thread, no other thread can have access to it? Is that what it means to be confined to a thread? How does one keep an object confined to a thread? Edit: But what if I still want to share the object with another thread? Let's say that after thread A finishes with object O, thread B wants to access O. In this case, can O

When and how should I use a ThreadLocal variable?

一曲冷凌霜 提交于 2019-11-26 00:17:16
问题 When should I use a ThreadLocal variable? How is it used? 回答1: One possible (and common) use is when you have some object that is not thread-safe, but you want to avoid synchronizing access to that object (I'm looking at you, SimpleDateFormat). Instead, give each thread its own instance of the object. For example: public class Foo { // SimpleDateFormat is not thread-safe, so give one to each thread private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat