最佳编程方法/方法,以确保线程安全 [英] Best programming approach/methodology to assure thread safety

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

问题描述

当我在从基本的,Pascal,COBOL和C的20年程序编程的背景下学习Java时,我想当时最困难的事情是围绕OOP术语和概念。现在有了大约8年的坚实Java在我的腰带,我得出的结论是,在Java和类似的语言,如C#编程的最难的事情是多线程/并发方面。

When I was learning Java coming from a background of some 20 years of procedural programming with basic, Pascal, COBOL and C, I thought at the time that the hardest thing about it was wrapping my head around the OOP jargon and concepts. Now with about 8 years of solid Java under my belt, I have come to the conclusion that the single hardest thing about programming in Java and similar languages like C# is the multithreaded/concurrent aspects.

编码可靠和可扩展的多线程应用程序只是平凡困难!随着处理器增长更宽而不是更快的趋势,它正在迅速变得非常关键。

Coding reliable and scalable multi-threaded applications is just plain hard! And with the trend for processors to grow "wider" rather than faster, it is rapidly becoming just plain critical.

最困难的领域当然是控制线程之间的交互

The hardest area is, of course, controlling interactions between threads and the resulting bugs: deadlocks, race conditions, stale data and latency.

因此,我的问题是:你的方法或方法是什么 >用于产生安全的并发代码,同时减少潜在的死锁,延迟和其他问题?我提出了一种方法,这是一个有点不寻常,但在几个大型应用程序非常好,我会在这个问题的详细答案。

So my question to you is this: what approach or methodology do you employ for producing safe concurrent code while mitigating the potential for deadlocks, latency, and other problems? I have come up with an approach which is a little unconventional but has worked very well in several large applications, which I will share in a detailed answer to this question.

推荐答案

有很多技术刚刚进入公众意识(如:最近几年)。一个大的会是演员。这是Erlang首先带给网格铁的东西,但它已经被像Scala(JVM上的actor)这样的新语言所推动。尽管演员们不能解决每个问题,但是他们确实能够更容易地推断出您的代码并识别故障点。它们还使得设计并行算法变得简单得多,因为它们迫使你使用继续传递在共享的可变状态。

There are a number of techniques which are coming into the public consciousness just now (as in: the last few years). A big one would be actors. This is something that Erlang first brought to the grid iron but which has been carried forward by newer languages like Scala (actors on the JVM). While it is true that actors don't solve every problem, they do make it much easier to reason about your code and identify trouble spots. They also make it much simpler to design parallel algorithms because of the way they force you to use continuation passing over shared mutable state.

Fork / Join是你应该看到的东西,特别是如果你在JVM上。 Doug Lea写了关于这个话题的开创性论文,但是很多研究人员多年来一直在讨论。根据我的理解,Doug Lea的参考框架计划包含在Java 7中。

Fork/Join is something you should look at, especially if you're on the JVM. Doug Lea wrote the seminal paper on the topic, but many researchers have discussed it over the years. As I understand it, Doug Lea's reference framework is scheduled for inclusion into Java 7.

在一个稍微少一点的入侵级别上,线程应用程序只是为了降低锁定的复杂性。细粒度锁定(在Java 5风格)是伟大的吞吐量,但非常很难得到正确。通过Clojure获得一些牵引的一种替代方法是软件事务存储器(STM)。这基本上与常规锁定相反,它是乐观的而不是悲观的。你首先假设你不会有任何冲突,然后允许框架解决问题,如果和当它们发生时。数据库通常以这种方式工作。这对于具有低冲突率的系统的吞吐量非常有用,但是最大的优势在于算法的逻辑组件化。而不是任意地将一个锁(或一系列的锁)与一些数据相关联,你只需在一个事务中包装危险代码,让框架找出其余的。你甚至可以得到一个相当的编译时检查出正确的STM实现,如GHC的STM单子或我的实验Scala STM。

On a slightly less-invasive level, often the only steps necessary to simplify a multi-threaded application are just to reduce the complexity of the locking. Fine-grained locking (in the Java 5 style) is great for throughput, but very very difficult to get right. One alternative approach to locking which is gaining some traction through Clojure would be software-transactional memory (STM). This is essentially the opposite of conventional locking in that it is optimistic rather than pessimistic. You start out by assuming that you won't have any collisions, and then allow the framework to fix the problems if and when they occur. Databases often work this way. It's great for throughput on systems with low collision rates, but the big win is in the logical componentization of your algorithms. Rather than arbitrarily associating a lock (or a series of locks) with some data, you just wrap the dangerous code in a transaction and let the framework figure out the rest. You can even get a fair bit of compile-time checking out of decent STM implementations like GHC's STM monad or my experimental Scala STM.

有很多新的选项构建并发应用程序,你选择哪一个很大程度上取决于你的专业知识,你的语言和你试图模拟什么样的问题。作为一般规则,我认为演员加上持久,不可变的数据结构是一个坚实的赌注,但正如我所说,STM是有点少侵入,有时可以产生更多的即时改进。

There are a lot of new options for building concurrent applications, which one you pick depends greatly on your expertise, your language and what sort of problem you're trying to model. As a general rule, I think actors coupled with persistent, immutable data structures are a solid bet, but as I said, STM is a little less invasive and can sometimes yield more immediate improvements.

这篇关于最佳编程方法/方法,以确保线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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