/**
* Two threads, one thread output 1, one thread output 2,
* *
* @author ffr@cnic.cn
*/public
class SleepAndWaitThread2 { Public
static void Main (string[] args) {
Onethread one = new Onethread ();
Twothread two = new Twothread ();
One.start ();
Two.start ();
}
Class Onethread extends Thread {
@Override public
void Run () {
synchronized (sleepandwaitthread2.class) {while
(true) {
System.out.println ("1");
try {
SleepAndWaitThread2.class.wait ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
SleepAndWaitThread2.class.notify ()
;
}}} Class Twothread extends Thread {
@Override public
void Run () {
synchronized (sleepandwaitthread2.class) {while
(true) {
System.out.println ("2");
SleepAndWaitThread2.class.notify ();
try {
SleepAndWaitThread2.class.wait ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
}
}
}