java中什么是线程安全?
可能的重复:
线程安全是什么意思?
我很困惑任何类都是线程安全的。据我了解,如果任何类是线程安全的,那么它的方法有一些特定的内容(如同步)。这是对还是错?请帮我详细解释一下它的含义。
Possible Duplicate:
What does threadsafe mean?
I am very confused that any class is Thread safe. I am understanding that, if any class is thread safe then it has some specific on its methods(as synchronized). Is it right or wrong? Please help me by elaborating on the meaning of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Seth 所说,线程安全意味着一个方法或类实例可以被多个线程同时使用,而不会出现任何问题。
考虑以下方法:
现在线程
A
和线程B
都想执行AddOne()
。但A
首先启动并将myInt (0)
的值读取到tmp
中。现在,由于某种原因,调度程序决定停止线程A
并将执行推迟到线程B
。线程B
现在还将myInt
的值(仍然是0
)读取到它自己的变量tmp
中。线程B
完成了整个方法,所以最后myInt = 1
。并返回1
。现在又轮到线程A
了。线程A
继续。并将1
添加到tmp
(线程A
的tmp
为0
)。然后将此值保存在myInt
中。myInt
又是1
。因此,在本例中,方法
AddOne()
被调用了两次,但由于该方法不是以线程安全的方式实现的,因此myInt
的值不是2
,如预期,但是1
,因为第二个线程在第一个线程完成更新之前读取了变量myInt
。在不平凡的情况下,创建线程安全方法非常困难。而且有很多技巧。在Java中,您可以将一个方法标记为同步,这意味着在给定时间只有一个线程可以执行该方法。其他线程排队等待。这使得方法线程安全,但是如果方法中有很多工作要做,那么这会浪费很多时间。另一种技术是通过创建锁或信号量并锁定这小部分(通常称为临界区)来“仅将方法的一小部分标记为同步”。甚至有一些方法被实现为无锁线程安全,这意味着它们的构建方式使得多个线程可以同时通过它们而不会引起问题,当一个方法只执行一个方法时可能会出现这种情况原子调用。原子调用是一种不能被中断且一次只能由一个线程完成的调用。
As Seth stated thread safe means that a method or class instance can be used by multiple threads at the same time without any problems occuring.
Consider the following method:
Now thread
A
and threadB
both would like to executeAddOne()
. butA
starts first and reads the value ofmyInt (0)
intotmp
. Now for some reason the scheduler decides to halt threadA
and defer execution to threadB
. ThreadB
now also reads the value ofmyInt
(still0
) into it's own variabletmp
. ThreadB
finishes the entire method, so in the endmyInt = 1
. And1
is returned. Now it's ThreadA
's turn again. ThreadA
continues. And adds1
totmp
(tmp
was0
for threadA
). And then saves this value inmyInt
.myInt
is again1
.So in this case the method
AddOne()
was called two times, but because the method was not implemented in a thread safe way the value ofmyInt
is not2
, as expected, but1
because the second thread read the variablemyInt
before the first thread finished updating it.Creating thread safe methods is very hard in non trivial cases. And there are quite a few techniques. In Java you can mark a method as synchronized, this means that only one thread can execute that method at a given time. The other threads wait in line. This makes a method thread safe, but if there is a lot of work to be done in a method, then this wastes a lot of time. Another technique is to 'mark only a small part of a method as synchronized' by creating a lock or semaphore, and locking this small part (usually called the critical section). There are even some methods that are implemented as lockless thread safe, which means that they are built in such a way that multiple threads can race through them at the same time without ever causing problems, this can be the case when a method only executes one atomic call. Atomic calls are calls that can't be interrupted and can only be done by one thread at a time.
线程安全仅仅意味着它可以同时从多个线程使用而不会引起问题。这可能意味着对任何资源的访问都是同步的,或者其他什么。
Thread safe simply means that it may be used from multiple threads at the same time without causing problems. This can mean that access to any resources are synchronized, or whatever.