该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package TwentyOne;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class A implements Runnable{
public synchronized void a(){
try {
wait();
System.out.println("这是第一个Runnable");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while(!Thread.interrupted())
a();
}
}
class B implements Runnable{
public synchronized void b(){
try {
TimeUnit.SECONDS.sleep(1);
notifyAll();
//System.out.println("这是第二个Runnable");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while(!Thread.interrupted())
b();
}
}
public class TwentyOne {
public static void main(String[] args) throws InterruptedException
{
ExecutorService exec=Executors.newCachedThreadPool();
exec.execute(new A());
exec.execute(new B());
TimeUnit.SECONDS.sleep(5);
exec.shutdown();
}
}