Test _java for Atomicinteger and int values in Java to increment operations under multithreading

Source: Internet
Author: User
Tags assert thread class

Java has designed classes for numeric security counters under multithreading, these classes are called atomic classes, and some of them are as follows:

Java.util.concurrent.atomic.AtomicBoolean;
Java.util.concurrent.atomic.AtomicInteger;
Java.util.concurrent.atomic.AtomicLong;
Java.util.concurrent.atomic.AtomicReference;

Below is a contrast atomicinteger with the ordinary int value under the multithreading increment test, uses is the JUNIT4;

Complete code:

Package Test.java;
Import Java.util.concurrent.CountDownLatch;

Import Java.util.concurrent.atomic.AtomicInteger;
Import Org.junit.Assert;
Import Org.junit.Before;

Import Org.junit.Test; /** * Test Atomicinteger and ordinary int value under multithreading the increment operation */public class Testatomic {//atomic integer increment object public static Atomicinteger Coun
 ter_integer;//= new Atomicinteger (0);

 A variable of type int public static int count_int = 0;
 @Before public void Setup () {//The initial setup work before all tests start Counter_integer = new Atomicinteger (0);
 @Test public void Testatomic () throws Interruptedexception {//number of threads created int threadcount = 100;
 How many times int loopcount = 10000600 in the inner loop of other satellite threads;
 Control the auxiliary object of the subordinate thread; (other await line enters upgradeable waiting for the main thread to start shouting) countdownlatch latch_1 = new Countdownlatch (1);
 Control the secondary object of the main thread, (the main thread waits for all the subordinate threads to run and then continues) Countdownlatch latch_n = new Countdownlatch (threadcount); Create and start other subordinate threads for (int i = 0; i < threadcount i++) {thread thread = new Atomicintegerthread (latch_1, Latch_n, loo
  Pcount);
 Thread.Start (); Long Startnano = SysTem.nanotime ();
 Let the other waiting threads unify to start latch_1.countdown ();
 Wait for other threads to finish executing latch_n.await ();
 Long Endnano = System.nanotime ();
 int sum = Counter_integer.get ();
 Assert.assertequals ("Sum is not equal to ThreadCount * loopcount, test Failed", SUM, ThreadCount * loopcount); System.out.println ("--------testatomic ();
 Expected to be equal------------");
 SYSTEM.OUT.PRINTLN ("Time Consuming:" + ((Endnano-startnano)/(1000 * 1000)) + "MS");
 System.out.println ("ThreadCount =" + (threadcount) + ";");
 System.out.println ("Loopcount =" + (Loopcount) + ";");

 System.out.println ("sum =" + (sum) + ";");
 @Test public void Testintadd () throws Interruptedexception {//number of threads created int threadcount = 100;
 How many times int loopcount = 10000600 in the inner loop of other satellite threads;
 Control the auxiliary object of the subordinate thread; (other await line enters upgradeable waiting for the main thread to start shouting) countdownlatch latch_1 = new Countdownlatch (1);
 Control the secondary object of the main thread, (the main thread waits for all the subordinate threads to run and then continues) Countdownlatch latch_n = new Countdownlatch (threadcount); Create and start other subordinate threads for (int i = 0; i < threadcount i++) {thread thread = new Integerthread (latch_1, Latch_n, Loopcount);
 Thread.Start ();
 Long Startnano = System.nanotime ();
 Let the other waiting threads unify to start latch_1.countdown ();
 Wait for other threads to finish executing latch_n.await ();
 Long Endnano = System.nanotime ();
 int sum = Count_int;
 Assert.assertnotequals ("Sum equals ThreadCount * Loopcount,testintadd () test Failed", SUM, ThreadCount * loopcount); System.out.println ("-------testintadd ();
 Expected to be unequal---------");
 SYSTEM.OUT.PRINTLN ("Time Consuming:" + (Endnano-startnano)/(1000*1000)) + "MS");
 System.out.println ("ThreadCount =" + (threadcount) + ";");
 System.out.println ("Loopcount =" + (Loopcount) + ";");

 System.out.println ("sum =" + (sum) + ";");
 Thread class Atomicintegerthread extends thread {private Countdownlatch latch = null;
 Private Countdownlatch latchdown = null;

 private int loopcount;
  Public Atomicintegerthread (Countdownlatch latch, countdownlatch latchdown, int loopcount) {this.latch = latch;
  This.latchdown = Latchdown;
 This.loopcount = Loopcount; @Override public void Run () {
  Wait for signal sync try {this.latch.await ();
  catch (Interruptedexception e) {e.printstacktrace ();
  }//for (int i = 0; i < Loopcount; i++) {counter_integer.getandincrement ();
 //Notification Descending 1 times latchdown.countdown ();
 }//thread class Integerthread extends thread {private Countdownlatch latch = null;
 Private Countdownlatch latchdown = null;

 private int loopcount;
  Public Integerthread (Countdownlatch latch, countdownlatch latchdown, int loopcount) {this.latch = latch;
  This.latchdown = Latchdown;
 This.loopcount = Loopcount;
  @Override public void Run () {//wait for signal synchronization try {this.latch.await ();
  catch (Interruptedexception e) {e.printstacktrace ();
  }//for (int i = 0; i < Loopcount; i++) {count_int++;
 //Notification Descending 1 times latchdown.countdown ();

 }
 }
}

The results of the general PC are similar to the following:

--------------testatomic (); Expected to be equal-------------------time
consuming: 85366ms
threadcount =;
Loopcount = 10000600;
sum = 1000060000;
--------------Testintadd (); Expected to be unequal-------------------time
consuming: 1406ms
threadcount = m;
Loopcount = 10000600;
sum = 119428988;

From this we can see that the atomicinteger operation and the efficiency of the int operation is roughly 50-80 times the same, of course, the int does not consume time, this comparison is to provide a reference.

If it is determined to be single-threaded, then int should be used; and the int in the multithreading under the operation of the efficiency is quite high, 1 billion times only spent 1.5 seconds clock;

(assuming that the CPU is 2GHZ, dual-core 4 threads, the theory max 8GHZ, then there are theoretically 8 billion cycles per second,

1 billion times Java int increase consumes 1.5 seconds, that is 12 billion times operation, calculate down each cycle consumes CPU cycle 12;

Personally feel the efficiency is good, C language should also need more than 4 clock cycle (judge, execute internal code, self-increase judgment, jump)

The premise: The JVM and CPU are not aggressively optimized.

)

and Atomicinteger efficiency is not low, 1 billion times consumed 80 seconds, that 1 million times is about 1 per thousand, 80 milliseconds.

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.