java中什么是线程安全?

发布于 2024-11-15 02:47:52 字数 245 浏览 4 评论 0原文

可能的重复:
线程安全是什么意思?

我很困惑任何类都是线程安全的。据我了解,如果任何类是线程安全的,那么它的方法有一些特定的内容(如同步)。这是对还是错?请帮我详细解释一下它的含义。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅冇旳回忆 2024-11-22 02:47:52

正如 Seth 所说,线程安全意味着一个方法或类实例可以被多个线程同时使用,而不会出现任何问题。

考虑以下方法:

private int myInt = 0;
public int AddOne()
{
    int tmp = myInt;
    tmp = tmp + 1;
    myInt = tmp;
    return tmp;
}

现在线程 A 和线程 B 都想执行 AddOne()。但A 首先启动并将myInt (0) 的值读取到tmp 中。现在,由于某种原因,调度程序决定停止线程 A 并将执行推迟到线程 B。线程 B 现在还将 myInt 的值(仍然是 0)读取到它自己的变量 tmp 中。线程B完成了整个方法,所以最后myInt = 1。并返回1。现在又轮到线程A了。线程 A 继续。并将 1 添加到 tmp(线程 Atmp0)。然后将此值保存在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:

private int myInt = 0;
public int AddOne()
{
    int tmp = myInt;
    tmp = tmp + 1;
    myInt = tmp;
    return tmp;
}

Now thread A and thread B both would like to execute AddOne(). but A starts first and reads the value of myInt (0) into tmp. Now for some reason the scheduler decides to halt thread A and defer execution to thread B. Thread B now also reads the value of myInt (still 0) into it's own variable tmp. Thread B finishes the entire method, so in the end myInt = 1. And 1 is returned. Now it's Thread A's turn again. Thread A continues. And adds 1 to tmp (tmp was 0 for thread A). And then saves this value in myInt. myInt is again 1.

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 of myInt is not 2, as expected, but 1 because the second thread read the variable myInt 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.

弱骨蛰伏 2024-11-22 02:47:52

线程安全仅仅意味着它可以同时从多个线程使用而不会引起问题。这可能意味着对任何资源的访问都是同步的,或者其他什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文