为什么,wait 时,c的锁没解开,
而thread2,进得不到c的锁/
我不知道?????import java.lang.Thread;//sharing class need to lock in each use it classclass publicc{
int i=1;}//thread1 call wait()class thread1 extends Thread{
publicc c;
public thread1(publicc c){
this.c=c;
this.start();
}
public void run(){
synchronized (c)
{
synchronized (this){
try{
System.out.println("thread1 is waiting!");
this.wait(); }catch(InterruptedException e){}
}
} }}//thread2 call notify()class thread2 extends Thread{
publicc c;
public thread1 b;
public thread2(publicc c,thread1 b){
this.c=c;
this.b=b;
this.start();
}
public void run(){
System.out.println("thread2 is running!");
synchronized(c){
synchronized(b){
System.out.println("thread2 is notify !");
b.notifyAll();
}
}
}}
public class Test {
public Test() {
} public static void main(String[] args) {
Test test = new Test();
publicc c=new publicc();
thread1 t1=new thread1(c);
thread2 t2=new thread2(c,t1);
}
}
而thread2,进得不到c的锁/
我不知道?????import java.lang.Thread;//sharing class need to lock in each use it classclass publicc{
int i=1;}//thread1 call wait()class thread1 extends Thread{
publicc c;
public thread1(publicc c){
this.c=c;
this.start();
}
public void run(){
synchronized (c)
{
synchronized (this){
try{
System.out.println("thread1 is waiting!");
this.wait(); }catch(InterruptedException e){}
}
} }}//thread2 call notify()class thread2 extends Thread{
publicc c;
public thread1 b;
public thread2(publicc c,thread1 b){
this.c=c;
this.b=b;
this.start();
}
public void run(){
System.out.println("thread2 is running!");
synchronized(c){
synchronized(b){
System.out.println("thread2 is notify !");
b.notifyAll();
}
}
}}
public class Test {
public Test() {
} public static void main(String[] args) {
Test test = new Test();
publicc c=new publicc();
thread1 t1=new thread1(c);
thread2 t2=new thread2(c,t1);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货