On multithreaded working memory when it will refresh

JMM program used to access rules defined variables, the definition of who wants to block out the memory access hardware and system differences caused.

Before looking at the empty JMM also mentioned the concept of working memory, each thread has its own working memory, all the variables there is more main memory, working memory storage is variable various threads used in main memory a copy of a copy, work memory can not directly operate between the other working memory variables, to be mediated by the middle of main memory as each thread can only operate their work thread variable, not directly operate the main memory variables.

Then there is the definition of the atomic operation. 8, to control variable, namely lock, unlock, read, load, use, assign, store, write.

It defines eight rules

1 a variable can only be one thread lock, the same thread can lock multiple times, corresponding to repeatedly unlock, behind say is reentrant.

After 2lock a variable, clear their working memory first, and then when you want to use, reload from main memory; after unlock, to be synchronized to the main memory.

Only 3 variables defined in the main memory, the user prior to use or to load or store assign

4 working memory variables did not change, no reason not synchronous main memory, be sure to change once the synchronous main memory.

These are the basics. Excerpt from "in-depth understanding of java virtual machine"

 

When we encounter two threads simultaneously operating a field object, we encountered some problems,

First posted Code

public class MyObject {
private String name="1";
private String pass="11";

public void print() {
System.out.println(name+" "+pass);
}

public void setvalue(String u,String p){
this.name=u;
if(Thread.currentThread().getName().equals("a")){
System.out.println("a停止 ");
Thread.currentThread().suspend();
}
this.pass=p;
}
}
public class Test {
public static void main(String[] args) throws Exception{
final MyObject object = new MyObject();
Thread thread2 = new Thread(){
@Override
public void run() {
while (true){
object.print();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread2.start();

Thread thread1 = new Thread(){
@Override
RUN void public () {
object.setvalue ( "A", "AA");
}
};

thread1.setName ( "A");
thread1.start ();
the Thread.sleep (500);

//thread2.start ( );

}

description: thread2 Thread1 and simultaneously to operate a target object,
After thread2 operation, the output is 1 and 11, after Thread1 starts, it will change the value of the object field, this time thread2 loop output has changed, and turned into a 11. 

ponder:
1 Each thread has its own working memory, will target object (field 1, 11) and one copy thread2 Thread1 respective working memory, the first field output thread2 (1, 11) can be appreciated.
2 but when started Thread1 change of the object field into the working memory (a, 11), 
. 3 followed
thread2 output field has become (a, 11)
so connected Consideration not do have their own working memory, thread thread1 did not stop ah, why threads thread2 working memory variable can also become lost.

The following are my views
thread thread1 the suspend () method will be synchronized to the main memory, and sleep () is true.
Then the next step is how synchronous main memory into the working memory thread thread2 in

this opinion is someone else
in order to improve performance, there are threads working memory, so access to the main memory data is not read, can be faster. After the thread shared variable is modified, the value of working memory in the thread and other threads will be inconsistent, and also inconsistent with the value of main memory, so you need to brush the value of working memory into main memory, but this into the brush and possibly other threads did not see.
After using volatile by cpu command barrier mandatory reading occurs after the write operation, and other threads when reading the shared variable, you need to clean up the value of their work memory and instead re-read from main memory, volatile guarantee that we'll refresh, but do not write not necessarily other invisible thread.

That is, not necessarily, there are random, without voliatile other threads do not necessarily see. Plus a certain look, see.
This is currently my understanding.



Guess you like

Origin www.cnblogs.com/xlblog/p/11520793.html