public class TestDeadLock implements Runnable
{
public int flag = 1;
public Object o1 = new Object ();
public Object o2 = new Object (); public void run () {
System.out.println ( "flag = " + flag); if (flag == 1) {
synchronized (o1) {
try {
Thread.sleep(5000);
}
catch (Exception e) {
e.printStackTrace ();
}
} synchronized (o2) {
System.out.println ("1");
}
} if (flag == 0) {
synchronized (o2) {
try {
Thread.sleep(5000);
}
catch (Exception e1) {
e1.printStackTrace ();
}
} synchronized (o1) {
System.out.println ("0");
}
}
} public static void main(String[] args) {
TestDeadLock tdk1 = new TestDeadLock ();
TestDeadLock tdk2 = new TestDeadLock ();
tdk1.flag = 1;
tdk2.flag = 0; Thread t1 = new Thread (tdk1);
Thread t2 = new Thread (tdk2);
t1.start();
t2.start();
}
}
为什么结果总是
flag = 1
flag = 0
1
0
而没有形成死锁啊?????
解决方案 »
- 巨多字符串,频繁split,内存溢出
- 多线程的 start() , run() ,执行顺序
- Applet如何获取浏览器(不一定是IE)使用的代理和Cookie?
- java子类父类的关系
- 使用JNI技术时,碰到了一个HANDLD变量要传递,没辙了!
- 静态成员变量什么时候进行初始化
- 《core java》上的一段代码,能编译,但是不能运行,一运行就抛出异常。
- 哪里有java api参考帮助,我要得是windows帮助格式的不是html格式的
- 关于Oracle BLOB类型,一个String字符怎么写入BLOB字段?
- 【求助】关于java集合中的问题
- java如何从parser网页上的byte数组(c#端的http)
- 把String数组放入对象数组里
{
public int flag = 1;
public Object o1 = new Object ();
public Object o2 = new Object (); public void run () {
System.out.println ( "flag = " + flag); if (flag == 1) {
synchronized (o1) {
try {
Thread.sleep(5000);
}
catch (Exception e) {
e.printStackTrace ();
}
} synchronized (o2) {
System.out.println ("1");
}
} if (flag == 0) {
synchronized (o2) {
try {
Thread.sleep(5000);
}
catch (Exception e1) {
e1.printStackTrace ();
}
} synchronized (o1) {
System.out.println ("0");
}
}
} public static void main(String[] args) {
TestDeadLock tdk1 = new TestDeadLock ();
TestDeadLock tdk2 = new TestDeadLock ();
tdk1.flag = 1;
tdk2.flag = 0; Thread t1 = new Thread (tdk1);
Thread t2 = new Thread (tdk2); t1.start();
t2.start();
}
}这个的中间
first:you have created two different objects of Class TestDeadLock, they don't need to share a "sharing resource";
second: you didn't really lock o2 if you want to test deadlock try to change the code as bellow:
code=Java]public class TestDeadLock implements Runnable {
public int flag = 1;
public static Object o1 = new Object();
public static Object o2 = new Object(); public void run() {
System.out.println("flag = " + flag + "" + this + ">time:" + System.currentTimeMillis()); if (flag == 1) { // 1, lock o1 first
synchronized (o1) {
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println("1");
}
}
} if (flag == 0) { // 0 lock o2 first
synchronized (o2) {
try {
Thread.sleep(5000);
} catch (Exception e1) {
e1.printStackTrace();
}
synchronized (o1) {
System.out.println("0");
}
}
}
} public static void main(String[] args) {
TestDeadLock tdk1 = new TestDeadLock ();
TestDeadLock tdk2 = new TestDeadLock ();
tdk1.flag = 0;
tdk2.flag = 1; Thread t1 = new Thread(tdk1);
Thread t2 = new Thread(tdk2); t1.start();
t2.start();
}
}[/code]
public int flag = 1;
public static Object o1 = new Object();
public static Object o2 = new Object();public void run() {
System.out.println("flag = " + flag + "" + this + ">time:" + System.currentTimeMillis());if (flag == 1) { // 1, lock o1 first
synchronized (o1) {
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}synchronized (o2) {
System.out.println("1");
}
}
}if (flag == 0) { // 0 lock o2 first
synchronized (o2) {
try {
Thread.sleep(5000);
} catch (Exception e1) {
e1.printStackTrace();
}synchronized (o1) {
System.out.println("0");
}
}
}
}public static void main(String[] args) {
TestDeadLock tdk1 = new TestDeadLock ();
TestDeadLock tdk2 = new TestDeadLock ();
tdk1.flag = 0;
tdk2.flag = 1;Thread t1 = new Thread(tdk1);
Thread t2 = new Thread(tdk2);t1.start();
t2.start();
}
}
public Object o1 = new Object ();
public Object o2 = new Object ();如上3个你定义的变量是成员变量,并非类变量,这在该类的不同实例间显然是互不影响的。用static修饰下就能看到效果了
{
public int flag = 1;
public Object o1 = new Object ();
public Object o2 = new Object (); public void run () {
System.out.println ( "flag = " + flag); if (flag == 1) {
synchronized (o1) {
try {
Thread.sleep(5000);
}
catch (Exception e) {
e.printStackTrace ();
}
} synchronized (o2) {
System.out.println ("1");
}
} if (flag == 0) {
synchronized (o2) {
try {
Thread.sleep(5000);
}
catch (Exception e1) {
e1.printStackTrace ();
}
} synchronized (o1) {
System.out.println ("0");
}
}
} public static void main(String[] args) {
TestDeadLock tdk1 = new TestDeadLock ();
TestDeadLock tdk2 = new TestDeadLock ();
tdk1.flag = 0;
tdk2.flag = 1; Thread t1 = new Thread (tdk1);
Thread t2 = new Thread (tdk2);
t1.start();
t2.start();
}
}
这是我的原代码
用static后,结果是
flag = 1
flag = 1
1
1
为什么会出现这个结果啊?
package example;import java.util.concurrent.atomic.AtomicInteger;public class DeathLockExample {
public static void main(String[] args) {
final AtomicInteger x = new AtomicInteger(0);
final AtomicInteger y = new AtomicInteger(0); new Thread() {
public void run() {
while (true) {
synchronized (x) {
synchronized (y) {
x.addAndGet(1);
}
}
System.err.println("x " + x.toString());
}
}
}.start(); new Thread() {
public void run() {
while (true) {
synchronized (y) {
synchronized (x) {
y.addAndGet(1);
}
}
System.err.println("y " + y.toString());
}
}
}.start();
}
}
{
public int flag = 1;
public Object o1 = new Object ();
public Object o2 = new Object (); public void run () {
System.out.println ( "flag = " + flag); if (flag == 1) {
synchronized (o1) {
try {
Thread.sleep(5000);
}
catch (Exception e) {
e.printStackTrace ();
}
} synchronized (o2) {
System.out.println ("1");
}
} if (flag == 0) {
synchronized (o2) {
try {
Thread.sleep(5000);
}
catch (Exception e1) {
e1.printStackTrace ();
}
} synchronized (o1) {
System.out.println ("0");
}
}
} public static void main(String[] args) {
TestDeadLock tdk = new TestDeadLock (); tdk.flag = 1;
Thread t1 = new Thread (tdk);
tdk.flag = 0;
Thread t2 = new Thread (tdk);
t1.start();
t2.start();
}
}这个应该能试出效果。
{
public int flag = 1;
public Object o1 = new Object ();
public Object o2 = new Object (); public void run () {
System.out.println ( "flag = " + flag); if (flag == 1) {
synchronized (o1) {
try {
Thread.sleep(5000);
}
catch (Exception e) {
e.printStackTrace ();
}
} synchronized (o2) {
System.out.println ("1");
}
} if (flag == 0) {
synchronized (o2) {
try {
Thread.sleep(5000);
}
catch (Exception e1) {
e1.printStackTrace ();
}
} synchronized (o1) {
System.out.println ("0");
}
}
} public static void main(String[] args) {
TestDeadLock tdk = new TestDeadLock ();
Thread t1 = new Thread (tdk);
Thread t2 = new Thread (tdk);
tdk.flag = 1;
t1.start();
tdk.flag = 0;
t2.start(); }
}
不好意思,这样才对!
flag = 0
flag = 0
0
0
public static Object o1 = new Object();
public static Object o2 = new Object(); public void run() {
System.out.println("flag = " + flag); if (flag ==1) {
synchronized (o1) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println("1");
}
}
}
if(flag==0){
synchronized (o2) {
try {
Thread.sleep(1000);
} catch (Exception e1) {
e1.printStackTrace();
}
synchronized (o1) {
System.out.println("0");
}
}
}
} public static void main(String[] args) {
TestDeadLock tdk1 = new TestDeadLock();
TestDeadLock tdk2 = new TestDeadLock();
tdk1.flag = 1;
tdk2.flag = 0; Thread t1 = new Thread(tdk1);
Thread t2 = new Thread(tdk2); t1.start();
t2.start();
}