从头认识多线程-2.23 静态内部类的同步机制跟普通类相同
来源:互联网 发布:redis同步mysql 编辑:程序博客网 时间:2024/06/06 02:56
这一章节主要讨论静态内部类的同步机制跟普通类相同。
1.当同步方法的时候
package com.ray.deepintothread.ch02.topic_23;/** * * @author RayLee * */public class SynchOfStaticInnerClass {static class InnerClass {private static int id = 0;public static 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 static 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 static void main(String[] args) {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_23;/** * * @author RayLee * */public class SynchOfStaticInnerClass2 {static class InnerClass {private static int id = 0;private static Object object = new Object();public static 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 static 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 static void main(String[] args) {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_23;/** * * @author RayLee * */public class SynchOfStaticInnerClass3 {static class InnerClass {private static int id = 0;private static Object object = new Object();public static 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 static 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 static void main(String[] args) {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:0
Thread-0 id:1
Thread-0 id:2
Thread-1 id:3
Thread-0 id:4
Thread-0 id:5
Thread-1 id:6
Thread-1 id:7
Thread-1 id:8
从输出可以看见,数据不同步,出现脏读
4.当使用不同监视器来同步代码块的时候
package com.ray.deepintothread.ch02.topic_23;/** * * @author RayLee * */public class SynchOfStaticInnerClass4 {static class InnerClass {private static int id = 0;private static Object object1 = new Object();private static Object object2 = new Object();public static 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 static 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 static void main(String[] args) {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-1 id:0
Thread-0 id:0
Thread-0 id:1
Thread-0 id:2
Thread-1 id:3
Thread-0 id:4
Thread-0 id:5
Thread-1 id:6
Thread-1 id:7
Thread-1 id:8
上面的两种情况就会出现数据不同步的现象。
总结:这一章节主要展示静态内部类的同步机制跟普通类相同。
这一章节就到这里,谢谢
------------------------------------------------------------------------------------
我的github:https://github.com/raylee2015/DeepIntoThread
目录:http://blog.csdn.net/raylee2007/article/details/51204573
- 从头认识多线程-2.23 静态内部类的同步机制跟普通类相同
- 从头认识多线程-2.22 内部类的同步机制跟普通类相同
- 从头认识多线程-2.18 同步方法与同步静态代码块持有的是不同的锁
- 从头认识java-18.2 基本的线程机制(6)-使用构造器或者内部类来实现多线程的编码变体
- 普通内部类和静态内部类的一点区别
- 静态内部类和普通内部类的区别
- Java静态内部类,普通内部类的理解
- 静态内部类类和普通内部类(2)
- 嵌套类(静态内部类)和普通内部类的用法和总结
- 从头认识java-8.12 内部类的继承
- 静态内部类与普通内部类
- 静态内部类和普通内部类
- 从头认识java-8.9 嵌套类(使用static的内部类)
- 普通内部类静态成员
- 从头认识java-8.1 内部类
- 从头认识java-8.6 匿名内部类
- 从头认识多线程-2.8 同步方法的隐患
- 从头认识多线程-2.24 修改监视器对同步的影响
- iOS_11_XMPP在工程中的应用
- [leetcode] 43. Multiply Strings
- HDU 1599 find the mincost route(floyd改/找最小环)
- HDU 1846 Brave Game (巴什博弈)
- canvas 让你呼风唤雨,下雨下雪效果
- 从头认识多线程-2.23 静态内部类的同步机制跟普通类相同
- 快速幂
- addEventListener和removeEventListener
- 聚集运算之group
- 文本域与input窗口中 修改placeHolder 颜色
- 快速排序算法(排序详解)
- iOS蓝牙开发(一)蓝牙相关基础知识
- 快速排序
- sdut oj 2404 super prime(素数筛)