测试类
public class Test {
public static void main(String[] args) {
Movie m = new Movie();
Watcher w = new Watcher(m);
Player p = new Player(m);
Thread t1 = new Thread(w);
Thread t2 = new Thread(p);
t1.start();
t2.start();
}
}
//导演类
public class Player implements Runnable {
private Movie m;
public Player(Movie m){
this.m = m;
}
@Override
public void run() {
for(int i =0;i<=20;i++){
if(i%2==0){
m.play("蜘蛛侠");
}else{
m.play("钢铁侠");
}
}
}
}
//观众类
public class Watcher implements Runnable {
private Movie m;
public Watcher(Movie m){
this.m = m;
}
@Override
public void run() {
for(int i=0;i<=20;i++){
m.watch();
}
}
}