java中jdk1.8,做forEach,需要把外面的变量赋值如int,long,boolean,需要外面定义原子类型的Atomic类型,AtomicBoolean,AtomicInteger

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_));
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值