N
N
nikit-cpp-0072015-04-02 22:29:05
Java
nikit-cpp-007, 2015-04-02 22:29:05

Why don't the variables in the thread's anonymous class have time to be initialized?

... for this, in line 19, I added an expectation if the player variable did not have time to initialize.
pastebin.com/TbFP6wmy
What is this behavior called? How to deal with it / not step on this rake in the future?
UPD class code:
https://github.com/nikit-cpp/VkCachingPlayer/blob/...
UPD2: it turns out that all class variables that can be accessed from multiple threads should be marked volatile?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
angry_cellophane, 2015-04-02
@nikit-cpp-007

It is not so easy to take and work with one variable from several threads.
The main question is what value will the read operation read?
Where to learn more about it:
1. Classics of the genre - Java concurrency in practice. Brian Goetz.
2. Java memory model
pragmatics 3. What is memory model .
3. Java memory model itself (not for the faint of heart)
4. Good post on Habré
5. ...
6. PROFIT!
PS: There are a lot of problems in CustomPlayer and not only with multithreading. Practical advice: do not create threads by hand (see ExecutorService - thread pool), do not leave empty catch blocks (at least write an error to the logger), observejava naming conventions , remove code duplication, love life and not lose heart.

P
programmerjava, 2015-04-02
@programmerjava

Well, actually, I see this shared variable in the code ...
more code is needed. including the declaration of the class of this object :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question