问题一:为什么同步方法中synchronized后面不用跟个(某个object),而同步代码块中却有,
问题二:下面这段代码,我是这么理解的:两个线程必定有一个先运行把。假设t1先运行,锁定了resource1然后进入了同步代码块,这时t1线程就会一直运行下去。直至同步代码块运行结束。也就是说在此期间t2根本没机会锁定resource2,怎么出现死锁啊,不知道我哪里理解有问题。请大侠指点。
public class Deadlock {
public static void main(String[ ] args) {
// These are the two resource objects we'll try to get locks for
final Object resource1 = "resource1";
final Object resource2 = "resource2";
int a=0;
// Here's the first thread. It tries to lock resource1 then resource2
Thread t1 = new Thread( ) {
public void run( ) {
// Lock resource 1
synchronized(resource1) {
System.out.println("Thread 1: locked resource 1");
try { Thread.sleep(50); }
catch (InterruptedException e) { }
// Now wait 'till we can get a lock on resource 2
synchronized(resource2) {
while(true){
System.out.println("Thread 1: locked resource 2");
try { Thread.sleep(10000); }
catch (InterruptedException e) { }
}
}
}
}
};
// Here's the second thread. It tries to lock resource2 then resource1
Thread t2 = new Thread( ) {
public void run( ) {
// This thread locks resource 2 right away
synchronized(resource2) {
System.out.println("Thread 2: locked resource 2");
// Then it pauses, just like the first thread.
try { Thread.sleep(50); }
catch (InterruptedException e) { }
synchronized(resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start( );
t2.start( );
}
}
问题二:下面这段代码,我是这么理解的:两个线程必定有一个先运行把。假设t1先运行,锁定了resource1然后进入了同步代码块,这时t1线程就会一直运行下去。直至同步代码块运行结束。也就是说在此期间t2根本没机会锁定resource2,怎么出现死锁啊,不知道我哪里理解有问题。请大侠指点。
public class Deadlock {
public static void main(String[ ] args) {
// These are the two resource objects we'll try to get locks for
final Object resource1 = "resource1";
final Object resource2 = "resource2";
int a=0;
// Here's the first thread. It tries to lock resource1 then resource2
Thread t1 = new Thread( ) {
public void run( ) {
// Lock resource 1
synchronized(resource1) {
System.out.println("Thread 1: locked resource 1");
try { Thread.sleep(50); }
catch (InterruptedException e) { }
// Now wait 'till we can get a lock on resource 2
synchronized(resource2) {
while(true){
System.out.println("Thread 1: locked resource 2");
try { Thread.sleep(10000); }
catch (InterruptedException e) { }
}
}
}
}
};
// Here's the second thread. It tries to lock resource2 then resource1
Thread t2 = new Thread( ) {
public void run( ) {
// This thread locks resource 2 right away
synchronized(resource2) {
System.out.println("Thread 2: locked resource 2");
// Then it pauses, just like the first thread.
try { Thread.sleep(50); }
catch (InterruptedException e) { }
synchronized(resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start( );
t2.start( );
}
}
解决方案 »
- MyBatis3.1.1 Insert 返回主键 long类型 MYSQL 数据库
- 求获得时间的方法
- 求解答!!!!!!!!!!!!!
- hibernate级联删除,请各位给出经验谢谢.大家分享下.跪谢!!!
- DB2 数据批量导入
- struts-2.1.6的struts.xml一个配置问题
- 如何捕获jsp中的exception?
- 为什么tomcat改变了root目录后,不能在该目录的WEB-INF加web。xml
- 我需要继承HttpSession吗?
- Java web 中连接sql server2014数据库始终显示报错
- 8583协议里的位图怎么处理
- 部署在tomcat里面的程序是不是线程多了前台打开时非常缓慢
法,除非第一个方法完成了自己的工作,并解除锁定。
你看看你的while(true){
System.out.println("Thread 1: locked resource 2");
try { Thread.sleep(10000); }
catch (InterruptedException e) { }
}while(true),不死锁才怪
一般synchronized的方法都会定义为static的