什么是java中的线程安全? [英] What is thread Safe in java?

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

问题描述


可能重复:

线程安全是什么意思?

我很困惑任何一个班级是线程安全的。我理解,如果任何类是线程安全的,那么它的方法有一些特定的(同步)。是对还是错?请帮助我详细说明它的含义。

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.

推荐答案

正如Seth所说的线程安全意味着方法或类实例可以是多个线程同时使用,没有任何问题。

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.

考虑以下方法:

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 tmp <$ c线程 A )的$ c> 0 。然后将此值保存在 myInt 中。 myInt 再次 1

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.

所以在这种情况下方法 AddOne()被调用了两次,但因为该方法没有以线程安全的方式实现,所以 myInt 不是 2 ,正如预期的那样,但 1 因为第二个线程读取变量 myInt 在第一个线程完成更新之前。

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.

在非平凡的情况下创建线程安全方法非常困难。而且有很多技巧。在Java中,您可以将方法标记为 synchronized ,这意味着在给定时间只有一个线程可以执行该方法。其他线程排队等候。这使得方法线程安全,但如果在方法中要做很多工作,那么这会浪费很多时间。另一种技术是通过创建锁或信号量并锁定这个小部分(通常称为临界区)来'仅将方法的一小部分标记为已同步'。甚至有一些方法被实现为无锁线程安全,这意味着它们的构建方式使得多个线程可以同时通过它们而不会引起问题,这可能是一种方法只执行一个方法的情况原子电话。原子调用是无法中断的调用,一次只能由一个线程完成。

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.

这篇关于什么是java中的线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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