import java.util.ArrayList;
import java.util.Random;

public class ThreadExtendsTest extends Thread{

	private static int index = 0;

	private int id = -1;
	public ThreadExtendsTest(int id){
		this.id = id;
	}

	public void run(){
		System.out.println( id + "¹ø ¾²·¹µå µ¿ÀÛ Áß..." );
		Random r = new Random(System.currentTimeMillis());
		try {
			long s = r.nextInt(3000); // 3ÃÊ³»·Î ³¡³»ÀÚ.
			Thread.sleep(s); // ¾²·¹µå¸¦ Àá½Ã ¸ØÃã
			setIndex();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println( id + "¹ø ¾²·¹µå µ¿ÀÛ Á¾·á..." );
	}

	public synchronized static void setIndex(){
		index++; // index º¯¼ö¸¦ Áõ°¡½ÃÅµ´Ï´Ù.
	}

	public static void main(String[] args) {

		System.out.println("Start main method.");

		ArrayList<Thread> threadList = new ArrayList<Thread>();

		for(int i = 0 ; i < 10 ; i++ ){
			ThreadExtendsTest test = new ThreadExtendsTest(i);

			test.start(); // ÀÌ ¸Þ¼Òµå¸¦ ½ÇÇàÇÏ¸é Thread ³»ÀÇ run()À» ¼öÇàÇÑ´Ù.
			threadList.add(test); // »ý¼ºÇÑ ¾²·¹µå¸¦ ¸®½ºÆ®¿¡ »ðÀÔ
		}

		for(int i = 0 ; i < threadList.size(); i++){
			try {
				threadList.get(i).join(); // ¾²·¹µåÀÇ Ã³¸®°¡ ³¡³¯¶§±îÁö ±â´Ù¸³´Ï´Ù.
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		System.out.println("current Index: "+ index); // indexÀÇ °ªÀ» ¹ÝÈ¯ÇÕ´Ï´Ù.
		System.out.println("End main method.");
	}


}
