View Single Post
Old 11-19-2004, 02:26 PM   PM User | #6
rynox
New Coder

 
Join Date: Aug 2002
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
rynox is an unknown quantity at this point
Code:
public class rServerRegistrar {

	public Thread[] cthreads;

	public rServerRegistrar(Thread thr) {
		cthreads=new Thread[1];
		cthreads[0]=thr;
	}
	
	public void registerThread(Thread newthread) {
		Thread[] tempthreads=new Thread[cthreads.length+1];
		for(int i=0;i<cthreads.length;i++) {
			tempthreads[i]=cthreads[i];
		}
		tempthreads[cthreads.length]=newthread;
		cthreads=tempthreads;
	}
	
	public void deregisterThread(Thread delthread) {
		Thread[] tempthreads=new Thread[cthreads.length-1];
		int count=0;
		for(int i=0;i<cthreads.length;i++) {
			if(delthread.getName()!=cthreads[i].getName()) {
				tempthreads[count]=cthreads[i];
				count++;
			}
		}
		cthreads=tempthreads;
	}
	
	private void updateThreads() {
		int count=0; // First count the number of alive threads.
		for(int i=0;i<cthreads.length;i++) {
			if(cthreads[i].isAlive()) count++;
		}
		Thread[] temp=new Thread[count];
		count=0;
		for(int i=0;i<cthreads.length;i++) {
			if(cthreads[i].isAlive()) {
				temp[count]=cthreads[i];
				count++;
			}
		}
	}
	
	public Thread[] getClientThreads() {
		updateThreads(); // Return all except first thread.
		Thread[] temp=new Thread[cthreads.length-1];
		for(int i=0;i<temp.length;i++) {
			temp[i]=cthreads[i+1];
		}
		return temp;
	}

}
rynox is offline   Reply With Quote