6-1 jmu-Java-07多线程-交替执行 (6分)
import java.util.*;
class Repo {
static ArrayList a = new ArrayList();
static boolean k=false;
public Repo(String items) {
String e[] = items.split("\s+");
for (int i = 0; i < e.length; i++) {
a.add(e[i]);
}
}
public Repo() {
}
public int getSize() {
return a.size();
}
public synchronized void getNum() {
if(k==false) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
System.out.println(Thread.currentThread().getName()+" finish "+a.get(0));
a.remove(0);
k=false;
this.notify();
}
}
public synchronized void getNum2() {
if(k==true) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
System.out.println(Thread.currentThread().getName()+" finish "+a.get(0));
a.remove(0);
k=true;
this.notify();
}
}
}
class Worker1 implements Runnable {
Repo repo = new Repo();
public Worker1(Repo repo) {
super();
this.repo = repo;
}
public void run() {
while(repo.a.size()>0) {
repo.getNum2();
}
}
}
class Worker2 implements Runnable {
Repo repo = new Repo();
public Worker2(Repo repo) {
su