线程限制 [英] Thread Confinement

查看:115
本文介绍了线程限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实践中阅读Java并发,并且混淆了线程限制概念。这本书说,


当对象被限制在一个线程中时,这种使用是自动线程安全的,即使受限对象本身不是


所以当一个对象被限制在一个线程中时,没有其他线程可以访问它?这是什么意思被限制在一个线程?


但是如果我仍然想要共享对象另一个线程?让我们说,在线程A完成对象O之后,线程B想要访问O.在这种情况下,在A完成之后,O仍然可以被限制到B吗?



使用局部变量是一个例子肯定,但这只是意味着你不与其他线程共享你的对象(全部)。在JDBC连接池的情况下,一旦一个线程完成了该连接,它不会将一个连接从一个线程传递到另一个线程(完全不知道这是因为我从来没有使用JDBC)。


因此,当一个对象被限制在一个线程中时,没有其他线程可以访问它?


< blockquote>

不,反之亦然:如果你确保没有其他线程可以访问对象,那么该对象被限制在一个线程。



没有将对象限制在单个线程的语言或JVM级别机制。你只需要确保没有对对象的引用转义到可以被另一个线程访问的地方。有些工具可帮助避免泄漏引用,例如 ThreadLocal 类,但确保没有任何引用在任何地方泄漏。



例如:如果对象的引用来自局部变量,则该对象绝对限于单个线程,因为其他线程永远不能访问局部变量。



同样,如果对象的引用是来自另一个已被证明局限于单个对象的对象

广告编辑:在实践中,你可以有一个对象,只能通过一个单个线程在其生命周期内,但单个线程改变(一个JDBC Connection 对象从连接池是一个很好的例子)。



验证,这样的对象只能通过单个线程访问比难以证明的对象限制为



在我看来,这些对象从来没有真正的局限于单线程(这将暗示一个强大的保证),但可以被称为被一个单一线程一次使用。


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 still be confined to B after A is done with it?

Using a local variable is one example for sure but that just means you don't share your object with other thread (AT ALL). In case of JDBC Connection pool, doesn't it pass one connection from one thread to another once a thread is done with that connection (totally clueless about this because I never used JDBC).

解决方案

So when an object is confined to a thread, no other thread can have access to it?

No, it's the other way around: if you ensure that no other thread has access to an object, then that object is said to be confined to a single thread.

There's no language- or JVM-level mechanism that confines an object to a single thread. You simply have to ensure that no reference to the object escapes to a place that could be accessed by another thread. There are tools that help avoid leaking references, such as the ThreadLocal class, but nothing that ensures that no reference is leaked anywhere.

For example: if the only reference to an object is from a local variable, then the object is definitely confined to a single thread, as other threads can never access local variables.

Similarly, if the only reference to an object is from another object that has already been proven to be confined to a single thread, then that first object is confined to the same thread.

Ad Edit: In practice you can have an object that's only accessed by a single thread at a time during its lifetime, but for which that single thread changes (a JDBC Connection object from a connection pool is a good example).

Proving that such an object is only ever accessed by a single thread is much harder than proving it for an object that's confined to a single thread during its entire life, however.

And in my opinion those objects are never really "confined to a single thread" (which would imply a strong guarantee), but could be said to "be used by a single thread at a time only".

这篇关于线程限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