import java.io.*;
import java.util.Vector;
import java.util.concurrent.*;

class VectorTest
{
	static private Vector vect=new Vector();
	static private final int MaxNumber=1000;
	static private final int ThreadNumber=10;
	static private CyclicBarrier barrier=new CyclicBarrier(ThreadNumber);
	static private int finished=0;
	public static void main(String args[])
	{
		MyThread threads[]=new MyThread[ThreadNumber];
		int numberArray[]=new int[ThreadNumber];
		for (int i=0; i<ThreadNumber; i++)
		{
			threads[i]=new MyThread(i);
			threads[i].start();
			numberArray[i]=0;
		}
	
		try
		{
			while (finished<ThreadNumber)
			{
				Thread.sleep(1000);
			}
		}
		catch (Exception er)
		{

		}
		
		for (int i=0; i<vect.size(); i++)
		{
			IntClass theObject=(IntClass)(vect.elementAt(i));
			int temp=theObject.id;
			if (temp>=ThreadNumber)
			{
				System.out.println("unexpected error");
			}
			else
			{
				numberArray[temp]++;
			}
		}
		for (int i=0; i<ThreadNumber; i++)
		{
			System.out.println("thread "+i+" finished "+numberArray[i]);
		}
		
	}
	static class IntClass
	{
		public int id;
		public IntClass(int theID)
		{
			id=theID;
		}
	}

	static class MyThread extends Thread
	{
		private IntClass myID;
		public MyThread(int id)
		{
			myID=new IntClass(id);
		}
			
		public void run()
		{
			int counter=0;
			try
			{
				barrier.await();
		
				while (counter<MaxNumber)
				{
					Thread.sleep(10);
					vect.addElement(myID);
					Thread.sleep(10);
					counter++;
				}

				System.out.println(myID+ " finish "+counter+" times");
				finished++;
			}
			catch (Exception er)
			{
			}
		}
	}
			
}			

