오라클자바커뮤니티에서 설립한 오엔제이프로그래밍
실무교육센터
(신입사원채용무료교육, 오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷)
public class PoolManager {
private static PoolManager manager = null;
private static PoolManager manager = null;
private int MAX_RESOURCE = 20;
private long TIME_WAIT = 2 * 1000;
private java.util.Hashtable pool = new java.util.Hashtable();
private java.util.Stack freeStack = new java.util.Stack();
private long TIME_WAIT = 2 * 1000;
private java.util.Hashtable pool = new java.util.Hashtable();
private java.util.Stack freeStack = new java.util.Stack();
private PoolManager() {
for(int i=0;i<MAX_RESOURCE;i++){
Integer index = new Integer(i);
pool.put( index, new PoolObject(index) );
freeStack.push(index);
}
}
public static synchronized PoolManager getInstance() {
if ( manager == null ) {
manager = new PoolManager();
}
return manager;
}
public PoolObject getPoolObject() throws Exception {
Integer index = null;
long start = System.currentTimeMillis();
synchronized(this){
while( freeStack.empty() ) {
try{
wait(1 * 1000);
}catch(Exception e){
System.err.println("getPoolObject: awaiked " + e.toString());
}
for(int i=0;i<MAX_RESOURCE;i++){
Integer index = new Integer(i);
pool.put( index, new PoolObject(index) );
freeStack.push(index);
}
}
public static synchronized PoolManager getInstance() {
if ( manager == null ) {
manager = new PoolManager();
}
return manager;
}
public PoolObject getPoolObject() throws Exception {
Integer index = null;
long start = System.currentTimeMillis();
synchronized(this){
while( freeStack.empty() ) {
try{
wait(1 * 1000);
}catch(Exception e){
System.err.println("getPoolObject: awaiked " + e.toString());
}
long end = System.currentTimeMillis();
if ( freeStack.empty() && (end - start) >= TIME_WAIT ) {
throw new Exception("getPoolObject : timeout(" + TIME_WAIT + ") exceed");
}
}
index = (Integer)freeStack.pop();
}
PoolObject obj = (PoolObject)pool.get(index);
return obj;
}
public void release(PoolObject obj) {
synchronized ( this ){
freeStack.push(obj.getIndex());
//notify();
notifyAll();
// 만약 notifyAll() 을 하면 getPoolObject()에서 wait()에서 걸린
// 모든 Thread 가 일시에 일어나게 되어 쓸데없이 전부 while 문을 동시에
// 확인하게되고, 이는 CPU 부하를 야기할 것으로 생각되나,
// 일부 책에서는 notify() 대신 notifyAll() 을 권장하기도 함.
// 두가지를 모두 테스트해 보면 아주 재밌는 현상을 만날 수 있을 것임.
}
//System.out.println("Object-" + obj.getIndex() + ": released.");
}
}
if ( freeStack.empty() && (end - start) >= TIME_WAIT ) {
throw new Exception("getPoolObject : timeout(" + TIME_WAIT + ") exceed");
}
}
index = (Integer)freeStack.pop();
}
PoolObject obj = (PoolObject)pool.get(index);
return obj;
}
public void release(PoolObject obj) {
synchronized ( this ){
freeStack.push(obj.getIndex());
//notify();
notifyAll();
// 만약 notifyAll() 을 하면 getPoolObject()에서 wait()에서 걸린
// 모든 Thread 가 일시에 일어나게 되어 쓸데없이 전부 while 문을 동시에
// 확인하게되고, 이는 CPU 부하를 야기할 것으로 생각되나,
// 일부 책에서는 notify() 대신 notifyAll() 을 권장하기도 함.
// 두가지를 모두 테스트해 보면 아주 재밌는 현상을 만날 수 있을 것임.
}
//System.out.println("Object-" + obj.getIndex() + ": released.");
}
}
import java.io.*;
import java.net.*;
import java.util.*;
import java.net.*;
import java.util.*;
public class PoolTest extends Thread{
private int client = 0;
public PoolTest(int n){
this.client = n;
}
public static void main(java.lang.String[] args) throws Exception {
// PoolObject 의 최대값인 20 보다 많은 요청을 보내어야 테스트가 됨.
int max = 40;
System.out.println("Calling " + max + " threads");
private int client = 0;
public PoolTest(int n){
this.client = n;
}
public static void main(java.lang.String[] args) throws Exception {
// PoolObject 의 최대값인 20 보다 많은 요청을 보내어야 테스트가 됨.
int max = 40;
System.out.println("Calling " + max + " threads");
Thread[] threads = new PoolTest[max];
for(int i=0;i<max;i++){
threads[i] = new PoolTest(i);
threads[i].start();
}
}
public void run(){
PoolManager manager = null;
PoolObject obj = null;
try {
manager = PoolManager.getInstance();
// obj = manager.getPoolObject();
obj.execute();
}
finally{
if ( obj != null ) manager.release(obj);
}
}
}
for(int i=0;i<max;i++){
threads[i] = new PoolTest(i);
threads[i].start();
}
}
public void run(){
PoolManager manager = null;
PoolObject obj = null;
try {
manager = PoolManager.getInstance();
// obj = manager.getPoolObject();
obj.execute();
}
finally{
if ( obj != null ) manager.release(obj);
}
}
}
public class PoolObject {
private static java.util.Random random = new java.util.Random();
private Integer index = null;
public PoolObject(Integer i) {
this.index = i;
}
public void execute() {
System.out.println("Thread-" + index + ": started.");
int second = 0;
try{
// 5 - 15 seconds random sleep.
second = 5 + (int)(random.nextDouble()*10);
Thread.sleep(second*1000);
}catch(Exception e){}
System.out.println("Thread-" + index + ": elapsed=" + second);
}
public Integer getIndex() {
return index;
}
}
private static java.util.Random random = new java.util.Random();
private Integer index = null;
public PoolObject(Integer i) {
this.index = i;
}
public void execute() {
System.out.println("Thread-" + index + ": started.");
int second = 0;
try{
// 5 - 15 seconds random sleep.
second = 5 + (int)(random.nextDouble()*10);
Thread.sleep(second*1000);
}catch(Exception e){}
System.out.println("Thread-" + index + ": elapsed=" + second);
}
public Integer getIndex() {
return index;
}
}
댓글 없음:
댓글 쓰기