从头认识多线程-2.20 synchronized同步方法的无限等待与解决方法
来源:互联网 发布:禁止搜狗输入法云计算 编辑:程序博客网 时间:2024/06/05 11:54
这一章节我们讨论一下synchronized同步方法的无限等待与解决方法。
1.synchronized同步方法的无限等待
package com.ray.deepintothread.ch02.topic_20;/** * * @author RayLee * */public class InfiniteWait {public static void main(String[] args) throws InterruptedException {MyService myService = new MyService();ThreadOne threadOne = new ThreadOne(myService);Thread thread = new Thread(threadOne);thread.start();ThreadTwo threadTwo = new ThreadTwo(myService);Thread thread2 = new Thread(threadTwo);thread2.start();}}class ThreadOne implements Runnable {private MyService myService;public ThreadOne(MyService myService) {this.myService = myService;}@Overridepublic void run() {try {myService.updateA();} catch (InterruptedException e) {e.printStackTrace();}}}class ThreadTwo implements Runnable {private MyService myService;public ThreadTwo(MyService myService) {this.myService = myService;}@Overridepublic void run() {try {myService.updateB();} catch (InterruptedException e) {e.printStackTrace();}}}class MyService {private static int id = 0;// 死循环或者不停的长连接public synchronized void updateA() throws InterruptedException {while (true) {}}public synchronized void updateB() throws InterruptedException {for (int i = 0; i < 5; i++) {System.out.println(Thread.currentThread().getName() + " " + id++);Thread.sleep(100);}}}
输出:
(无)
上面的例子由于线程1一直持有对象的锁,因此线程2无论如何都进不去方法里面。
2.解决方法
(1)使用同步代码块
package com.ray.deepintothread.ch02.topic_20;/** * * @author RayLee * */public class SolutionOfInfiniteWait {public static void main(String[] args) throws InterruptedException {MyService2 myService = new MyService2();ThreadThree threadThree = new ThreadThree(myService);Thread thread = new Thread(threadThree);thread.start();ThreadFour threadFour = new ThreadFour(myService);Thread thread2 = new Thread(threadFour);thread2.start();}}class ThreadThree implements Runnable {private MyService2 myService;public ThreadThree(MyService2 myService) {this.myService = myService;}@Overridepublic void run() {try {myService.updateA();} catch (InterruptedException e) {e.printStackTrace();}}}class ThreadFour implements Runnable {private MyService2 myService;public ThreadFour(MyService2 myService) {this.myService = myService;}@Overridepublic void run() {try {myService.updateB();} catch (InterruptedException e) {e.printStackTrace();}}}class MyService2 {private static int id = 0;private static Object object1 = new Object();private static Object object2 = new Object();public void updateA() throws InterruptedException {synchronized (object1) {while (true) {}}}public synchronized void updateB() throws InterruptedException {synchronized (object2) {for (int i = 0; i < 5; i++) {System.out.println(Thread.currentThread().getName() + " " + id++);Thread.sleep(100);}}}}
输出:
Thread-1 0
Thread-1 1
Thread-1 2
Thread-1 3
Thread-1 4
虽然这个代码清单里面线程2能够进去方法里面,但是线程1所造成的无限等待还是没有解决,而我们平常的服务器最怕就是这种长连接,因为它占掉服务器非常多的资源,不能够有效释放
(2)增加超时计数器
package com.ray.deepintothread.ch02.topic_20;/** * * @author RayLee * */public class SolutionOfInfiniteWait2 {public static void main(String[] args) throws InterruptedException {MyService3 myService = new MyService3();ThreadFive threadFive = new ThreadFive(myService);Thread thread = new Thread(threadFive);thread.start();ThreadSix threadSix = new ThreadSix(myService);Thread thread2 = new Thread(threadSix);thread2.start();}}class ThreadFive implements Runnable {private MyService3 myService;public ThreadFive(MyService3 myService) {this.myService = myService;}@Overridepublic void run() {try {myService.updateA();} catch (InterruptedException e) {e.printStackTrace();}}}class ThreadSix implements Runnable {private MyService3 myService;public ThreadSix(MyService3 myService) {this.myService = myService;}@Overridepublic void run() {try {myService.updateB();} catch (InterruptedException e) {e.printStackTrace();}}}class MyService3 {private int id = 0;private Object object1 = new Object();private Object object2 = new Object();public void updateA() throws InterruptedException {//计数器int count = 0;synchronized (object1) {while (true) {count++;if (count > 3) {System.out.println(Thread.currentThread().getName() + " get out");break;} else {Thread.sleep(100);}}}}public synchronized void updateB() throws InterruptedException {synchronized (object2) {for (int i = 0; i < 5; i++) {System.out.println(Thread.currentThread().getName() + " " + id++);Thread.sleep(100);}}}}
输出:
Thread-1 0
Thread-1 1
Thread-1 2
Thread-0 get out
Thread-1 3
Thread-1 4
从输出我们可以看见,线程0已经在某个边界的时候,通过计数器的超时得到了释放。
总结:这一章节主要讨论了synchronized同步方法的无限等待与解决方法。
这一章节就到这里,谢谢
------------------------------------------------------------------------------------
我的github:https://github.com/raylee2015/DeepIntoThread
目录:http://blog.csdn.net/raylee2007/article/details/51204573
0 0
- 从头认识多线程-2.20 synchronized同步方法的无限等待与解决方法
- 从头认识多线程-2.15 解决由同步的synchronized (newobject()) 引起的脏读的方法
- 从头认识多线程-2.25 synchronized同步方法在jvm是怎样执行的?
- 从头认识多线程-2.18 同步方法与同步静态代码块持有的是不同的锁
- 从头认识多线程-2.8 同步方法的隐患
- 从头认识多线程-2.14 由同步的synchronized (newobject()) 引起的异步现象和脏读
- 从头认识多线程-2.11 通过同步代码块证明synchronized标记的是对象锁
- 从头认识多线程-2.26 synchronized同步代码块在jvm是怎样执行的?
- 从头认识多线程-2.2 synchronized持有对象锁与类锁的相同点
- 从头认识多线程-2.3 synchronized持有对象锁与类锁的不同点
- 从头认识多线程-2.9 缓解同步方法的隐患-同步代码块
- 从头认识多线程-2.24 修改监视器对同步的影响
- 从头认识多线程-2.12 synchronized标记的方法和synchronized(this)标记的代码块锁定的是当前对象
- 【多线程】synchronized同步方法
- 从头认识多线程-2.19 synchronized (new object())同步时各种不同类别的监视器以及其引起的问题(常量池的影响)
- 从头认识多线程-3.3 synchronized某些解决不了的可视性问题,只能使用volatile来解决
- 从头认识多线程-2.10 同步代码块的特殊现象:一半同步,一半异步
- Java多线程--同步与死锁:synchronized;等待与唤醒:wait、notify、notifyAll;生命周期
- 【Android】18、活动被回收了怎么办
- ListView用法(1)
- 【转】一位软件工程师的6年总结
- 《机器学习实战》--k近邻分类算法kNN
- Android Activity的生命周期
- 从头认识多线程-2.20 synchronized同步方法的无限等待与解决方法
- Gmail附件大小及格式限制全面解析
- 计算机各进制之间的转换
- java sdk安装后环境变量的配置
- android开发,@override 编译错误的解决办法
- 嘻哈--软件开发(序)
- 【转】Android Gesture 之触摸屏手势识别
- PreferenceActivity用例
- 【转】GridView如何设置选中项高亮,直到选中其他项时取消高亮