package base.day4;
class Student {
private String name;
private String sex;
private boolean isEmpty = true;
public void set(String name,String sex) {
synchronized(this) {
while(this.isEmpty == false) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name;
this.sex = sex;
this.isEmpty = false;
this.notifyAll();
}
}
public void get() {
synchronized(this) {
while(this.isEmpty == true) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("name:" + this.name + "sex:" + this.sex);
this.isEmpty = true;
this.notifyAll();
}
}
}
class Pro implements Runnable {
private Student s;
Pro(Student s) {
this.s = s;
}
@Override
public void run() {
for(int i=0; i<100; i++) {
if(i%2 == 0)
this.s.set("hnlm", "male");
else
this.s.set("章泽天", "女");
}
}
}
class Con implements Runnable {
private Student s;
Cons(Student s) {
this.s = s;
}
@Override
public void run() {
for(int i=0; i<100; i++) {
this.s.get();
}
}
}
public class Demo11 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student();
new Thread(new Pro(s)).start();
new Thread(new Cons(s)).start();
}
}
你能找到哪个是保留字么?:P |