引用对象的使用和易产生bug的示例

时间:2023-03-08 15:49:02

本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5481608.html  (Robin)

QuoteTest(引用对象技巧)

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by robin on 2016/4/13.
* 引用型对向操作总结:
* 1.被引用的对象,值改变时,会直接改变引用源的值;
* 2.当引用的对象,改变其引用源时,对其操作,只会改变新的引用源的值,并不会影响之前的引用源的值
* 3.从map中获取的引用不存在时,需要将新的引用put到map中,map中该位置的值,才会被引入
* @author robin
*/
public class QuoteTest { public static void main(String args[]){
Map<String,List<String>> map = new HashMap<String, List<String>>();
for (int i =0;i< 5;i++){
List<String> datalist = new ArrayList<String>();
for (int j=0;j<10;j++){
datalist.add(i*10+""+j);
}
map.put(""+i,datalist);
}
for (List<String> list:map.values()){
System.out.println(listToString(list));
}
System.out.println("----------分隔线1-----------");
List<String> tempList = map.get("3");
tempList.add("avc");
tempList.remove("300");
for (List<String> list:map.values()){
System.out.println(listToString(list));
}
System.out.println("----------分隔线2-----------");
List<String> tempList2 = map.get("2");//tempList 获得map中 key为2的引用
List<String> replaceList = new ArrayList<String>();
tempList2 = replaceList;////tempList 获得其他list的引用,失去map中 key为2的引用,此时对templist2做任何操作,影响的时replaceList引用的区域
tempList2.add("replaceList的值被改变");
for (List<String> list:map.values()){
System.out.println(listToString(list));
}
System.out.println("replaceList的值:"+listToString(replaceList));
System.out.println("----------分隔线3-----------");
List<String> tempList3 = map.get("2");
tempList3 = replaceList;
map.put("2",tempList3);
for (List<String> list:map.values()){
System.out.println(listToString(list));
}
System.out.println("----------分隔线4-----------");
List<String> notExistList = map.get("5");
if(notExistList == null){
notExistList = new ArrayList<String>();
}
notExistList.add("第5行数据添加进来...");
for (List<String> list:map.values()){
System.out.println(listToString(list));
}
System.out.println("----------分隔线5-----------");
List<String> notExistList2 = map.get("6");
if(notExistList2 == null){
notExistList2 = new ArrayList<String>();
}
notExistList2.add("第6行数据添加进来...");
map.put("6",notExistList2);
for (List<String> list:map.values()){
System.out.println(listToString(list));
}
System.out.println("----------分隔线5-----------"); Map<String,Map<String,String>> mapOne = new HashMap<String, Map<String, String>>();
String keyss = "mapTest";
Map<String,String> mapTwo = new HashMap<String, String>();
mapOne.put(keyss,mapTwo);
System.out.println("mapOne的数据:" + mapToString(mapOne));
System.out.println("----------分隔线6-----------");
mapTwo.put("aaa", "aaav");
mapTwo.put("bbb", "bbbv");
mapTwo.put("ccc","cccv");
System.out.println("mapOne的数据:"+mapToString(mapOne));
System.out.println("----------分隔线7-----------");
} private static String listToString(List<String> list){
StringBuilder sb = new StringBuilder("");
for (String s:list){
sb.append("["+s+"] ");
}
return sb.toString();
} private static String mapToString(Map<?,?> map){
StringBuilder sb = new StringBuilder("");
for(Map.Entry entry:map.entrySet()){
sb.append("[key:"+entry.getKey()+";value:"+entry.getValue()+"]");
}
return sb.toString();
} }

---------------------

引用对象易产生的bug:

2016.05.11

关于引用对象,使用不恰当,很容易给自己挖坑,产生非常严重的bug,进而导致整个系统实际业务的崩溃,而且这种bug很难被查出来。(如果日志记录不够详细,分析不够彻底,要找出这种bug,只能靠上帝保佑)

下面先上bug 代码 demo

 import java.util.Iterator;
import java.util.List;
import java.util.Vector; /**
* Created by robin on 2016/5/11.
*
* @author robin
*/
public class QuoteBugDemo { private static List<Integer> publicNums = new Vector<Integer>(); public static void main(String args[]) throws InterruptedException {
initPublicNums();//初始化公共数据源 timeTask(1);//模拟执行定时任务1 timeTask(2);//模拟执行定时任务2 } private static void initPublicNums(){
for (int i =0;i < 10;i++){
publicNums.add(i);
}
} /**
* 这块代码模拟的逻辑:
* 1.每天获取配置好10个的数据源;
* 2.检查这10个数据源,当数据源的数据准备好后,开始执行数据同步;
* 3.从当天的带同步数据源list删除已经同步的数据;
* @param mark
* @throws InterruptedException
*/
private static void timeTask(int mark) throws InterruptedException {
final long start = System.currentTimeMillis();//程序开始运行时间
//每天待同步数据源
List<Integer> dataSources = publicNums;
StringBuffer sb = new StringBuffer("mark("+mark+");公共数据源数目:"+dataSources.size()+";数据源列表[");
for (Integer i:dataSources){
sb.append(i+",");
}
sb.append("]");
System.out.println("日志【"+sb.toString()+"】");
while(true){
long seconds = (System.currentTimeMillis() - start) / 1000;
if(seconds > 15l){
System.out.println("运行超过限定时间:15秒,退出");
break;
}
Iterator<Integer> ite = dataSources.iterator();
while (ite.hasNext()){//
int dataSource = ite.next();
boolean flag = isOk(dataSource);
if(flag){//数据源数据已准备好
System.out.println("对数据源:"+dataSource+"进行数据处理。");//同步数据
ite.remove();//待同步数据源删除该数据源
}
}
if(dataSources.size() != 0){
Thread.sleep(1000);
}else{
break;
}
}
System.out.println("定时任务mark["+mark+"]已经执行完毕");
} /**
* 模拟检查数据源是否OK
* @param dataSource
* @return
*/
private static boolean isOk(int dataSource){
if(dataSource%2 == 0){
return true;
}
return false;
} }

