多线程?帮帮忙,谢谢!!!
public class DeadLock implements Runnable{
public static void main(String[] args){
new DeadLock();
}
A a=new A();
B b=new B();
public DeadLock(){
Thread.currentThread().setName("MainThread");
new Thread(this).start();
a.foo(b);
System.out.println("back in maint hread");
}
public void run(){
Thread.currentThread().setName("OtherThread");
b.bar(a);
System.out.println("back in other thread");
}
}
class A{
String name=Thread.currentThread().getName(); //问题在这,我知道该句应该在foo()里面,为什么下面打印的name都是main?
synchronized void foo(B b){
System.out.println(name+" enter A.foo()");
try{
Thread.sleep(100);
}
catch(InterruptedException e){
System.out.println(e.getMessage());
}
System.out.println(name+" trying to call A.last()");
b.last();
}
synchronized void last(){
System.out.println("inside A.last()");
}
}
class B{
String name=Thread.currentThread().getName();//问题在这,我知道该句应该在foo()里面,为什么下面打印的name都是main?
synchronized void bar(A a){
System.out.println(name+" enter B.bar()");
try{
Thread.sleep(100);
}
catch(InterruptedException e){
System.out.println(e.getMessage());
}
System.out.println(name+" trying to call B.last()");
a.last();
}
synchronized void last(){
System.out.println("inside B.last()");
}
}
问题:我在写死锁的时候把那两句位置弄错了,在这里我想问的是为什么和main有关?
------解决方案-------------------- Thread.currentThread().setName("MainThread");
new Thread(this).start();
因为这个还在main线程中
------解决方案--------------------当你
A a=new A();
B b=new B();
新线程还没开启
String name=Thread.currentThread().getName();
还是在主线程main中
------解决方案-------------------- String name=Thread.currentThread().getName(); //问题在这,我知道该句应该在foo()里面,为什么下面打印的name都是main?
你这句把name 放在了成员变量区,当然就是生成对象的时候就对name赋值了。然后你直接用肯定是main
线程互锁,加sysout调试一下就知道哪儿过不去了。。
Java code
public class DeadLock implements Runnable{
public static void main(String[] args) throws Exception{
new DeadLock();
}
A a=new A();
B b=new B();
public DeadLock() throws Exception{
Thread.currentThread().setName("MainThread");
new Thread(this).start();//a new Thread
// Thread.sleep(1000);
a.foo(b); //MainThread Thread
System.out.println("back to main hread"); //never executed
}
public void run(){
Thread.currentThread().setName("OtherThread");
b.bar(a);
System.out.println("back to other thread");
}
}
class A{
String name; //问题在这,我知道该句应该在foo()里面,为什么下面打印的name都是main?
synchronized void foo(B b){
name =Thread.currentThread().getName(); //每次调用的时候获取当前线程
System.out.println(name+" enter A.foo()");
try{
Thread.sleep(100);
}
catch(InterruptedException e){
System.out.println(e.getMessage());
}
System.out.println(name+" trying to call A.last()");
b.last();
}
synchronized void last(){
System.out.println("inside A.last()");
}
}
class B{
// String name=Thread.currentThread().getName();//问题在这,我知道该句应该在foo()里面,为什么下面打印的name都是main?
String name;
synchronized void bar(A a){
name =Thread.currentThread().getName();
System.out.println(name+" enter B.bar()");
try{
Thread.sleep(100);
}
catch(InterruptedException e){
System.out.println(e.getMessage());
}
System.out.println(name+" trying to call B.last()");
a.last();
}
synchronized void last(){
System.out.println("inside B.last()");
}
}