【发布时间】:2012-04-02 09:44:52
【问题描述】:
我正在尝试将在一分钟内获得的类 Message 的所有对象存储到树形图中,并将其精确时间作为键。一分钟完成后,我试图中断线程并将映射序列化为 @ 987654321@ 到我要返回到调用类的字节[]。
public class StoreMessage {
private static long start_nanotime=System.nanoTime();
private static Thread thisThread = Thread.currentThread();
private static int timeToRun = 60000; // 1 minute
private static byte[] b=null;
public static Map <Long,Message> map1=new TreeMap<Long,Message>();
public static byte[] store(Message message){
new Thread(new Runnable(){
public void run(){
try{
sleep(timeToRun);
thisThread.interrupt();
b=serializer.serialize(map1);
}
catch(Exception e){
e.printStackTrace();
}
}
}).start();
while (!Thread.interrupted()) {
long precise_time=TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis())+(System.nanoTime()-start_nanotime);
map1.put(precise_time, message);
}
return b;
}
}
为什么这段代码不起作用?
它给了我Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space
【问题讨论】:
标签: java multithreading time treemap