001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.pool.demo;
010:
011: import com.completex.objective.components.log.adapter.StdErrorLogAdapter;
012: import com.completex.objective.components.pool.impl.ResourceLimitingPool;
013:
014: /**
015: * @author Gennady Krizhevsky
016: */
017: public class ResourceLimitingPoolDemo {
018:
019: public static void main(String args[]) throws InterruptedException {
020: ResourceLimitingPoolDemo demo = new ResourceLimitingPoolDemo();
021: demo.demoShrink();
022: demo.demoWait();
023: demo.demoWait02();
024: }
025:
026: public void demoShrink() throws InterruptedException {
027: System.out.println("Enter demoShrink");
028: try {
029: StdErrorLogAdapter logAdapter = StdErrorLogAdapter
030: .newLogInstance();
031: logAdapter.setTraceEnabled(true);
032: ResourceLimitingPool pool = new ResourceLimitingPool(
033: new ResourceFactoryImpl(), 10, 10000, logAdapter);
034: pool.setShrinkSize(2);
035: pool.setLog(logAdapter);
036: pool.start();
037: int secs = 15;
038: int n = 9;
039: Object[] resources = new Object[n];
040: for (int i = 0; i < n; i++) {
041: resources[i] = pool.acquireResource();
042: }
043: for (int i = 0; i < n; i++) {
044: pool.releaseResource(resources[i]);
045: }
046: System.out.println("Acquired " + n
047: + " resources: initial pool size [" + pool.size()
048: + "]");
049: System.out.println("Wait " + secs + " secs");
050: Thread.sleep(secs * 1000);
051:
052: System.out.println("Final pool size [" + pool.size() + "]");
053: pool.shutdown();
054: } finally {
055: System.out.println("Exit demoShrink");
056: }
057:
058: }
059:
060: public void demoWait() throws InterruptedException {
061: System.out.println("Enter demoWait");
062: try {
063: StdErrorLogAdapter logAdapter = StdErrorLogAdapter
064: .newLogInstance();
065: logAdapter.setTraceEnabled(false);
066: ResourceLimitingPool pool = new ResourceLimitingPool(
067: new ResourceFactoryImpl(), 10, 10000, logAdapter);
068: pool.setShrinkSize(2);
069: pool.setLog(logAdapter);
070: pool.start();
071:
072: ConsumerA consumerA = new ConsumerA(pool, 10000);
073: ConsumerB consumerB = new ConsumerB(pool, 2000);
074: consumerA.start();
075: consumerB.start();
076: consumerA.join();
077: consumerB.join();
078: System.out.println("Final pool size [" + pool.size() + "]");
079:
080: pool.shutdown();
081: } finally {
082: System.out.println("Exit demoWait");
083: }
084:
085: }
086:
087: public void demoWait02() throws InterruptedException {
088: System.out
089: .println("Enter demoWait02: demonstrates use of timeout:"
090: + " with short timeout & pool size = 1 only the 1st consumer will be "
091: + "successful, all others will fail");
092: try {
093: StdErrorLogAdapter logAdapter = StdErrorLogAdapter
094: .newLogInstance();
095: logAdapter.setTraceEnabled(false);
096: ResourceLimitingPool pool = new ResourceLimitingPool(
097: new ResourceFactoryImpl(), 1, 10000, 1, logAdapter);
098: pool.setShrinkSize(2);
099: pool.setLog(logAdapter);
100: pool.start();
101:
102: ConsumerA consumerA = new ConsumerA(pool, 10000);
103: ConsumerB consumerB = new ConsumerB(pool, 2000);
104: consumerA.start();
105: consumerB.start();
106: consumerA.join();
107: consumerB.join();
108: System.out.println("Final pool size [" + pool.size() + "]");
109:
110: pool.shutdown();
111: } finally {
112: System.out.println("Exit demoWait02");
113: }
114:
115: }
116:
117: class ConsumerA extends Thread {
118: ResourceLimitingPool pool;
119: long timeToWait;
120:
121: public ConsumerA(ResourceLimitingPool pool, long timeToWait) {
122: this .pool = pool;
123: this .timeToWait = timeToWait;
124: }
125:
126: public void run() {
127: int n = 9;
128: Object[] resources = new Object[n];
129: for (int i = 0; i < n; i++) {
130: resources[i] = pool.acquireResource();
131: System.out.println("ConsumerA acquired " + i
132: + " resources");
133: }
134: System.out
135: .println("ConsumerA acquired " + n + " resources");
136: try {
137: Thread.sleep(timeToWait);
138: } catch (InterruptedException e) {
139: // Do nothing
140: }
141: for (int i = 0; i < n; i++) {
142: pool.releaseResource(resources[i]);
143: System.out
144: .println("ConsumerA released resource; pool size "
145: + pool.size());
146: }
147: }
148: }
149:
150: class ConsumerB extends Thread {
151: ResourceLimitingPool pool;
152: long timeToWait;
153:
154: public ConsumerB(ResourceLimitingPool pool, long timeToWait) {
155: this .pool = pool;
156: this .timeToWait = timeToWait;
157: }
158:
159: public void run() {
160: try {
161: Thread.sleep(timeToWait);
162: } catch (InterruptedException e) {
163: // Do nothing
164: }
165: int n = 9;
166: Object[] resources = new Object[n];
167: for (int i = 0; i < n; i++) {
168: resources[i] = pool.acquireResource();
169: System.out.println("ConsumerB acquired resource");
170: }
171: }
172: }
173:
174: class ResourceFactoryImpl implements
175: com.completex.objective.components.pool.ResourceFactory {
176: public Object createResource() {
177: return new Object();
178: }
179:
180: public void destroyResource(Object resource) {
181: }
182: }
183:
184: }
|