1. 程式人生 > >從頭認識多執行緒-2.22 內部類的同步機制跟普通類相同

從頭認識多執行緒-2.22 內部類的同步機制跟普通類相同

這一章節主要討論內部類的同步機制跟普通類相同。

1.當同步方法的時候

package com.ray.deepintothread.ch02.topic_22;

/**
 * 
 * @author RayLee
 *
 */
public class SynchOfInnerClass {
	class InnerClass {
		private int id = 0;

		public synchronized void service_1() throws InterruptedException {
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + " id:" + id++);
				Thread.sleep(50);
			}
		}

		public synchronized void service_2() throws InterruptedException {
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + " id:" + id++);
				Thread.sleep(100);
			}
		}
	}

	public InnerClass getInnerClass() {
		return new InnerClass();
	}

	public static void main(String[] args) {
		SynchOfInnerClass synchOfInnerClass = new SynchOfInnerClass();
		InnerClass innerClass = synchOfInnerClass.getInnerClass();
		Thread thread = new Thread(new Runnable() {
			public void run() {
				try {
					innerClass.service_1();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		thread.start();
		Thread thread2 = new Thread(new Runnable() {
			public void run() {
				try {
					innerClass.service_2();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		thread2.start();
	}
}

輸出:

Thread-0 id:0
Thread-0 id:1
Thread-0 id:2
Thread-0 id:3
Thread-0 id:4
Thread-1 id:5
Thread-1 id:6
Thread-1 id:7
Thread-1 id:8
Thread-1 id:9

2.當同步程式碼塊的時候

package com.ray.deepintothread.ch02.topic_22;

/**
 * 
 * @author RayLee
 *
 */
public class SynchOfInnerClass2 {
	class InnerClass {
		private int id = 0;
		private Object object = new Object();

		public void service_1() throws InterruptedException {
			synchronized (object) {
				for (int i = 0; i < 5; i++) {
					System.out.println(Thread.currentThread().getName() + " id:" + id++);
					Thread.sleep(50);
				}
			}
		}

		public void service_2() throws InterruptedException {
			synchronized (object) {
				for (int i = 0; i < 5; i++) {
					System.out.println(Thread.currentThread().getName() + " id:" + id++);
					Thread.sleep(100);
				}
			}
		}
	}

	public InnerClass getInnerClass() {
		return new InnerClass();
	}

	public static void main(String[] args) {
		SynchOfInnerClass2 synchOfInnerClass = new SynchOfInnerClass2();
		InnerClass innerClass = synchOfInnerClass.getInnerClass();
		Thread thread = new Thread(new Runnable() {
			public void run() {
				try {
					innerClass.service_1();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		thread.start();
		Thread thread2 = new Thread(new Runnable() {
			public void run() {
				try {
					innerClass.service_2();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		thread2.start();
	}
}

輸出:

Thread-0 id:0
Thread-0 id:1
Thread-0 id:2
Thread-0 id:3
Thread-0 id:4
Thread-1 id:5
Thread-1 id:6
Thread-1 id:7
Thread-1 id:8
Thread-1 id:9

上面的兩種方式都是可以實現同步機制,使得資料保持一致,不會出現髒讀

3.當同步方法與同步程式碼塊同時出現的時候

package com.ray.deepintothread.ch02.topic_22;

/**
 * 
 * @author RayLee
 *
 */
public class SynchOfInnerClass3 {
	class InnerClass {
		private int id = 0;
		private Object object = new Object();

		public void service_1() throws InterruptedException {
			synchronized (object) {
				for (int i = 0; i < 5; i++) {
					System.out.println(Thread.currentThread().getName() + " id:" + id++);
					Thread.sleep(50);
				}
			}
		}

		public synchronized void service_2() throws InterruptedException {
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + " id:" + id++);
				Thread.sleep(100);
			}
		}
	}

	public InnerClass getInnerClass() {
		return new InnerClass();
	}

	public static void main(String[] args) {
		SynchOfInnerClass3 synchOfInnerClass = new SynchOfInnerClass3();
		InnerClass innerClass = synchOfInnerClass.getInnerClass();
		Thread thread = new Thread(new Runnable() {
			public void run() {
				try {
					innerClass.service_1();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		thread.start();
		Thread thread2 = new Thread(new Runnable() {
			public void run() {
				try {
					innerClass.service_2();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		thread2.start();
	}
}

輸出:

Thread-0 id:0
Thread-1 id:1
Thread-0 id:2
Thread-1 id:3
Thread-0 id:4
Thread-0 id:5
Thread-0 id:6
Thread-1 id:6
Thread-1 id:7
Thread-1 id:8

從輸出可以看見,資料不同步,出現髒讀

4.當使用不同監視器來同步程式碼塊的時候

package com.ray.deepintothread.ch02.topic_22;

/**
 * 
 * @author RayLee
 *
 */
public class SynchOfInnerClass4 {
	class InnerClass {
		private int id = 0;
		private Object object1 = new Object();
		private Object object2 = new Object();

		public void service_1() throws InterruptedException {
			synchronized (object1) {
				for (int i = 0; i < 5; i++) {
					System.out.println(Thread.currentThread().getName() + " id:" + id++);
					Thread.sleep(50);
				}
			}
		}

		public synchronized void service_2() throws InterruptedException {
			synchronized (object2) {
				for (int i = 0; i < 5; i++) {
					System.out.println(Thread.currentThread().getName() + " id:" + id++);
					Thread.sleep(100);
				}
			}
		}
	}

	public InnerClass getInnerClass() {
		return new InnerClass();
	}

	public static void main(String[] args) {
		SynchOfInnerClass4 synchOfInnerClass = new SynchOfInnerClass4();
		InnerClass innerClass = synchOfInnerClass.getInnerClass();
		Thread thread = new Thread(new Runnable() {
			public void run() {
				try {
					innerClass.service_1();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		thread.start();
		Thread thread2 = new Thread(new Runnable() {
			public void run() {
				try {
					innerClass.service_2();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		thread2.start();
	}
}

輸出:

Thread-0 id:0
Thread-1 id:1
Thread-0 id:2
Thread-1 id:3
Thread-0 id:3
Thread-0 id:4
Thread-0 id:5
Thread-1 id:5
Thread-1 id:6
Thread-1 id:7

上面的兩種情況就會出現資料不同步的現象。

總結:這一章節主要展示內部類的同步機制跟普通類相同。

這一章節就到這裡,謝謝

------------------------------------------------------------------------------------