나는 이렇게 다선정을 이해한다(一)

5378 단어 다중 스레드
package test2;

public class MainService {

	private static MainService instance = new MainService();

	public static MainService getInstance() {
		return instance;
	}
	
	/**
	 *     synchronized (this)
	 * @param parm
	 */
	@SuppressWarnings("static-access")
	public void fun03(String parm) {
		synchronized (this) {
			for (int i = 1; i < 4; i++) {
				StringBuffer buf = new StringBuffer();
				buf.append("    :").append(this).append("\t\t");
				buf.append("    :").append(Thread.currentThread().getName())
				.append("\t\t");
				buf.append("  i :").append(i);
				System.out.println(buf.toString());
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 *     synchronized (this.getClass())
	 * @param parm
	 */
	@SuppressWarnings("static-access")
	public void fun04(String parm) {
		synchronized (this.getClass()) {
			for (int i = 1; i < 4; i++) {
				StringBuffer buf = new StringBuffer();
				buf.append("    :").append(this).append("\t\t");
				buf.append("    :").append(Thread.currentThread().getName())
				.append("\t\t");
				buf.append("  i :").append(i);
				System.out.println(buf.toString());
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

동기화 블록synchronized(this) 사용
다른 하나는 동기화 블록 synchronized (this.getClass () 를 사용합니다.
 
 
테스트 클래스, TestThread03
package test2;

public class TestThread03 {

	public static void test01() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				new MainService().fun03(null);
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				new MainService().fun03(null);
			}
		}).start();
	}

	public static void test02() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				MainService.getInstance().fun03(null);
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				MainService.getInstance().fun03(null);
			}
		}).start();
	}

	public static void main(String[] args) {
		// test01();
		//     :test.MainService@24c21495     :Thread-0   i :1
		//     :test.MainService@41d5550d     :Thread-1   i :1
		//     :test.MainService@24c21495     :Thread-0   i :2
		//     :test.MainService@41d5550d     :Thread-1   i :2
		//     :test.MainService@24c21495     :Thread-0   i :3
		//     :test.MainService@41d5550d     :Thread-1   i :3

		// test02();
		//     :test.MainService@24c21495     :Thread-0   i :1
		//     :test.MainService@24c21495     :Thread-0   i :2
		//     :test.MainService@24c21495     :Thread-0   i :3
		//     :test.MainService@24c21495     :Thread-1   i :1
		//     :test.MainService@24c21495     :Thread-1   i :2
		//     :test.MainService@24c21495     :Thread-1   i :3
	}
}

결과:
서로 다른 대상이 같은 방법을 집행하고 방법은 서로 배척하지 않는다.
같은 대상이 같은 방법을 집행하고 방법은 서로 배척한다.
package test2;

public class TestThread04 {

	public static void test01() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				new MainService().fun04(null);
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				new MainService().fun04(null);
			}
		}).start();
	}

	public static void test02() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				MainService.getInstance().fun04(null);
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				MainService.getInstance().fun04(null);
			}
		}).start();
	}

	public static void main(String[] args) {
		// test01();
		//     :test.MainService@24c21495     :Thread-0   i :1
		//     :test.MainService@24c21495     :Thread-0   i :2
		//     :test.MainService@24c21495     :Thread-0   i :3
		//     :test.MainService@41d5550d     :Thread-1   i :1
		//     :test.MainService@41d5550d     :Thread-1   i :2
		//     :test.MainService@41d5550d     :Thread-1   i :3

		// test02();
		//     :test.MainService@24c21495     :Thread-0   i :1
		//     :test.MainService@24c21495     :Thread-0   i :2
		//     :test.MainService@24c21495     :Thread-0   i :3
		//     :test.MainService@24c21495     :Thread-1   i :1
		//     :test.MainService@24c21495     :Thread-1   i :2
		//     :test.MainService@24c21495     :Thread-1   i :3
	}
}

결과:
같은 종류의 모든 대상이 같은 방법을 집행하면 서로 배척한다.

좋은 웹페이지 즐겨찾기