线程安全与同步 [英] Threadsafe vs Synchronized

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

问题描述

我是Java新手. 我在Threadsafe和Synchronized之间有些困惑. 线程安全意味着一个方法或类实例可以被多个线程同时使用,而不会发生任何问题. 同步"表示一次只能有一个线程运行.

I'm new to java. I'm little bit confused between Threadsafe and synchronized. Thread safe means that a method or class instance can be used by multiple threads at the same time without any problems occurring. Where as Synchronized means only one thread can operate at single time.

那么它们如何相互联系?

So how they are related to each other?

推荐答案

实践中的Java并发性是:

如果一个类从多个线程访问时能正确运行,则该类是线程安全的,而与运行时环境对这些线程的执行进行调度或交织无关,并且在调用方没有任何其他同步或其他协调的情况下代码.

A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.

例如,一个java.text.SimpleDateFormat对象具有内部可变状态,该状态在调用解析或格式化方法时会被修改.如果多个线程调用同一dateformat对象的方法,则一个线程可能会修改其他线程所需的状态,结果是某些线程获得的结果可能是错误的.内部状态可能被破坏而导致输出错误的可能性使此类不是线程安全的.

For example, a java.text.SimpleDateFormat object has internal mutable state that is modified when a method that parses or formats is called. If multiple threads call the methods of the same dateformat object, there is a chance a thread can modify the state needed by the other threads, with the result that the results obtained by some of the threads may be in error. The possibility of having internal state get corrupted causing bad output makes this class not threadsafe.

有多种方法可以解决此问题.您可以在应用程序中的每个需要SimpleDateFormat对象的地方都实例化一个新实例,您可以使ThreadLocal持有一个SimpleDateFormat对象,以便程序的每个线程都可以访问其自己的副本(因此,每个线程仅具有创建一个),您可以使用SimpleDateFormat的一种不保持状态的替​​代方法,也可以使用synchronized进行锁定,以便一次仅一个线程可以访问dateFormat对象.

There are multiple ways of handling this problem. You can have every place in your application that needs a SimpleDateFormat object instantiate a new one every time it needs one, you can make a ThreadLocal holding a SimpleDateFormat object so that each thread of your program can access its own copy (so each thread only has to create one), you can use an alternative to SimpleDateFormat that doesn't keep state, or you can do locking using synchronized so that only one thread at a time can access the dateFormat object.

锁定不一定是最好的方法,最好是尽可能避免共享的可变状态.这就是为什么他们在Java 8中引入了不保持可变状态的日期格式化程序.

Locking is not necessarily the best approach, avoiding shared mutable state is best whenever possible. That's why in Java 8 they introduced a date formatter that doesn't keep mutable state.

synchronized关键字是一种限制对方法或代码块的访问的方法,这样就不会破坏线程不安全的数据.此关键字通过要求线程必须获得对某个锁的独占访问权来保护方法或块(对象实例(如果synced是在实例方法上,或者是类实例,如果synchronized是在静态方法上或在指定方法上) (如果使用同步块,则锁定),然后才能进入方法或块,同时提供内存可见性,以使线程看不到过时的数据.

The synchronized keyword is one way of restricting access to a method or block of code so that otherwise thread-unsafe data doesn't get corrupted. This keyword protects the method or block by requiring that a thread has to acquire exclusive access to a certain lock (the object instance, if synchronized is on an instance method, or the class instance, if synchronized is on a static method, or the specified lock if using a synchronized block) before it can enter the method or block, while providing memory visibility so that threads don't see stale data.

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

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