从头认识多线程-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
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
相关文章推荐
- Python3写爬虫(四)多线程实现数据爬取
- C#实现多线程的同步方法实例分析
- 浅谈chuck-lua中的多线程
- C#简单多线程同步和优先权用法实例
- C#多线程学习之(四)使用线程池进行多线程的自动管理
- C#多线程编程中的锁系统(三)
- 解析C#多线程编程中异步多线程的实现及线程池的使用
- C#多线程学习之(六)互斥对象用法实例
- 基于一个应用程序多线程误用的分析详解
- C#多线程学习之(三)生产者和消费者用法分析
- C#多线程学习之(一)多线程的相关概念分析
- C#多线程之Thread中Thread.IsAlive属性用法分析
- 分享我在工作中遇到的多线程下导致RCW无法释放的问题
- C#多线程编程之使用ReaderWriterLock类实现多用户读与单用户写同步的方法
- C#控制台下测试多线程的方法
- 21天学习android开发教程之SurfaceView与多线程的混搭
- Ruby 多线程的潜力和弱点分析
- C#中WPF使用多线程调用窗体组件的方法
- C#如何对多线程、多任务管理(demo)
- C#实现多线程的Web代理服务器实例