Java AtomicInteger, javaatomicinteger

Source: Internet
Author: User

Java AtomicInteger, javaatomicinteger

AtomicInteger, a class that provides atomic operations. In Java, the ++ I and I ++ operations are not thread-safe. When using them, the synchronized keyword is inevitable. AtomicInteger uses a thread-safe addition/subtraction operation interface.



Import java. text. SimpleDateFormat;

Import java. util. Date;
Import java. util. Timer;
Import java. util. TimerTask;
Import java. util. concurrent. ScheduledThreadPoolExecutor;
Import java. util. concurrent. TimeUnit;
Import java. util. concurrent. atomic. AtomicInteger;




/**
* @ Description
* @ Author
* @ Date 9:37:45
*/
Public class ConcurrentAtomicTest {


Private final static AtomicInteger atomicInteger = new AtomicInteger (1 );
Private static volatile int integer = 1 ;;


/**
* @ Description TODO
* @ Return void
* @ Throws
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// TestAtomicInteger ();
TestInteger2 ();


}


Public static void testAtomicInteger (){
String time = new SimpleDateFormat ("HH: mm: ss"). format (new Date ());
System. out. println ("Start time:" + time );

Int temp = atomicInteger. addAndGet (1); // add
AtomicInteger. get (); // get current
System. out. print (temp + ",");

Temp = atomicInteger. decrementAndGet (); // Atomically decrements by one the current value.
System. out. print (temp + ",");

Temp = atomicInteger. getAndIncrement (); // Atomically decrements by one the current value.
System. out. print (temp + ",");

Temp = atomicInteger. intValue (); // Atomically decrements by one the current value.
System. out. print (temp + ",");

AtomicInteger. set (0); // Atomically decrements by one the current value.
Temp = atomicInteger. get ();
System. out. println (temp + ",");

// Result: 2, 1, 2, 0,


ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor (5); // create five execution threads


Runnable runnable = new Runnable (){
@ Override
Public void run (){
String time = new SimpleDateFormat ("HH: mm: ss"). format (new Date ());
AtomicInteger. incrementAndGet ();
System. out. println ("Now Time:" + time + ", integer:" + integer );
}
};


Executor. scheduleWithFixedDelay (runnable, 2, 3, TimeUnit. SECONDS );
}

/**
* Test int plus 1
* @ Description TODO
* @ Return void
* @ Throws
*/
Public static void testInteger2 (){
String time = new SimpleDateFormat ("HH: mm: ss"). format (new Date ());
System. out. println ("Start time:" + time );

Integer ++;
System. out. println ("integer ++:" + integer );

Integer --;
System. out. println ("integer --:" + integer );


ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor (5); // create five execution threads


Runnable runnable = new Runnable (){
@ Override
Public void run (){
String time = new SimpleDateFormat ("HH: mm: ss"). format (new Date ());
Integer = integer + 1;
System. out. println ("Now Time:" + time + ", testInteger2 Integer:" + atomicInteger. get ());
}
};


Executor. scheduleWithFixedDelay (runnable, 2, 3, TimeUnit. SECONDS );
}
// Start time: 10:27:16
// Integer ++: 2
// Integer --: 1
// Now Time: 10:27:18, testInteger2 Integer: 1
// Now Time: 10:27:21, testInteger2 Integer: 1
// Now Time: 10:27:24, testInteger2 Integer: 1
// Now Time: 10:27:27, testInteger2 Integer: 1


}

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.