V
V
Vetal Matitskiy2015-06-29 08:30:29
Java
Vetal Matitskiy, 2015-06-29 08:30:29

What is the correct way to use immutable objects for multithreaded applications?

Good afternoon, dear Java gurus. Analyzing the questions that are asked at interviews, I try to understand the topic of immutable objects in more detail. Unfortunately, the resources that Google offers give only very general information in the style of "immutable objects, these are objects that cannot be changed in the program. To make an object immutable, use the final modifier." As encyclopedic knowledge, of course, this can be said at an interview, but I think they want to hear an understanding of deeper principles there. in addition, I want to write a mini-project for a better understanding of this from the most understandable so far I found a lecture https://youtu.be/z8bstpYRV9Ithere, at least on an example, it is shown how immutable is connected with multithreading. and it is clear why it is important to use immutable objects. Unfortunately, the video does not cover all points. Can you explain with examples or suggest adequate sources, where recommendations on the use of immutable objects in the context of multithreading are disclosed?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2015-06-29
@zolt85

In order to use something correctly, you need to know how it works. This is the problem, as I understand it. I advise you to google Mr. Kuksenko's reports on the Java Memory Model, it will give you a direction where to dig. Here's the first one I found on Google: www.youtube.com/watch?v=QBLKJxURdro . The topic is very interesting. You will understand - you will be able to write effective multi-threaded applications.

B
bromzh, 2015-06-29
@bromzh

First of all, you need to understand and be able to work with collections in a multi-threaded environment. The final modifier does not yet mean that the field cannot be changed. It only indicates that such a field cannot be assigned a value twice through the = operator.
On the topic of collections in multithreading, there is this: www.youtube.com/watch?v=-yQeYo32Lt4
Well, in general, everything is basically simple. In a multi-threaded environment, it becomes necessary to use some common data. The first option is to use synchronization (semaphores, mutexes, etc.). Another option is to declare that all data is immutable. These fields can be assigned only once, all collections are also immutable. Then you can read their values ​​without using synchronizations (which are sometimes complicated, and extra memory is wasted) and process them transparently in several threads.

A
angry_cellophane, 2015-06-30
@angry_cellophane

Better read: classic - Java concurrency in practice , another good explanation can be found here : comparison of shared, isolated mutability and immutable objects; concerrency primitives vs STMs vs actors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question