2022-01-05 星期三 1414 by:enAn
java中jdk1.8,做forEach,需要把外面的变量赋值如int,long,boolean,需要外面定义原子类型的Atomic,如:AtomicBoolean,AtomicInteger
1.[日期:2022-01-05 14:13:18],atomicBoolean:false,atomicBoolean_:false
2.[日期:2022-01-05 14:13:18],atomicLong:0,atomicLong_:0
3.[日期:2022-01-05 14:13:18],atomicInteger:0,atomicInteger_:0
4.[日期:2022-01-05 14:13:18],AtomicReference:null,atomicReference_:null
5.[日期:2022-01-05 14:13:18],AtomicReference<Integer>:null,atomicReferenceInteger_:0
demo
package org.springblade.common.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author:
* @date:2022-01-05
* @desc
*/
public class AbcTest {
public static AtomicBoolean initBoolean(){
return new AtomicBoolean();
}
public static AtomicLong initLong(){
return new AtomicLong();
}
public static AtomicInteger initInteger(){return new AtomicInteger();}
public static AtomicReference initReference(){
return new AtomicReference();
}
public static AtomicReference<Integer> initReferenceInteger(){
return new AtomicReference<Integer>();
}
public static int getDataInt(AtomicInteger atomicInteger){
return atomicInteger.get();
}
public static boolean getDataBoolean(AtomicBoolean atomicBoolean){
return atomicBoolean.get();
}
public static long getDataLong(AtomicLong atomicLong){return atomicLong.get();}
public static Object getDataReference(AtomicReference atomicReference){return atomicReference.get();}
public static int getDataReferenceInteger(AtomicReference<Integer> atomicReferenceInteger){
// 传入的可能为null,再get,就会报错,需要选处理下null值问题
int defaultNum = 0;
String nullStr = "null";
boolean bl = ( null == atomicReferenceInteger || nullStr.equals(atomicReferenceInteger.toString()));
return bl ? defaultNum:atomicReferenceInteger.get();
}
public static String getSystemDate(){
String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
return new SimpleDateFormat(YYYYMMDDHHMMSS).format(new Date());
}
public static void testNeverUse(){
/**
* [日期:2022-01-05 11:26:47],atomicBoolean:false,atomicBoolean_:false
* [日期:2022-01-05 11:26:47],atomicLong:0,atomicLong_:0
* [日期:2022-01-05 11:26:47],atomicInteger:0,atomicInteger_:0
* ====================================================================================
* [日期:2022-01-05 11:33:33],atomicBoolean:false,atomicBoolean_:false
* [日期:2022-01-05 11:33:33],atomicLong:0,atomicLong_:0
* [日期:2022-01-05 11:33:33],atomicInteger:0,atomicInteger_:0
* [日期:2022-01-05 11:33:33],AtomicReference:null,atomicReference_:null
* ====================================================================================
* 1.[日期:2022-01-05 14:13:18],atomicBoolean:false,atomicBoolean_:false
* 2.[日期:2022-01-05 14:13:18],atomicLong:0,atomicLong_:0
* 3.[日期:2022-01-05 14:13:18],atomicInteger:0,atomicInteger_:0
* 4.[日期:2022-01-05 14:13:18],AtomicReference:null,atomicReference_:null
* 5.[日期:2022-01-05 14:13:18],AtomicReference<Integer>:null,atomicReferenceInteger_:0
*/
}
public static void main(String[] args) {
AtomicBoolean atomicBoolean = initBoolean();
AtomicLong atomicLong = initLong();
AtomicInteger atomicInteger = initInteger();
AtomicReference atomicReference = initReference();
AtomicReference<Integer> atomicReferenceInteger = initReferenceInteger();
// ****************************************
boolean atomicBoolean_ = getDataBoolean(atomicBoolean);
long atomicLong_ = getDataLong(atomicLong);
int atomicInteger_ = getDataInt(atomicInteger);
Object atomicReference_ = getDataReference(atomicReference);
Object atomicReferenceInteger_ = getDataReferenceInteger(atomicReferenceInteger);
String systemDate = getSystemDate();
// ****************************************
System.out.println(String.format("1.[日期:%s],atomicBoolean:%s,atomicBoolean_:%s",systemDate,atomicBoolean,atomicBoolean_));
System.out.println(String.format("2.[日期:%s],atomicLong:%s,atomicLong_:%s",systemDate,atomicLong,atomicLong_));
System.out.println(String.format("3.[日期:%s],atomicInteger:%s,atomicInteger_:%s",systemDate,atomicInteger,atomicInteger_));
System.out.println(String.format("4.[日期:%s],AtomicReference:%s,atomicReference_:%s",systemDate,atomicReference,atomicReference_));
System.out.println(String.format("5.[日期:%s],AtomicReference<Integer>:%s,atomicReferenceInteger_:%s",systemDate,atomicReferenceInteger,atomicReferenceInteger_));
}
}