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
原创粉丝点击