我发现不管从什么话题,都能扯到Thread上面,例如:
同步-》安全-》多线程,
Core Java-》IO-》多线程,
框架-》底层-》多线程,
这样的话题导致最后的结束点,往往集中在多线程,虽然实际开发中线程级的编程,普通程序员几乎碰不到,但是还是需要我们有所了解。
找了一个生产者/消费者模式的例子,线程的很多功能的问题,都可以由它来阐述。这个例子可以用List,Stack等多种方式来模拟,这里采用最简单一个共享int,只为理解生产和消费的原理。
Store仓库类,为生产者和消费者所共享。对增减上锁,同步执行:
public class Store {
private final int MAX_SIZE;
private int count;
public Store(int n) {
this.MAX_SIZE = n;
count = 0;
}
public synchronized void add() {
while (count >= MAX_SIZE) {
System.out.println("已经满了。");
try {
this.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
count++;
System.out.println(Thread.currentThread().toString() + " put " + count);
this.notifyAll();
}
public synchronized void remove() {
while (count <= 0) {
System.out.println("空了 ");
}
try {
this.wait();
} catch (Exception e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().toString() + " get " + count);
this.notify();
}
public static void main(String arg[]) {
Store store = new Store(5);
Thread pro = new Producer(store);
Thread con = new Consumer(store);
pro.setName("product");
con.setName("consumer");
Thread pro1 = new Producer(store);
Thread con1 = new Consumer(store);
pro1.setName("product1");
con1.setName("consumer1");
pro.start();
con.start();
pro1.start();
con1.start();
}
}
Producer生产者类。不停的add产品。每次生产后,等待1000ms,直至仓库装满。
public class Producer extends Thread {
private Store store;
public Producer(Store store) {
this.store = store;
}
public void run() {
while (true) {
store.add();
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Consumer消费者类。不停的remove产品,每次减少产品后,等待1000ms,直至仓库为空。
public class Consumer extends Thread {
private Store store;
public Consumer(Store store) {
this.store = store;
}
public void run() {
while (true) {
store.remove();
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
贴上运行的结果:
Thread[product,5,main] put 1
Thread[product1,5,main] put 2
Thread[consumer,5,main] get 1
Thread[product1,5,main] put 2
Thread[consumer1,5,main] get 1
Thread[product,5,main] put 2
Thread[consumer,5,main] get 1
Thread[product1,5,main] put 2
Thread[product,5,main] put 3
Thread[consumer,5,main] get 2
Thread[consumer1,5,main] get 1
Thread[product1,5,main] put 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product1,5,main] put 3
Thread[product,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product1,5,main] put 3
Thread[product,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product1,5,main] put 3
Thread[product1,5,main] put 4
Thread[product,5,main] put 5
Thread[consumer1,5,main] get 4
Thread[consumer,5,main] get 3
Thread[product1,5,main] put 4
Thread[product,5,main] put 5
Thread[consumer,5,main] get 4
Thread[consumer1,5,main] get 3
Thread[product,5,main] put 4
Thread[product1,5,main] put 5
Thread[consumer1,5,main] get 4
Thread[consumer,5,main] get 3
Thread[product,5,main] put 4
Thread[product1,5,main] put 5
Thread[consumer1,5,main] get 4
Thread[consumer,5,main] get 3
Thread[product1,5,main] put 4
Thread[product,5,main] put 5
已经满了。
已经满了。