内部类问题
2016-05-19 10:07
309 查看
package concurrentMap; import java.util.HashMap; import java.util.concurrent.ConcurrentHashMap; public class ConcurrencyHashMapTest { public ConcurrentHashMap<Integer, HashMap<Integer, Integer>> pMsgs = new ConcurrentHashMap<Integer, HashMap<Integer, Integer>>(); public ConcurrencyHashMapTest() { } /** * @param args */ public static void main(String[] args) { ConcurrencyHashMapTest chmt = new ConcurrencyHashMapTest() ; Thread t1 = new Thread(new Sender(chmt.pMsgs,1)) ; //内部类问题报错, No enclosing instance of type ConcurrencyHashMapTest is accessible. Must qualify the allocation with an enclosing instance of type ConcurrencyHashMapTest (e.g. x.new A() where x is an instance of ConcurrencyHashMapTest).
Thread t2 = new Thread(new Sender(chmt.pMsgs,2)) ; Thread t3 = new Thread(new Sender(chmt.pMsgs,2)) ; t1.start() ; t2.start() ; t3.start() ; } class Sender implements Runnable { ConcurrentHashMap<Integer, HashMap<Integer, Integer>> pMsgs; int id = -1 ; public Sender(ConcurrentHashMap<Integer, HashMap<Integer, Integer>> pMsgs, int partition) { this.pMsgs = pMsgs; id = partition ; } @Override public void run() { HashMap<Integer, Integer> partition = new HashMap<Integer, Integer>(); for (int i = 0; i < 10000; i++) partition.put(i, 200); pMsgs.put(id, partition); } } }
</pre><pre code_snippet_id="1688919" snippet_file_name="blog_20160519_1_9021163" name="code" class="java">修改后<pre name="code" class="java">package concurrentMap; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; public class ConcurrencyHashMapTest { public ConcurrentHashMap<Integer, HashMap<Integer, Integer>> pMsgs = new ConcurrentHashMap<Integer, HashMap<Integer, Integer>>(); /** * @param args */ public static void main(String[] args) { ConcurrencyHashMapTest chmt = new ConcurrencyHashMapTest() ; <span style="color:#ff0000;">Thread t1 = new Thread(chmt.new Sender(chmt.pMsgs,1)) ; //内部类必须先定义外部类实例后才能定义! Thread t2 = new Thread(chmt.new Sender(chmt.pMsgs,2)) ; Thread t3 = new Thread(chmt.new Sender(chmt.pMsgs,2)) ;</span> t1.start() ; t2.start() ; t3.start() ; try { RandomAccessFile raf = new RandomAccessFile("F:\\test\\Concurrency.txt", "rw") ; Thread.currentThread().sleep(1000) ; for(Map.Entry<Integer, HashMap<Integer, Integer>> partition : chmt.pMsgs.entrySet()){ System.out.println("Partition " + partition.getKey() + " Size: " + partition.getValue().size() +"\n") ; raf.writeBytes(partition.toString()) ; } raf.close() ; } catch (InterruptedException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } class Sender implements Runnable { ConcurrentHashMap<Integer, HashMap<Integer, Integer>> pMsgs; int part = -1 ; public Sender(ConcurrentHashMap<Integer, HashMap<Integer, Integer>> pMsgs, int partition) { this.pMsgs = pMsgs; this.part = partition ; } @Override public void run() { HashMap<Integer, Integer> partition = new HashMap<Integer, Integer>(); int id = (int) Thread.currentThread().getId() ; for (int i = 0; i < 10000; i++) partition.put(i, id); pMsgs.put(part, partition); System.out.println("Thread " + id + " Partition " + part + " is inserted! "); } } }
相关文章推荐
- SAP Business One 对象类型大全 (Object Type)
- javadoc常用的标记以及生成API文档的方法
- 2、安装Lync Server 2013
- Python:模块(一)
- GoAhead2.5源代码分析之3-用户管理实例(umui.c)
- UILabel 图文混排的简单实现
- java中的volatile和synchronized
- 隐藏TabBar是个累人的活
- 案例分析8
- 保存、另存为地图文件
- SimpleDateFormat问题随记
- 每天laravel-20160810| Container -13
- javascript的事件流
- Promise 常用API
- wireshark数据包格式的基本介绍
- 通过ajax给js成员变量赋值问题
- 未完成的作业
- 每天laravel-20160810| Container -13
- CODEFORCES 484E Sign on Fence
- 九度 1516 调整数组顺序使奇数位于偶数前面