Java Threading Basic Learning

Source: Internet
Author: User

I saw some threading knowledge in my spare time today.

The first thing to look at is the volatile keyword, which, as I've seen in my previous book, understands that the volatile keyword prevents compilation optimizations because the CPU reads data from the cache instead of reading it. This is a bit of a simple understanding of yourself and there may be errors. I just saw a bit of threading knowledge, but very little (no use) when I wrote the code, that is, when I learned to follow the book on the code.

Internship to me to write a stress test, 1000 threads at the same time, the number of requests, statistics request failed, I used the volatile keyword, but feel a bit of a problem, tested today.

I've always thought that the variable highlighted with the volatile keyword will not be problematic in the multi-threaded self-increment

Here's my first test code

Importjava.util.ArrayList;Importjava.util.List; Public classCount {Private Static volatile intCount = 0;  Public Static voidInc () {count++; }     Public Static intGetCount () {returncount; }     Public Static voidMain (string[] args)throwsException {List<Thread> list =NewArraylist<thread> (100);  for(inti = 0; I < 100; i++) {List.add (NewThread (NewRunnable () {@Override Public voidrun () {//for (int i=0;i<100;i++) {Count.inc (); // }                }            })); }         for(Thread t:list) {T.start (); }         for(Thread t:list) {t.join (); } System.out.println ("-----End-----");    System.out.println (Count.getcount ()); }}

I tested it many times and the final result was 100, and I almost thought it was the right thing to do.

But I see it in a book. In the Run method to set a loop, that is, to cancel the above comments will cause problems

Now the test, the result is indeed a problem, the expected result is 10000, but the test results are always 9800+,9900+, is not to 10000.

But there are comments and no comments, is there a difference in this piece of code? I think for a long time the difference between this code, I think it may be that each thread runs too short, resulting in the expected results

To continue the test or add a comment, change the 100 loop to 1000 times and the test result to 999.

This finally overturned the variable highlighted by the volatile keyword in the multi-threaded self-increment is not the problem of this conclusion.

But when I get this conclusion, I don't know where the volatile keyword should be used.

This is really a tangled problem, go back to check the information, the gods can also help you

Java Threading Basic Learning

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.