这一章节主要讨论内部类的同步机制跟普通类相同。
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