习题:模拟一个人生产50个玩具,每200毫秒生产一个,当生产到第20个时加入每秒吃1个馒头,共吃完3个后接着生产如下错误代码:
public class Practise2 {
public static void main(String[] args) {
Person person=new Person();
ProductJoy productJoy=new ProductJoy("生产玩具线程",person);
productJoy.start();
EatBread eatBread=new EatBread("吃馒头的线程",person);
eatBread.start(); }
}
class Person{
int toy=0;
int bread=0; public int getToy() {
return toy;
} public void setToy(int toy) {
this.toy = toy;
} public int getBread() {
return bread;
} public void setBread(int bread) {
this.bread = bread;
}
}
class EatBread extends Thread{ Person person; public EatBread(String name,Person person){
super(name);
this.person=person;
}
@Override
public void run() {
while(true){ synchronized (person){ person.setBread(person.getBread()+1);
System.out.println(Thread.currentThread().getName()+"总共吃了馒头:"+person.getBread()+" 个");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(person.getBread()%3==0){
person.notify();
try {
person.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
}
}class ProductJoy extends Thread{
Person person; public ProductJoy(String name,Person person){
super(name);
this.person=person;
} @Override
public void run() {
while(true){ synchronized (person){
person.setToy(person.getToy()+1);
System.out.println(Thread.currentThread().getName()+"总共生产玩具:"+person.getToy()+" 个");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(person.getToy()>=50) return;
if(person.getToy()%20==0){
person.notify();
try {
person.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
问题:输出结果没有达到预期情况,明明生产的玩具没有达到20个,怎么吃馒头的线程就能对锁对象进行吃馒头操作了,这时候不应该等生产玩具的线程synchronized代码块执行完或者调用锁对象.wait方法释放锁对象后,吃馒头的线程获取到锁再进行操作吗??
public class Practise2 {
public static void main(String[] args) {
Person person=new Person();
ProductJoy productJoy=new ProductJoy("生产玩具线程",person);
productJoy.start();
EatBread eatBread=new EatBread("吃馒头的线程",person);
eatBread.start(); }
}
class Person{
int toy=0;
int bread=0; public int getToy() {
return toy;
} public void setToy(int toy) {
this.toy = toy;
} public int getBread() {
return bread;
} public void setBread(int bread) {
this.bread = bread;
}
}
class EatBread extends Thread{ Person person; public EatBread(String name,Person person){
super(name);
this.person=person;
}
@Override
public void run() {
while(true){ synchronized (person){ person.setBread(person.getBread()+1);
System.out.println(Thread.currentThread().getName()+"总共吃了馒头:"+person.getBread()+" 个");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(person.getBread()%3==0){
person.notify();
try {
person.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
}
}class ProductJoy extends Thread{
Person person; public ProductJoy(String name,Person person){
super(name);
this.person=person;
} @Override
public void run() {
while(true){ synchronized (person){
person.setToy(person.getToy()+1);
System.out.println(Thread.currentThread().getName()+"总共生产玩具:"+person.getToy()+" 个");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(person.getToy()>=50) return;
if(person.getToy()%20==0){
person.notify();
try {
person.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
问题:输出结果没有达到预期情况,明明生产的玩具没有达到20个,怎么吃馒头的线程就能对锁对象进行吃馒头操作了,这时候不应该等生产玩具的线程synchronized代码块执行完或者调用锁对象.wait方法释放锁对象后,吃馒头的线程获取到锁再进行操作吗??
你把synchronized放在while里面,
每一次循环你都要释放资源再重新获取资源,
在你释放的瞬间有可能就被另一个线程占用了。