java线程——死锁
来源:互联网 发布:软件测试的工资待遇 编辑:程序博客网 时间:2024/06/17 01:11
/**
* 注意:线程锁定的对象为当前类的事例
* @author ankun
*
*/
public class Thread_DeadLock {
public static void main(String[] args) {
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();
thread1.setThread2(thread2) ;
thread2.setThread1(thread1) ;
thread1.start() ;
thread2.start();
}
}
class Thread1 extends Thread {
Thread2 thread2 ;
public void setThread2(Thread2 thread2) {
this.thread2 = thread2;
}
public void run(){
this.perform1(thread2);
}
//线程调用此方法时,相当于调用了synchronized(Thread1Obj),即锁定了当前的Thread1Obj事例
public synchronized void perform1(Thread2 thread2){
System.out.println("thread1 is running!...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(thread2) { //锁定另一对象
thread2.getClass() ;
}
System.out.println("thread1 is over!");
}
}
class Thread2 extends Thread {
Thread1 thread1 ;
public void setThread1(Thread1 thread1) {
this.thread1 = thread1;
}
public void run(){
this.perform2(thread1) ;
}
public synchronized void perform2(Thread1 thread1){
System.out.println("thread2 is running!...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(thread1) {
thread1.getClass() ;
}
System.out.println("thread2 is over!");
}
}
* 注意:线程锁定的对象为当前类的事例
* @author ankun
*
*/
public class Thread_DeadLock {
public static void main(String[] args) {
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();
thread1.setThread2(thread2) ;
thread2.setThread1(thread1) ;
thread1.start() ;
thread2.start();
}
}
class Thread1 extends Thread {
Thread2 thread2 ;
public void setThread2(Thread2 thread2) {
this.thread2 = thread2;
}
public void run(){
this.perform1(thread2);
}
//线程调用此方法时,相当于调用了synchronized(Thread1Obj),即锁定了当前的Thread1Obj事例
public synchronized void perform1(Thread2 thread2){
System.out.println("thread1 is running!...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(thread2) { //锁定另一对象
thread2.getClass() ;
}
System.out.println("thread1 is over!");
}
}
class Thread2 extends Thread {
Thread1 thread1 ;
public void setThread1(Thread1 thread1) {
this.thread1 = thread1;
}
public void run(){
this.perform2(thread1) ;
}
public synchronized void perform2(Thread1 thread1){
System.out.println("thread2 is running!...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(thread1) {
thread1.getClass() ;
}
System.out.println("thread2 is over!");
}
}
- java线程——死锁
- java线程——死锁
- Java——线程死锁
- java线程状态——死锁
- [线程]——死锁
- 学习笔记——JAVA线程<5>线程的死锁
- Java线程和多线程(九)——死锁
- java再复习——线程的死锁问题
- Java线程安全问题——同步和死锁
- 多线程——线程死锁
- 线程(四)—死锁
- java线程死锁例子
- java模拟线程死锁
- java线程 死锁实例
- java 线程死锁
- Java多线程:线程死锁
- java 线程死锁模拟
- java线程死锁程序
- 今天的问题
- poj_3268 Silver Cow Party
- 一个关于输入系统的想法
- POJ 1837 Balance(DP)
- 在Eclipse中配置NDK自动编译环境builders
- java线程——死锁
- 构造函数与析构函数执行顺序及其与虚函数关系
- Android import_includes export_includes
- 管程机制浅析
- 基于 canvas 将图片转化成字符画
- nyoj 127&&nyoj 170
- Git详解之 Git 标签
- uva185
- SHARP的LQ043Y1DX01