马老的视频讲得相当的清楚,受益匪浅。但是在线程那一章讲生产者消费者问题的时候,那个窝头的代码有一个小错误,导致打印的内容不符合线程实时的内容。马老师当时是带着大家一起写的,所以难免出现小漏洞。如下是源代码:
public class ProducerConsumer {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(c).start();
}
}class WoTou {
int id; 
WoTou(int id) {
this.id = id;
}
public String toString() {
return "WoTou : " + id;
}
}class SyncStack {
int index = 0;
WoTou[] arrWT = new WoTou[6];

public synchronized void push(WoTou wt) {
while(index == arrWT.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrWT[index] = wt;
index ++;
}

public synchronized WoTou pop() {
while(index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return arrWT[index];
}
}class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}

public void run() {
for(int i=0; i<20; i++) {
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class Consumer implements Runnable {
SyncStack ss = null;
Consumer(SyncStack ss) {
this.ss = ss;
}

public void run() {
for(int i=0; i<20; i++) {
WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}如果大家跑过这段代码就知道,在打印出来的内容中,有些时候消费者并没有取栈顶的那个窝头。原因在于在执行完pop操作的时候,系统不能保证消费者该线程仍然能够锁住ss对象,导致另一个线程的操作可能插入进来。也就是说,在pop或者push操作结束后,该线程立刻放弃对ss对象的锁控制,此时很有可能另一个线程执行了起来,而接下来打印的内容也就不对了。正确的代码应该将打印的操作放到push或者pop内部,这样可以保证代码的正确性!最后再赞一下马老师,讲的很好。