异步加载

  1. package eu.plumbr.test;  
  2.   
  3. public class HealMe {  
  4.   private static final int SIZE = (int) (Runtime.getRuntime().maxMemory() * 0.6);  
  5.   
  6.   public static void main(String[] args) throws Exception {  
  7.     for (int i = 0; i < 1000; i++) {  
  8.       allocateMemory(i);  
  9.     }  
  10.   }  
  11.   
  12.   private static void allocateMemory(int i) {  
  13.     try {  
  14.       {  
  15.         byte[] bytes = new byte[SIZE];  
  16.         System.out.println(bytes.length);  
  17.       }  
  18.   
  19.       byte[] moreBytes = new byte[SIZE];  
  20.       System.out.println(moreBytes.length);  
  21.   
  22.       System.out.println("I allocated memory successfully " + i);  
  23.   
  24.     } catch (OutOfMemoryError e) {  
  25.       System.out.println("I failed to allocate memory " + i);  
  26.     }  
  27.   }  
  28. }  

在IT行业,碰到问题的第一个反应通常是——“你重启过没”——而这样做可能会适得其反,本文要讲述的就是这样的一个场景。 

接下来要介绍的这个应用,它不仅不需要重启,而且毫不夸张地说,它能够自我治愈:刚开始运行的时候它可能会碰到些挫折,但会渐入佳境。

转自:itxxz.com

posted @ 2014-12-16 16:47  poetdog  阅读(108)  评论(0)    收藏  举报