从头认识多线程-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


上面的两种情况就会出现数据不同步的现象。


总结:这一章节主要展示内部类的同步机制跟普通类相同。


这一章节就到这里,谢谢

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

我的github:https://github.com/raylee2015/DeepIntoThread


目录:http://blog.csdn.net/raylee2007/article/details/51204573


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值