multithreading 多线程设计模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17263335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Design Patterns for Multithreading
提问by Ankit Zalani
Multitasking seems to be a disaster at times when big projects crashes due to shared mutation of I would say shared resources are accessed by multiple threads. It becomes very difficult to debug and trace the origin of bug and what is causing it. It made me ask, are there any design patterns, which can be used while designing multithreaded
programs?
当大项目由于共享突变而崩溃时,多任务处理似乎是一场灾难,我会说共享资源被多个线程访问。调试和追踪错误的来源以及导致错误的原因变得非常困难。这让我不禁要问,有没有什么设计模式可以在设计multithreaded
程序时使用?
I would really appreciate your views and comments on this and if someone can present good design practices which can be followed to make our program thread safe, it will be a great help.
我非常感谢您对此的看法和评论,如果有人可以提出可以遵循的良好设计实践来使我们的程序线程安全,这将是一个很大的帮助。
采纳答案by Ibrahim Najjar
@WYSIWYG link seems to have a wealth of useful patterns but i can give you some guidelines to follow. The main source of problems with Multi-Threaded programs is Update Operations or Concurrent Modification and some of the less occurring problems are Starvation, Deadlocks , etc which are more deadly if i may say, so to avoid these situations you can:
@WYSIWYG 链接似乎有很多有用的模式,但我可以给你一些指导方针。多线程程序问题的主要来源是更新操作或并发修改,一些较少发生的问题是饥饿、死锁等,如果我可以说,这些问题更致命,因此为了避免这些情况,您可以:
- Make use of the Immutable Objectpattern, if an object can't be modified after creation then you can't have uncoordinated updates and as we know the creation operation itself is guaranteed to be atomic by the JVM in your case.
- Command Query Segregation Principle: that is separate code that modifies the object from code that reads them because reading can happen concurrently but modification can't.
- Take huge benefit of the language and library features you are using such as concurrent lists and threading structures because they are well designed and have good performance.
- There is a book (although an old one)but with very good designs for such systems, it is called Concurrent Programming in Java.
- 利用不可变对象模式,如果一个对象在创建后无法修改,那么你不能有不协调的更新,正如我们所知,在你的情况下,JVM 保证创建操作本身是原子的。
- 命令查询隔离原则:即修改对象的代码与读取对象的代码是分开的,因为读取可以并发发生,但修改不能。
- 充分利用您正在使用的语言和库功能,例如并发列表和线程结构,因为它们设计精良且性能良好。
- 有一本书(虽然是旧的)但是对这种系统有很好的设计,它被称为Java 中的并发编程。
回答by om471987
Design patterns are used to solve a specific problem. If you want to avoid deadlocks and increase debugging, there are some dos and donts
设计模式用于解决特定问题。如果你想避免死锁并增加调试,有一些注意事项
User thread-safe library. .Net java, C++ have their own thread safe libraries. Use them. Don't try to create your own data structures.
If you are using .Net, try Task instead of threads. They are more logical and safe.
用户线程安全库。.Net java、C++ 都有自己的线程安全库。使用它们。不要尝试创建自己的数据结构。
如果您使用的是 .Net,请尝试使用 Task 而不是线程。它们更合乎逻辑和安全。
You might wanna look at list of some Concurrency related patterns
您可能想查看一些与并发相关的模式列表