Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

like image 639
jjujuma Avatar asked May 04 '09 07:05

jjujuma


People also ask

Where can I learn Java concurrency?

Java Multithreading This is a free course to learn multithreading in Java — you can join it on Udemy, the biggest platform for online courses. This is a nice fundamental course — without being too comprehensive — that tells you everything you need to know in order to read and write concurrent code using Java libraries.

How do you solve concurrency issues in Java?

The simplest way to avoid problems with concurrency is to share only immutable data between threads. Immutable data is data which cannot be changed. To make a class immutable define the class and all its fields as final. Also ensure that no reference to fields escape during construction.

How we can implement concurrency in Java?

A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.

What is concurrency in Java with example?

Concurrency is the ability to run several or multi programs or applications in parallel. The backbone of Java concurrency is threads (a lightweight process, which has its own files and stacks and can access the shared data from other threads in the same process).


3 Answers

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.

like image 134
Julien Chastang Avatar answered Oct 19 '22 19:10

Julien Chastang


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

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

like image 37
B.E. Avatar answered Oct 19 '22 21:10

B.E.


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
like image 25
dfa Avatar answered Oct 19 '22 20:10

dfa