从头认识多线程-2.22 内部类的同步机制跟普通类相同
来源:互联网 发布:淘宝网页不能正常显示 编辑:程序博客网 时间:2024/06/06 02:04
这一章节主要讨论内部类的同步机制跟普通类相同。
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
- 从头认识多线程-2.22 内部类的同步机制跟普通类相同
- 从头认识多线程-2.23 静态内部类的同步机制跟普通类相同
- 从头认识java-18.2 基本的线程机制(6)-使用构造器或者内部类来实现多线程的编码变体
- 从头认识java-8.12 内部类的继承
- 从头认识java-8.9 嵌套类(使用static的内部类)
- 从头认识java-8.1 内部类
- 从头认识java-8.6 匿名内部类
- 从头认识多线程-2.8 同步方法的隐患
- 从头认识多线程-2.24 修改监视器对同步的影响
- 从头认识java-8.3 内部类的.this和.new语法
- 从头认识java-8.5 在方法和作用域里的内部类
- 从头认识java-8.8 使用匿名内部类改造7.8章节的工厂方法
- 从头认识java-8.4 内部类与向上转型
- 从头认识java-8.11 为什么需要内部类?
- 从头认识java-8.11 为什么需要内部类?(2)
- 从头认识Spring-1.9 内部类注入Bean
- 从头认识多线程-2.9 缓解同步方法的隐患-同步代码块
- 从头认识多线程-2.10 同步代码块的特殊现象:一半同步,一半异步
- 最长回文字符串
- eerThcraeSyraniBetadilaV.98
- 拆点最大流-HDU-4292-Food
- Swift Snapkit 看这篇基本就"够"了
- 字符编码Unicode和UTF-8
- 从头认识多线程-2.22 内部类的同步机制跟普通类相同
- python交互模式下方向键、退格键出现乱码及ipython的安装
- install sogou pinyin method on ubuntu
- Window 2003 R2下安装squid透明代理
- 内核log打印级别
- HDU - 2089 不要62 (数位DP&记忆化dfs)水
- 程序启动原理和UIApplication
- Codeblock安装提示:Can't find compiler executable 无法找到编译器
- HDU 2037 circumgyrate the string(字符串翻转)