java wait后怎么唤醒线程

class ss{
public static int i1=0;
public static int i2=0;
private Object task = new Object();

}

class number extends Thread
{
int m=500;
public static number x1;
// private static Object task = new Object();

public void run()
{
try
{
while(true)
{
sleep(m);
System.out.println(ss.i1);
ss.i1++;
synchronized(this){
if(ss.i1%7==0&&ss.i1%10==7)
{
this.wait();

}
}
}
}
catch(InterruptedException e)
{return;}
}
}

class letter extends Thread
{
int m=500;
String s=new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
public void run()
{
try
{
while(true)
{
sleep(m);
System.out.println(s.charAt(ss.i2));
ss.i2++;
if(ss.i2==26)
{
ss.i2=0;
}

}
}
catch(InterruptedException e)
{return;}
}
}

class hello extends Thread
{
int m=5000;
String s2="hello";
public void run()
{
try
{
while(true)
{
sleep(m);
System.out.println(s2);
}
}
catch(InterruptedException e)
{return;}
}
}
public class ex {
public static void main(String[] args)
{
number tr1 =new number();
letter tr2 = new letter();
hello tr3=new hello();
tr1.start();
tr2.start();
tr3.start();
}
}
number中有个wait()
现在要在letter中加入notify()后继续运行number,怎么改程序

notify()方法
无法指定。选择是任意性的。
唤醒在此对象监视器上等待的单个线程。如果有多个线程都在此对象上等待,则会选择唤醒其中一个线程。
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-01-02
class letter extends Thread
{
private Thread numberT;
int m=500;
String s=new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
public letter(Thread t){numberT=t;}
public void run()
{
try
{
while(true)
{
sleep(m);
System.out.println(s.charAt(ss.i2));
ss.i2++;
if(ss.i2==26)
{
ss.i2=0;
}
synchronized (t){
t.notify();}//可能细节写得有问题,反正就是加上同步的意思。

}
}
catch(InterruptedException e)
{return;}
}
}

下面的mian 中改为:
letter tr2 = new letter(tr1);本回答被网友采纳
第2个回答  2010-01-02
notify
第3个回答  2010-01-12
start()就启动了
第4个回答  2010-01-02
再用start()就启动了
相似回答