执行结果:

 日志【mark(1);公共数据源数目:10;数据源列表[0,1,2,3,4,5,6,7,8,9,]】
对数据源:0进行数据处理。
对数据源:2进行数据处理。
对数据源:4进行数据处理。
对数据源:6进行数据处理。
对数据源:8进行数据处理。
运行超过限定时间:15秒,退出
定时任务mark[1]已经执行完毕
日志【mark(2);公共数据源数目:5;数据源列表[1,3,5,7,9,]】
运行超过限定时间:15秒,退出
定时任务mark[2]已经执行完毕

定时任务2,执行的时候,数据源只剩1,3,5,7,9。

改进方案:将公共数据源保护起来,仅提供公共数据源的副本:shallow copy和deep copy

核心代码:

 /**
* 改进方案1:获取公共数据源对象的副本
* shallow copy:list中 元素引用 仍然是相同的
* @return
*/
private static List<Integer> getPublicNums(){
List<Integer> clone = new ArrayList<Integer>(publicNums);
return clone;
}

改进后全部代码:

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector; /**
* Created by robin on 2016/5/11.
*
* @author robin
*/
public class QuoteBugDemo { private static List<Integer> publicNums = new Vector<Integer>(); public static void main(String args[]) throws InterruptedException {
initPublicNums();//初始化公共数据源 timeTask(1);//模拟执行定时任务1 timeTask(2);//模拟执行定时任务2 } private static void initPublicNums(){
for (int i =0;i < 10;i++){
publicNums.add(i);
}
} /**
* 改进方案1:获取公共数据源对象的副本
* shallow copy:list中 元素引用 仍然是相同的
* @return
*/
private static List<Integer> getPublicNums(){
List<Integer> clone = new ArrayList<Integer>(publicNums);
return clone;
} /**
* 这块代码模拟的逻辑:
* 1.每天获取配置好10个的数据源;
* 2.检查这10个数据源,当数据源的数据准备好后,开始执行数据同步;
* 3.从当天的带同步数据源list删除已经同步的数据;
* @param mark
* @throws InterruptedException
*/
private static void timeTask(int mark) throws InterruptedException {
final long start = System.currentTimeMillis();//程序开始运行时间
//每天待同步数据源
List<Integer> dataSources = getPublicNums();//改进方案1
StringBuffer sb = new StringBuffer("mark("+mark+");公共数据源数目:"+dataSources.size()+";数据源列表[");
for (Integer i:dataSources){
sb.append(i+",");
}
sb.append("]");
System.out.println("日志【"+sb.toString()+"】");
while(true){
long seconds = (System.currentTimeMillis() - start) / 1000;
if(seconds > 15l){
System.out.println("运行超过限定时间:15秒,退出");
break;
}
Iterator<Integer> ite = dataSources.iterator();
while (ite.hasNext()){//
int dataSource = ite.next();
boolean flag = isOk(dataSource);
if(flag){//数据源数据已准备好
System.out.println("对数据源:"+dataSource+"进行数据处理。");//同步数据
ite.remove();//待同步数据源删除该数据源
}
}
if(dataSources.size() != 0){
Thread.sleep(1000);
}else{
break;
}
}
System.out.println("定时任务mark["+mark+"]已经执行完毕");
} /**
* 模拟检查数据源是否OK
* @param dataSource
* @return
*/
private static boolean isOk(int dataSource){
if(dataSource%2 == 0){
return true;
}
return false;
} }

执行结果:

 日志【mark(1);公共数据源数目:10;数据源列表[0,1,2,3,4,5,6,7,8,9,]】
对数据源:0进行数据处理。
对数据源:2进行数据处理。
对数据源:4进行数据处理。
对数据源:6进行数据处理。
对数据源:8进行数据处理。
运行超过限定时间:15秒,退出
定时任务mark[1]已经执行完毕
日志【mark(2);公共数据源数目:10;数据源列表[0,1,2,3,4,5,6,7,8,9,]】
对数据源:0进行数据处理。
对数据源:2进行数据处理。
对数据源:4进行数据处理。
对数据源:6进行数据处理。
对数据源:8进行数据处理。
运行超过限定时间:15秒,退出
定时任务mark[2]已经执行完毕

已达预期。

------------------------------------------

shallow copy 和 deep copy 的区别详见我的另一篇博客:http://www.cnblogs.com/robinjava77/p/5481874.html