Order reordering As-if-serial

Source: Internet
Author: User

I think after reading a book or just to understand a knowledge point is best to run some of the demo himself to try to understand the meaning of the various meanings so that the knowledge point will eventually be yours. By rote discussion or simply rude look at the data the brain must be a paste.

Public class Finaldemo {

Finaldemo Finaldemo;

Public final String b;

//

Public Finaldemo () {

random rw = new Random ();

int p = rw.nextint (ten);

b= "HelloWorld" +P;

// }

//

Public Void Write () {

//

finaldemo=new Finaldemo ();

// }

public void Domethodparam (product product) {

//string obj=b;

random rw = new Random ();

int p = rw.nextint (ten);

Product Totalmoney=product;

System. out.println ("Thread:" +thread.currentthread (). GetName () +"----Money:" +Totalmoney.getmoney () + "- ----------before---");

System. out.println ("Thread:" +thread.currentthread (). GetName () +"----money" +Totalmoney.getmoney () + "-----------End---");

}

@Test

public void Testfinalmethodparam () {

thread[] thread=new thread[4];

For (int i=0; i<4; i++) {

thread[i] = new Thread (new Runnable () {

@Override

Public void Run () {

int a=new Random (). Nextint (ten);

// TODO auto-generated method stub

product product=new Product ();

product.setname ("name_" +a);

Product.setmoney ("Ten" +a);

domethodparam (product);

}

});

thread[i].start ();

}

}

class product{

private String name;

Private String money ;

Public String getName () {

return name;

}

Public void SetName (String name) {

This . name = name;

}

Public String Getmoney () {

return money ;

}

Public void Setmoney (String money ) {

This . Money = Money ;

}

}

}

Printing results:

Threads: Thread-0----money:8-----------before---

Threads: Thread-2----money:9-----------before---

Threads: Thread-2----money9-----------End---

Threads: Thread-0----money8-----------End---

Threads: Thread-1----money:8-----------before---

Threads: Thread-3----money:5-----------before---

Threads: Thread-1----money8-----------End---

Threads: Thread-3----money5-----------End---

What do you read from the above print results ?

Summarized as follows:

1) The above-mentioned downlink has two lines of before content, and there is no before first and then end to explain a knowledge point: The program in the execution process was ordered re-ordering. The main reason is the means by which the compiler or processor sorts the sequence of instructions in order to optimize performance.

2) but have you observed that although the performance is the same as the result of the final execution of the same thread is the same, not because of the impact of multithreading to change the value. This is because the JMM memory model is determined. The local variables and method parameters are thread-private and are not shared by other threads.

3) There is a little as-if-serial semantics. No matter how the reordering is performed, the results of the execution of a single threaded thread that has dependencies are not changed. The processor guarantees that reordering is forbidden for directives that have dependencies

Order reordering As-if-serial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.