예제 #1
0
 public void release() {
   // Delete resources allocated
   Iterator<Resource> iterator = resources.iterator();
   while (iterator.hasNext()) {
     Resource resource = iterator.next();
     resource.release();
     iterator.remove();
   }
 }
예제 #2
0
 public void run() {
   while (true) {
     try {
       Reference<?> ref = queue.remove();
       Resource res = null;
       synchronized (ResourceManager.this) {
         res = refs.get(ref);
         refs.remove(ref);
       }
       res.release();
       ref.clear();
     } catch (InterruptedException ex) {
       break;
     }
   }
 }
예제 #3
0
  @BeforeClass
  public static void initialize() throws InterruptedException {
    Runtime rt = Runtime.getRuntime();

    rt.gc();
    freeMemory_preUse = rt.freeMemory();

    ResourceManager rm = new ResourceManager();

    for (int i = 0; i < 1000; i++) {
      Object key = Integer.toString(i);
      keyList.add(key);
      resourceList.add(rm.getResource(key));
    }

    rt.gc();
    freeMemory_useRes = rt.freeMemory();

    for (int i = 0; i < 1000; i++) {
      Object key = keyList.get(i);
      Resource res = resourceList.get(i);
      res.use(key);
    }

    for (int i = 0; i < 1000; i++) {
      Resource res = resourceList.get(i);
      res.release();
    }

    keyList.clear(); // リソースへの参照をすべて削除
    resourceList.clear(); // リソースへの参照をすべて削除

    rm.shutdown();
    rt.gc();

    rm.reaper.join();

    rt.gc();
    freeMemory_clearRes = rt.freeMemory();

    System.out.println("freeMemory_preUse  :" + freeMemory_preUse);
    System.out.println("freeMemory_useRes  :" + freeMemory_useRes);
    System.out.println("freeMemory_clearRes:" + freeMemory_clearRes);
  }
    @Override
    public void run() {
      String id = Thread.currentThread().getName();

      try {
        System.out.println(id + " acquiring resource..");
        Resource r = SemaphoreTest.getItem();
        System.out.println(id + " got resource: " + r);

        r.use(id);
        // .. more stuff here
        r.release();

        System.out.println(id + " releasing resource: " + r);
        SemaphoreTest.putItem(r);
        System.out.println(id + " released");

      } catch (InterruptedException ix) {
        System.out.println("!! INTERRUPTED");
      }
    }