スレッドベース(二)


ThreadTest
package org.wp.thread;

public class ThreadTest {
	public static void main(String args[]) {
		RunnableOne ro = new RunnableOne();
		Thread tr = new Thread(ro);
		tr.start();

		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		ro.shutDown();
	}
}

class RunnableOne implements Runnable {
	private boolean flag = true;

	@Override
	public void run() {
		int i = 0;
		while (flag) {
			System.out.println(i++);
		}
	}

	public void shutDown() {
		flag = false;
	}
}

 
DeadLockTest
package org.wp.thread;

public class DeadLockTest implements Runnable {
	private int flag = 0;
	private static Object o1 = new Object(), o2 = new Object();

	public static void main(String args[]) {
		DeadLockTest dlt1 = new DeadLockTest();
		DeadLockTest dlt2 = new DeadLockTest();
		dlt1.flag = 1;
		dlt2.flag = 2;
		new Thread(dlt1).start();
		new Thread(dlt2).start();
	}

	@Override
	public void run() {
		if (flag == 1) {
			try {
				synchronized (o1) {
					System.out.println(flag);
					Thread.sleep(500);
					synchronized (o2) {
						System.out.println(Thread.currentThread().getName());
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if (flag == 2) {
			try {
				synchronized (o2) {
					System.out.println(flag);
					Thread.sleep(500);
					synchronized (o1) {
						System.out.println(Thread.currentThread().getName());
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

 
SynchronizedTest
package org.wp.thread;

/**
 * synchronized    ,        
 *           A          ,                   B(  C D )
 *                 B(  C D)              A
 *     ,     
 *        :
 * synchronized     synchronized  
 * 
 * @author wp
 * 
 */
public class SynchronizedTest implements Runnable {
	private Timer timer = new Timer();

	public static void main(String args[]) {
		SynchronizedTest st = new SynchronizedTest();
		Thread t1 = new Thread(st);
		Thread t2 = new Thread(st);
		t1.setName("T1");
		t2.setName("T2");
		t1.start();
		t2.start();
	}

	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		timer.registOne(name);
		timer.registTwo(name);
		timer.registThree(name);
	}
}

class Timer {
	private static int num1 = 0;
	private static int num2 = 0;
	private static int num3 = 0;

	// 2,2
	public void registOne(String name) {
		try {
			num1++;
			Thread.sleep(1000);
			System.out.println("registOne  :" + name + "   " + num1
					+ "   Timer   ");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	// 1,2
	public synchronized void registTwo(String name) {
		try {
			num2++;
			Thread.sleep(1000);
			System.out.println("registTwo  :" + name + "   " + num2
					+ "   Timer   ");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	// 1,2
	public void registThree(String name) {
		try {
			synchronized (this) {
				num3++;
				Thread.sleep(1000);
				System.out.println("registThree  :" + name + "   " + num3
						+ "   Timer   ");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}