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++;
}
}
}

解决方案 »

  1.   

    上面的如不加synchronized  无法暂停但是无论怎么弄都无法唤醒线程大家热烈讨论!!!
      

  2.   

    notifyAll 是唤醒当前线程所得到的对象锁的等待线程你的两个线程锁不同当然不能唤醒
      

  3.   

    wait()  使線程進行等待, 直到被通知(notify()或notifyAll())而喚醒, 只能從同步方法(synchrosized)內調用該方法
      

  4.   

    还有你的唤醒条件count%4==0 始终不能修改阿怎么可能唤醒呢
      

  5.   

    你的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++;
    }
    }
    永远占有锁怎么可能会让第二个线程运行呢你的这个程序问题太多了
      

  6.   

    没有synchronized的话sleep也没有意义,加上以后Thread1也不能直接去唤醒Thread2啊,所以Thread2就一直wait了.除非使用公共的数据,比如说count不过没太看懂lz的意思
      

  7.   

    treeroot(旗鲁特)兄,你说的笑死我了,的确是这样。线程目的是为了加快处理速度,他们共享同一个内存空间。
      

  8.   

    楼主的程序是新建了Thread1和Thread2两个对象,它们根本不处于同一个内存空间(Java根本没有这样的说法)。它出现问题的原因是 treeroot(旗鲁特)  所说的,Thread1 Thread2使用的不是同一个锁对象造成的。 
      

  9.   

    大家来看看是不是这个意识?public class ThreadPrint
    {
    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();
    }
    }
    }先把对象封状起来 然后再使用?  还有锁不同是指什么 小弟初学 还请各位高手指教
      

  10.   

    呵呵  愚人节 快乐还没有解决吗锁不同: 就是你要执行sunchronized修饰的代码,就必须获得代码的锁
            你的synchronized方法是   //public synchronized void run()
            就说明这个方法的锁是当前的对象,也就是this,
            wait notifyAll.是针对同一个锁的等待和唤醒,
            而你的两个run方法的锁使两个不同的对象 怎么可能wait notifyAll会起作用呢
      
           
      

  11.   

    欢迎参加JAVA2005群:6276733
    要求二年工作经验!
      

  12.   

    synchronized 应该是只针对静态方法吧别的方法对于不同对象应该没什么意义