Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140919
  • 博文数量: 19
  • 博客积分: 230
  • 博客等级: 二等列兵
  • 技术积分: 239
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-01 12:47
文章分类
文章存档

2014年(5)

2013年(4)

2012年(10)

分类: Java

2013-03-22 10:51:21

情况描述:我在内存中有存放Map数据,数据是Map>类似这样的,但我在想后台页面编辑时候,修改了数据库,那么我需要更新这个List,但可能会有另一个线程会使用它,在遍历,我最终目的只是让它不要出错。GG!我要求不高
以前我都是先移除remove,然后在put,那天查api的时候发现好像不用这样,直接put就可以了,这个和set有些不一样的地方。模拟代码:

点击(此处)折叠或打开

  1. /**
  2.  *
  3.  * @author YDJX
  4.  * @version 1.0.0
  5.  *
  6.  */
  7. public class MapPutTest {

  8.     public static Map<String, String> map = new HashMap<String, String>();
  9.     public static void main(String[] args){
  10.         PutThread pt = new PutThread();
  11.         UseThread ut = new UseThread();
  12.         map.put("1", "1-2");
  13.         map.put("2", "2-2");
  14.         map.put("3", "3-3");
  15.         map.put("4", "4-4");
  16.         map.put("5", "5-5");
  17.         map.put("6", "6-6");
  18.         map.put("7", "7-7");
  19.         map.put("8", "8-8");
  20.         pt.start();
  21.         ut.start();
  22.         map.put("1", "1-1");
  23.         
  24.         Set<String> set = map.keySet();
  25.         for(String str:set){
  26.             System.out.println("main out->"+str+":"+map.get(str));
  27.         }
  28.     }
  29. }


  30. class PutThread extends Thread{
  31.     public PutThread() {
  32.     }
  33.     @Override
  34.     public void run() {
  35.         System.out.println("start PutThread");
  36.         putValue();
  37.         System.out.println("end PutThread");
  38.     }
  39.     public void putValue(){
  40.         MapPutTest.map.put("10", "10-10");
  41.         MapPutTest.map.put("8", "modify 8");
  42.         try {
  43.             Thread.currentThread().sleep(3*1000L);
  44.             System.out.println("PutThread sleep 3s");
  45.             MapPutTest.map.put("1", "latest modify");
  46.             MapPutTest.map.put("3", "latest modify");
  47.             MapPutTest.map.put("5", "latest modify");
  48.             MapPutTest.map.put("6", "latest modify");
  49.             System.out.println("PutThread have modify Map");
  50.         } catch (InterruptedException e) {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54. }

  55. class UseThread extends Thread{
  56.     public UseThread() {
  57.     }
  58.     @Override
  59.     public void run() {
  60.         System.out.println("start UseThread");
  61.         traversal();
  62.         System.out.println("start traversal");
  63.         Set<String> set = MapPutTest.map.keySet();
  64.         for(String str:set){
  65.             System.out.println("UseThread out->"+str+":"+MapPutTest.map.get(str));
  66.         }
  67.         System.out.println("e nd UseThread");
  68.     }
  69.     public void traversal(){
  70.         
  71.         Set<String> set = MapPutTest.map.keySet();
  72.         int i =0;
  73.         for(String str:set){
  74.             System.out.println(str+":"+MapPutTest.map.get(str));
  75.             try {
  76.                 if(i==4){
  77.                     Thread.currentThread().sleep(4*1000L);
  78.                     System.out.println("UseThread sleep 4s");
  79.                 }
  80.                 i++;
  81.             } catch (InterruptedException e) {
  82.                 e.printStackTrace();
  83.             }
  84.             
  85.         }
  86.     }
  87. }

我在让一个线程去跑 向Map中添加数据、修改Map中的数据,再让另一个线程去遍历,但这里可能有点遗漏的地方,就是我遍历的是Set,这个不知道是不是正确啊,如果有不妥的地方,还希望朋友指出,谢了!这样子跑出来的结果还是偶发的有问题,可能Map是非线程安全的原因
MapPutTest结果.txt 反复测试,虽然偶然出现异常,除了主函数,其它的数据都跑完了,我觉得还是可以接受的,所以我最终就这样更新Map里的数据了。记录一下,免得以后再出现这种问题
阅读(2339) | 评论(0) | 转发(0) |
0

上一篇:mutt收发邮件

下一篇:Debian下安装MySQL

给主人留下些什么吧!~~