多线程的设计模式 [英] Design Patterns for Multithreading

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

问题描述

多任务似乎是一场灾难,大型项目由于共享突变而崩溃,我会说共享资源被多个网页访问。调试和跟踪bug的起源以及造成的原因变得非常困难。它让我想起来,有没有任何设计模式,可以在设计多线程程序时使用。



我非常感谢您对此的意见和评论,如果有人可以呈现好的

解决方案

@WYSIWYG链接似乎是有很多有用的模式,但我可以给你一些指导方针。多线程程序的主要问题是更新操作或并发修改,而一些较不发生的问题是饥饿,死锁等,如果我可以说更致命,所以为了避免这些情况,您可以:




  • 利用 Immutable Object 模式,如果一个对象在创建后无法被修改,那么你不能有不协调的更新,而且我们知道创建操作本身就被JVM保存为原子。

  • 命令查询隔离原则:这是修改的单独代码从代码读取的对象,因为读取可能并发发生,但修改不能。

  • 利用您所使用的语言和库功能,如并发列表和线程结构因为它们的设计很好,性能很好。

  • 有一本书(虽然是旧的),但是对于
    而言,设计非常好系统,它被称为 Java中的并发编程


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 theads. It becomes very difficult to debug and trace the origin of bug and what is causing it. It made me think, are there any design patterns which can be used while designing multithreaded programs.

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.

解决方案

@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:

  • Make use of the Immutable Object pattern, 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.

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

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