package wintervacation.multithreading;
/**
* Created by wangw on 2016/3/2.
* 消费者和生产者的例子,用于说明线程之间的通信
* 把生产者和消费者作为两个线程
* 仓库作为一个类,有装入生产者生产的商品和向消费者提供商品两个方法
*/
public class ConsumerAndProductor {
public static void main(String[] args) {
Repo repo = new Repo();
ComsumerThread comsumerThread = new ComsumerThread(repo);
comsumerThread.start();
ProductorThread productorThread = new ProductorThread(repo);
productorThread.start();
}
}
class Repo {
//仓库可以容纳6件商品
char[] data = new char[6];
int index = 0;
public synchronized void in(char c) {
if (index == 6) {
try {
this.wait();
System.out.println("-----" + this);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
data[index++] = c;
System.out.println("生产了产品" + c);
this.notify();
}
public synchronized char out() {
if (index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
index--;
System.out.println("消费了产品" + data[index]);
this.notify();
return data[index];
}
}
class ComsumerThread extends Thread {
Repo repo;
ComsumerThread(Repo repo) {
this.repo = repo;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
char c = (char) (Math.random() * 26 + 'A');
repo.in(c);
try {
Thread.sleep((int) (Math.random() * 10));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ProductorThread extends Thread {
Repo repo;
ProductorThread(Repo repo) {
this.repo = repo;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
repo.out();
try {
Thread.sleep((int) (Math.random() * 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果:
既然是有wait方法,那就应该是对应的线程对象吧,可是打印出来又是自己创建的Repo对象,懵了
this是指当前对象本身,你给出的这个in方法是你的Repo类的,你通过这个对象调用in方法,肯定这个方法中的this对象就是这个仓库对象啊。所有类的方法中出现的this都是这个方法调用时.操作前面的对象。这就是为什么我们调用类的方法是必须先创建一个对象,然后通过对象.方法来调用的原因。
你在线程里调用了in方法,对应的对象是repo
测试例子:
public static void main(String args[]) throws Exception {
Test test = new Test();
System.out.println("test:"+test);
test.testThis();
}
private void testThis() {
System.out.println("this"+this);
new Thread( new Runnable() {
public void run() {
System.out.println(this);
}
}).start();
new Thread( new Runnable() {
public void run() {
System.out.println(this);
}
}).start();
new Thread( new Runnable() {
public void run() {
System.out.println(this);
}
}).start();
}
测试结果:
test:com.sjd.test.Test@76a40575
thiscom.sjd.test.Test@76a40575
com.sjd.test.Test$1@744957c7
com.sjd.test.Test$2@1043fb68
com.sjd.test.Test$3@32edeea8
想知道为什么多线程中的this打印出来和上面两个this不是同一个对象啊?