您的位置:首页 > 其它

内部类问题

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! ");

}
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: