您的位置:首页 > 其它

从头认识多线程-2.23 静态内部类的同步机制跟普通类相同

2016-05-18 21:21 513 查看
这一章节主要讨论静态内部类的同步机制跟普通类相同。
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多线程