141多线程-死锁
2015-10-20 14:45
288 查看
/*
死锁:
同步中嵌套同步
*/
class Ticket implements Runnable{
private int tick = 1000;
Object obj = new Object();
boolean flag = true;
public void run(){
if(flag){
while(true){
synchronized(obj){
show();
}
}
}
else
while(true)
show();
}
public synchronized void show(){
synchronized(obj){
if(tick>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()
+" code "+tick--);
}
}
}
}
class Deadlock{
public static void main(String[] args){
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag = false;
t2.start();
}
}
class Test implements Runnable{
private boolean flag;
Test(boolean flag){
this.flag = flag;
}
public void run(){
if(flag){
synchronized(MyLock.locka){
System.out.println("if locka");
synchronized(MyLock.lockb){
System.out.println("if lockb");
}
}
}
else{
synchronized(MyLock.lockb){
System.out.println("MyLock.lockb");
synchronized(MyLock.locka){
System.out.println("MyLock.locka");
}
}
}
}
}
class MyLock{
static Object locka = new Objecet();
static Object lockb = new Objecet();
}
class Deadlock{
public static void main(String[] args){
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
}
相关文章推荐
- 【JAVA多线程问题之死锁】
- 黑马程序员--读写字节数组,随机读写流,集合IO的思维导图,多线程部分,单例设计模式,线程和进程的概念,Java中的线程的创建方式,线程的随机性,线程的状态图,多线程操作共享数据的安全性,死锁
- 多线程(五)——多线程安问题之死锁(Dead Lock )
- java多线程中死锁情况的一个示例
- 多线程程序中死锁的分析和解决方案
- 多线程专题之线程死锁原因之谜
- 多线程的那点儿事(之避免死锁)
- java多线程系列:死锁及检测
- 多线程死锁问题分析和解决[java]
- 一个多线程死锁的问题,一个循环引用的问题
- Ios 死锁 GCD 多线程
- 简单的Java1.4版synchronized多线程的死锁演示
- 【Java多线程】之七:死锁例子
- 黑马程序员——java多线程之死锁和等待唤醒机制
- 黑马程序员———多线程和死锁问题总结
- Java多线程(6)——死锁
- java:多线程-同步与死锁
- 关于多线程死锁的小记
- 多线程死锁调试小技巧
- Java多线程 线程同步与死锁