About Java Atomic Package usage help

Source: Internet
Author: User

Recently learning some knowledge of high concurrency, when learning to Atomic, run the program, not the same as expected

The code is as follows

 Public classLearn13 {atomicinteger count=NewAtomicinteger (0); voidm () { for(inti= 0; i< 10000; i++) count.getandincrement (); }         Public Static voidMain (string[] args) {FinalLearn12 Learn =NewLearn12 (); Runnable R=NewRunnable () {@Override Public voidrun () {learn.m ();        }                    }; List<Thread> threads =NewArraylist<thread>();  for(inti= 0; i< 10; i++) {Threads.add (NewThread (R, "Thead" +i)); }         for(Thread thread:threads) {Thread.Start (); }                 for(Thread thread:threads) {Try{thread.join (); } Catch(interruptedexception e) {e.printstacktrace ();    }} System.out.println (Learn.count); }}

Example will be able to get 100000 results, but I did not get, each time is not the same, the method is not atomic operation, I do not know where there is a problem, I hope to see can help answer the next

About Java Atomic Package usage help

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.