java - Object visibility across threads -


I have a general suspicion about publishing of data and data publication in thread. For example, consider the following category.

  public class DataRace {static int a = 0; Public Stable Zero Main () {New MyThread () Start (); A = 1; } Public Static Class Extends MyThread Thread {Public Zero Run ()) {// Entry A, B}}}  

focuses on the main ().

Clearly

  New MyThread (). Start (); A = 1;  

After MyThread starts, we change the shared variables and thus there can be no thread-protected publication.

  a = 1; New Mythread () Start (); Although  

changes at this time one has been safely published in the new thread, because the Java language specification (JLS) guarantees that all the variables that were visible to one thread A This thread B starts appearing in Thread B., which is effective as a built-in synchronization in thread (start).

  New MyThread (). Start (); Int b = 1;  

In this case when a new variable is allocated after the generation of both threads, there is no guarantee that the new variable will be safely published in all the threads. That is, if Var B is used from other B thread, then its price is guaranteed to be seen as 1. Note that I am not talking about any subsequent amendments in B (which definitely need to be synchronized), but previously allocated by Jvm.

Thank you,

I was not sure this one:

  a = 1; New Mythread () Start ();  

... in it I was not sure that there was no guarantee that the value of a would be "flushed" for the shared memory However, it clearly talks about this. It says:

A task that synchronizes a thread - starts with the first action in the thread.

(It has been shown basically after the discussion that it will be alright.)

I'm not sure that your last example ( b with).


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -