对于学习 Java 并发的程序或小项目有什么建议吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/819138/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:55:38  来源:igfitidea点击:

Any suggestions for a program or small project to learn about concurrency in Java?

javamultithreadingconcurrency

提问by jjujuma

I'm aiming to learn about concurrency in Java. Currently my state of knowledge is poor. I'm pretty sure I know what "volatile" means. I sort of know what "synchronized" means. Sometimes. I've never written code that starts threads or manages them. Outside of this issue, I feel confident and at home working in Java.

我的目标是学习 Java 中的并发性。目前我的知识水平很差。我很确定我知道“易失性”是什么意思。我有点知道“同步”是什么意思。有时。我从未编写过启动线程或管理它们的代码。在这个问题之外,我对使用 Java 感到自信和在家工作。

I'm looking for suggestions for a small project or program which will require understanding concurrency to work correctly and efficiently.

我正在寻找一个小项目或程序的建议,这些项目或程序需要了解并发才能正确有效地工作。

采纳答案by B.E.

If you're really just starting then probably the producer-consumer problem is a good way to start:

如果您真的刚刚开始,那么生产者-消费者问题可能是一个很好的开始方式:

http://en.wikipedia.org/wiki/Producer-consumer_problem

http://en.wikipedia.org/wiki/Producer-consumer_problem

Don't read too much because the Wikipedia article also contains a solution to the problem :-)

不要阅读太多,因为维基百科文章也包含解决问题的方法:-)

回答by Julien Chastang

Write a matrix-multiply algorithm. Parallelize it. Optimize it. See how it scales especially if you have a multi-core machine. That would be a fun project.

写一个矩阵乘法算法。并行化它。优化它。看看它是如何扩展的,特别是如果你有一台多核机器。那将是一个有趣的项目。

回答by dfa

try a sudoku resolver, with various strategies:

尝试使用各种策略的数独解析器:

  • 3 threads : 1 for rows, 1 for columns and 1 for sub-squares
  • 9 threads : 3 for rows (1 thread each 3 rows), 3 for columns and 3 for sub-squares
  • 27 threads: 9 for rows (1 thread each 1 row), etc
  • 3 个线程:1 个用于行,1 个用于列,1 个用于子方块
  • 9 个线程:3 个用于行(每 3 行 1 个线程),3 个用于列,3 个用于子方块
  • 27 个线程:9 个用于行(每 1 行 1 个线程)等

回答by Rsh

I agree with @Julien on mtrix multiplication . In this problem the improvement caused by the usage of threads can be much more visible .
But as apposed @dfa, I disagree with Sudoku. it is not sensible enough
For example here is my result of writing Matrix-Mul in threads

我同意@Julien 关于 mtrix 乘法。在这个问题中,由使用线程引起的改进更加明显。
但正如@dfa 所说,我不同意数独。它不够明智
例如这是我在线程中编写 Matrix-Mul 的结果

 %        self                        
time    seconds   name    
32.38    10.72    Simple
21.29    7.05     Tiling
15.58    5.16     SimpleThread
9.63     3.19     ThreadTiling

This is the result of multiplication of a 1000*1000 matrix . You can easily see that how much threading can improve your program speed . ( Tiling is a technique used to enhance the cache hit )

After that, when you became familiar with syntax, you may dive into classical problems. they might be a little bit more challenging, but it can help you to become familiar with patterns in solving concurrency problems .
I suggest to take a look at Little book on semaphores. It so comprehensive and can help you to grasp ideas behind these problems .

这是一个 1000*1000 矩阵相乘的结果。您可以很容易地看到线程处理可以提高您的程序速度。(平铺是一种用于增强缓存命中的技术)

之后,当您熟悉语法时,您可能会深入研究经典问题。它们可能更具挑战性,但它可以帮助您熟悉解决并发问题的模式。
我建议看看关于信号量的小书。它如此全面,可以帮助您掌握这些问题背后的想法。

回答by Christoffer

When I read concurrent systems programming at the university, we built a video surveillance system - one master PC that fetched video data from one or more slave PC's with webcams. I remember that project since it really made you grok both efficient network programming, real-time issues and JNI :)

当我在大学阅读并发系统编程时,我们构建了一个视频监控系统 - 一台主 PC,它从一台或多台带有网络摄像头的从 PC 获取视频数据。我记得那个项目,因为它确实让你熟悉高效的网络编程、实时问题和 JNI :)

回答by Peter Lawrey

A common learning project for concurrency and networking is to write a chat program. It is similar to the FIX server suggestion but you just pass text around.

并发和网络的一个常见学习项目是编写一个聊天程序。它类似于 FIX 服务器建议,但您只需传递文本。

回答by willcodejavaforfood

If you are an somewhat experienced programmer it might be interesting to create a little server for prescribing to share prices that uses the Fix Protocol. It would need to be able to cope with several clients (and then you get to learn Swing or some web techs) which requires concurrency.

如果您是一位有一定经验的程序员,那么创建一个使用Fix Protocol来指定股价的小型服务器可能会很有趣。它需要能够处理需要并发的多个客户端(然后您可以学习 Swing 或一些 Web 技术)。

If you are a beginner I suggest something simpler like a producer thread with a couple of consumer threads and you would get extra points if you can graphically show the process.

如果您是初学者,我建议使用更简单的方法,例如带有几个消费者线程的生产者线程,如果您能以图形方式显示该过程,您将获得加分。

回答by Chathuranga Chandrasekara

This is not a Complete project but it contains some source codes as well. I think it would be great for get a good understanding about threading.

这不是一个完整的项目,但它也包含一些源代码。我认为对线程有一个很好的理解会很好。

Threading in Java

Java 中的线程

回答by Darron

I strongly recommend the book Java Concurrency In Practiceas a resource while you are working on whichever project you choose.

我强烈推荐《Java Concurrency In Practice》这本书作为您在选择任何项目时的资源。