Message.java
import PJSoft.TypedPool.*;
public class Message extends Allocable{
Sender sender;
double[] value=new double[1000];
public Message(){
super();
}
}
Reader.java
import PJSoft.TypedPool.*;
public class Reader extends Thread{
static TypedPool pool;
int max=50;
private Message[] queue = new Message[max];
int f=0, l=max-1;
int alloc_type;
int idx=0;
public Reader(int alloc_type){
super();
this.alloc_type=alloc_type;
if(alloc_type==1){
try{
pool = TypedPoolFactory.obtainPool(Class.forName("Message"), 200);
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
start();
}
public synchronized void send(Message mess){
while(queue[f]!=null) try{wait();}catch(Exception e){}
queue[f]=mess;
f = (f+1) % max;
notify();
}
public synchronized Message read(){
int i;
for(i=(l+1) % max;queue[i]==null;i=(l+1) % max){
try{
wait();
}catch(Exception e){}
}
Message mess=queue[i];
queue[i]=null;
l = (l+1) % max;
notify();
return mess;
}
public void run(){
for(idx=0;!Test.closing ; idx++){
Message mess=read();
if(alloc_type==1){
try{
pool.releaseObject(mess);
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
}
System.out.println("Sender "+alloc_type+": ["+idx+"]");
if(alloc_type==1)
System.out.println("Elements in the pool: " + pool.getCount());
}
public int getCounter(){return idx;}
}
Sender.java
import PJSoft.TypedPool.*;
public class Sender extends Thread{
static TypedPool pool;
Reader reader;
int alloc_type;
public Sender(int alloc_type, String name, Reader reader){
super(name);
this.alloc_type=alloc_type;
this.reader=reader;
if(alloc_type==1){
try{
pool = TypedPoolFactory.obtainPool(Class.forName("Message"), 200);
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
start();
}
public void run(){
for(int i=0;!Test.closing;i++){
Message mess;
if(alloc_type==1)
mess=(Message)pool.obtainObject();
else
mess=new Message();
mess.sender=this;
for(int idx=0;idx<mess.value.length;idx++) mess.value[idx]=(double)i;
reader.send(mess);
}
}
}
Test.java
class CounterTask extends Thread{
long counter=0;
boolean flag=true;
CounterTask(){
start();
}
public void run(){
while(flag){
counter++;
Thread.yield();
}
}
long getCounter(){
flag=false;
return counter;
}
}
public class Test{
public static volatile boolean closing=false;
public static int tasks=5;
public static int repetitions=1;
public static int pause=10000;
public static String typeOfExecution="BOTH";
public static boolean cTask=false;
static Reader r0, r1;
static CounterTask ct;
static public void main(String pars[]){
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
if(pars.length>0) tasks=Integer.parseInt(pars[0]);
if(pars.length>1) pause=Integer.parseInt(pars[1]);
if(pars.length>2) repetitions=Integer.parseInt(pars[2]);
if(pars.length>3) typeOfExecution=pars[3];
if(pars.length>4) cTask=(Integer.parseInt(pars[4])!=0);
if(!(typeOfExecution.equals("BOTH") || typeOfExecution.equals("NEW") || typeOfExecution.equals("POOL"))){
System.out.println("USAGE: Test <tesks> <pause_millis> <repetitions> {NEW | POOL | BOTH} <counter_task>");
System.exit(1);
}
if(cTask) ct=new CounterTask();
if(typeOfExecution.equals("BOTH") || typeOfExecution.equals("NEW")){
r0=new Reader(0);
for(int i=0;i<tasks;i++) new Sender(0, "SENDER NEW "+i, r0);
}
if(typeOfExecution.equals("BOTH") || typeOfExecution.equals("POOL")){
r1=new Reader(1);
for(int i=0;i<tasks;i++) new Sender(1, "SENDER POOL "+i, r1);
}
try{
for(int i=0;i<repetitions;i++){
Thread.sleep(pause);
System.out.print("["+i+"]");
if(typeOfExecution.equals("BOTH") || typeOfExecution.equals("NEW"))
System.out.print(" new: " + r0.getCounter());
if(typeOfExecution.equals("BOTH") || typeOfExecution.equals("POOL"))
System.out.print(" Pool: " + r1.getCounter());
System.out.println("");
}
if(cTask) System.out.println("Counter="+ct.getCounter());
}catch(Exception e){
}finally{
closing=true;
try{Thread.sleep(500*tasks);}catch(Exception x){}
}
}
}