Thread(多线程 死锁 同步中嵌套同步,并且锁不同)

来源:互联网 发布:九九乘法表c语言 编辑:程序博客网 时间:2024/04/30 05:41
package process;/* *死锁: *同步中嵌套同步,并且锁不同 */class Ticket5 implements Runnable{public static int tick = 1000;Object obj = new Object();boolean flag = true;int i = 0;@Overridepublic void run() {if(flag){while(true){synchronized(obj){show();}}}else{show();}}public synchronized void show(){synchronized(obj){if(tick > 0){System.out.println(Thread.currentThread().getName() + "sale: " + tick--);}}}}public class DeadLockDemo {public static void main(String[] args){Tickets t = new Tickets();Thread t1 = new Thread(t);Thread t2 = new Thread(t);t1.start();t2.start();}}

0 0
原创粉丝点击