12-24java面向对象之同步和死锁
来源:互联网 发布:faker哭了知乎 编辑:程序博客网 时间:2024/06/05 18:40
案例1
设计一个线程操作类,要求可以产生三个线程对象,并可以设置三个线程的休眠时间
分析:
1.使用Thread类实现
class MyThread extends Thread{//封装属性private String name ;//定义该线程的名称private int time;//定义休眠时间//构造方法public MyThread(String name , int time){super(name);this.time = time;}//覆写run方法public void run(){System.out.println(Thread.currentThread().getName() + "休眠开始");try{Thread.currentThread().sleep(this.time);}catch (InterruptedException e){System.out.println(Thread.currentThread().getName() +"休眠中断");}System.out.println(Thread.currentThread().getName() + "休眠结束" + "持续:" + this.time + "ms");}}public class TestThread16{public static void main(String[] args){MyThread mt1 = new MyThread("线程A",1000);mt1.start();MyThread mt2 = new MyThread("线程B",2000);mt2.start();MyThread mt3 = new MyThread("线程C",3000);mt3.start();}}
1.使用Runnable接口实现
class MyThread implements Runnable{//封装属性private String name ;private int time ;//构造方法public MyThread(String name , int time){this.name = name ;this.time = time ;}public String getName(){return this.name ;}//覆写run方法public void run(){System.out.println(this.name + "休眠开始");try{Thread.currentThread().sleep(this.time);}catch (InterruptedException e){System.out.println(this.name +"休眠中断");}System.out.println(this.name + "休眠结束" + "持续:" + this.time + "ms");}}public class TestThread17{public static void main(String[] args){MyThread mt1 = new MyThread("线程A",1000);new Thread(mt1).start() ;MyThread mt2 = new MyThread("线程B",2000);new Thread(mt2).start() ;MyThread mt3 = new MyThread("线程C",3000);new Thread(mt3).start() ;}}
同步
问题的引出:把各个售票点理解为线程,那么各个线程需要共享资源。如果引入延时,系统可能存在负数的问题。
class Sale implements Runnable{private int ticket = 5;public void run(){for (int i =0;i<50 ; ++i ){if (ticket>0){System.out.println("买票成功,还剩下票数 ticcket=" + ticket--);}}}}public class TestChronized1 {public static void main(String[] args) {Sale s = new Sale();Thread th1 = new Thread(s);Thread th2 = new Thread(s);Thread th3 = new Thread(s);th1.start();th2.start();th3.start();}}
下面引入延时
class Sale implements Runnable{private int ticket = 5;public void run(){for (int i =0;i<50 ; ++i ){if (ticket>0){try{Thread.sleep(500);}catch (InterruptedException e){e.printStackTrace();}System.out.println("买票成功,还剩下票数 ticcket=" + ticket--);}}}}public class TestChronized1 {public static void main(String[] args) {Sale s = new Sale();Thread th1 = new Thread(s);Thread th2 = new Thread(s);Thread th3 = new Thread(s);th1.start();th2.start();th3.start();}}
结果
要想解决数据共享问题——使用同步代码块或同法方法。
同步代码块
class Sale implements Runnable{private int ticket = 5;public void run(){for (int i =0;i<50 ; ++i ){synchronized (this){if (ticket>0){try{Thread.sleep(500);}catch (InterruptedException e){e.printStackTrace();}System.out.println("买票成功,还剩下票数 ticcket=" + ticket--);}}}}}public class TestChronized2 {public static void main(String[] args) {Sale s = new Sale();Thread th1 = new Thread(s);Thread th2 = new Thread(s);Thread th3 = new Thread(s);th1.start();th2.start();th3.start();}}
同步代码块:
Synchronized (同步对象){}
其中同步对象一般使用this代替
同步方法
class Sale implements Runnable{private int ticket = 5;public void run(){for (int i =0;i<50 ; ++i ){saleTicket();}}public synchronized void saleTicket(){if (ticket>0){try{Thread.sleep(500);}catch (InterruptedException e){e.printStackTrace();}System.out.println("买票成功,还剩下票数 ticcket=" + ticket--);}}}public class TestChronized3 {public static void main(String[] args) {Sale s = new Sale();Thread th1 = new Thread(s);Thread th2 = new Thread(s);Thread th3 = new Thread(s);th1.start();th2.start();th3.start();}}
结果完全相同
Synchronized 方法返回值 方法() {}
死锁
不同的线程在互相等待
只要数据共享就需要同步
过分同步就会产生死锁
0 0
- 12-24java面向对象之同步和死锁
- Java多线程之线程同步和死锁
- java线程同步之死锁
- 多线程之同步和死锁
- java多线程-同步和死锁
- JAVA面向对象之对象和类
- Java多线程之同步与死锁
- java线程同步之死锁问题
- Java多线程之同步与死锁
- java学习之路----同步于死锁
- [Java]多线程之同步及死锁
- Java面向对象之重载和重写
- Java面向对象之继承和重写
- 面试问题之线程同步和死锁
- 关于java的同步和死锁
- java多线程的同步和死锁
- Java同步和死锁的实例分析
- java之面向对象
- Android开发学习笔记一:Android启动界面的简单实现
- struts2中的if标签判断
- UVA - 591 Box of Bricks (水题)
- android handler计时器 Timer+handler
- tiny6410按键驱动(五)---同步互斥阻塞
- 12-24java面向对象之同步和死锁
- dedecms5.7文章内容页调用tag关键词并增加超链接的方法
- JSON
- uva10891 Game of Sum
- Java基础——内省、注解、类加载器
- 输入10个数字并且排序后输出(汇编)
- [LeetCode] Rotate Image 旋转图像
- 手斧Linux – 从LFS到Funtoo (23)
- ArcEngine数据删除几种方法和性能比较