1。wait()与notifyAll()方法 是不是都要在synchrosized关键字里写?若不写是否不起作用?2。wait()方法让线程等待了,为什么notifyAll()叫不醒???希望各位高手帮忙说说,新手可以学习学习 大家互相讨论我有个测试的小程序 代码如下:public class ThreadTest
{
public ThreadTest()
{
new Thread1().start();
new Thread2().start();
}
public static void main(String [] args)
{
new ThreadTest();
}
}class Thread1 extends Thread
{
public synchronized void run()
{
int count=0;
while(true)
{
try
{
this.sleep(400);
}catch(Exception ex){}
if(count%2==0) //count除2等于0 就唤醒所有的
{
try
{
this.notifyAll();
}catch(Exception ex){}
}
System.out.println("Thread1 Print:"+count);
count++;
}
}
}class Thread2 extends Thread
{
public synchronized void run()
{
int count=0;
while(true)
{
try
{
this.sleep(400);
}catch(Exception ex){}
if(count%4==0) //count除4等于0 就让该线程等待
{
try
{
this.wait();
}catch(Exception ex){}
}
System.out.println("Thread2 Print:"+count);
count++;
}
}
}
{
public ThreadTest()
{
new Thread1().start();
new Thread2().start();
}
public static void main(String [] args)
{
new ThreadTest();
}
}class Thread1 extends Thread
{
public synchronized void run()
{
int count=0;
while(true)
{
try
{
this.sleep(400);
}catch(Exception ex){}
if(count%2==0) //count除2等于0 就唤醒所有的
{
try
{
this.notifyAll();
}catch(Exception ex){}
}
System.out.println("Thread1 Print:"+count);
count++;
}
}
}class Thread2 extends Thread
{
public synchronized void run()
{
int count=0;
while(true)
{
try
{
this.sleep(400);
}catch(Exception ex){}
if(count%4==0) //count除4等于0 就让该线程等待
{
try
{
this.wait();
}catch(Exception ex){}
}
System.out.println("Thread2 Print:"+count);
count++;
}
}
}
{
int count=0;
while(true)
{
try
{
this.sleep(400);
}catch(Exception ex){}
if(count%2==0) //count除2等于0 就唤醒所有的
{
try
{
this.notifyAll();
}catch(Exception ex){}
}
System.out.println("Thread1 Print:"+count);
count++;
}
}
永远占有锁怎么可能会让第二个线程运行呢你的这个程序问题太多了
{
public int count=0;
public void addCount()
{
count++;
}
public synchronized void countWait()
{
if(count%4==0)
{
try
{
wait();
}catch(Exception ex){}
}
}
public synchronized void countNotify()
{
if(count%2==0)
{
notifyAll();
}
}
public void display(String str)
{
System.out.println(str+count);
}
}public class ThreadTest
{
public ThreadTest()
{
ThreadPrint tp=new ThreadPrint();
new Thread1(tp).start();
new Thread2(tp).start();
}
public static void main(String [] args)
{
new ThreadTest();
}
}class Thread1 extends Thread
{
ThreadPrint tp=null;
public Thread1(ThreadPrint tp)
{
this.tp=tp;
}
public synchronized void run()
{
while(true)
{
try
{
this.sleep(1000);
}catch(Exception ex){}
tp.countNotify();
tp.display("Thread1 Print:");
tp.addCount();
}
}
}class Thread2 extends Thread
{
ThreadPrint tp=null;
public Thread2(ThreadPrint tp)
{
this.tp=tp;
}
public synchronized void run()
{
while(true)
{
try
{
this.sleep(1000);
}catch(Exception ex){}
tp.countWait();
tp.display("Thread2 Print:");
tp.addCount();
}
}
}先把对象封状起来 然后再使用? 还有锁不同是指什么 小弟初学 还请各位高手指教
你的synchronized方法是 //public synchronized void run()
就说明这个方法的锁是当前的对象,也就是this,
wait notifyAll.是针对同一个锁的等待和唤醒,
而你的两个run方法的锁使两个不同的对象 怎么可能wait notifyAll会起作用呢
要求二年工作经验!