临时写了个这个,没事的时候,可以看一看。java 写的,源码如下:
-------------------------------------------------------------------------------------------
package com.spring.test.th;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class ProduceConsumer {
public static void main(String[] args) {
MyShell shell = new MyShell(10);
Producer pd = new Producer(shell);
Consumer cs = new Consumer(shell);
System.out.println("=====================5-1- running!");
pd.start();
cs.start();
int testTime = 1000; // ms
try {
Thread.sleep(testTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("上帝醒了,准备终止竞赛!");
pd.stop();
cs.stop();
System.out.println("竞赛停止!");
}
}
class MyShell {
private static final String simpleFmt = "YYYY-MM-dd hh:mm:ss.SSS";
int leftNum = 0;
private int fMAX_NUM = 10;
private SimpleDateFormat dateFmt = new SimpleDateFormat(simpleFmt);
public MyShell(int maxLimit) {
this.fMAX_NUM = maxLimit;
}
void add(String idTag) { // idTag is default set as class name.
//System.out.println("add, idTag is " + idTag);
if ("com.spring.test.th.Producer".equals(idTag)) {
// Goes down
} else {
return;
}
leftNum++;
System.out.println("after Producer add(), left " + leftNum
+ ", time " + dateFmt.format(Calendar.getInstance().getTime()));
}
void use(String idTag) {
//System.out.println("use, idTag is " + idTag);
if ("com.spring.test.th.Consumer".equals(idTag)) {
// Goes down
} else {
return;
}
leftNum--;
System.out.println("after Consumer use(), left " + leftNum
+ ", time " + dateFmt.format(Calendar.getInstance().getTime()));
}
boolean isEmpty() {
if (leftNum < 0) {
throw new RuntimeException("Bad operation !!");
}
return leftNum == 0;
}
boolean isFull() {
if (leftNum >= fMAX_NUM) {
System.err.println("left num is over the max num = " + leftNum);
return true;
}
return false;
}
}
class Producer extends Thread {
private static final String simpleFmt = "YYYY-MM-dd hh:mm:ss.sssss";
private SimpleDateFormat dateFmt = new SimpleDateFormat(simpleFmt);
private MyShell cmShell;
public Producer(MyShell shell) {
cmShell = shell;
}
@Override
public void run() {
super.run();
System.out.println("[Producer] run() tid = " + this.getId()
+ ", date = " + dateFmt.format(Calendar.getInstance().getTime()));
System.out.println(this.getId() + ", cmShell is " + cmShell);
synchronized (cmShell) {
while(true) {
while (cmShell.isFull()) {
cmShell.notify(); // optional
System.out.println("Before produce, wait at :" + dateFmt.format(Calendar.getInstance().getTime()));
try {
cmShell.wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Currently time is " + dateFmt.format(Calendar.getInstance().getTime()));
}
cmShell.add(Producer.class.getName());
//cmShell.notify(); // note.
try {
cmShell.wait(80);
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Consumer extends Thread {
private static final String simpleFmt = "YYYY-MM-dd hh:mm:ss.sssss";
private SimpleDateFormat dateFmt = new SimpleDateFormat(simpleFmt);
private MyShell cmShell;
public Consumer(MyShell shell) {
cmShell = shell;
}
@Override
public void run() {
super.run();
System.out.println("[Consumer] run() tid = " + this.getId()
+ ", date = " + dateFmt.format(Calendar.getInstance().getTime()));
System.out.println(this.getId() + ", cmShell is " + cmShell);
synchronized (cmShell) {
while(true) {
while (cmShell.isEmpty()) {
cmShell.notify(); // optional
System.out.println("[Consumer] Before usage, wait at :" + dateFmt.format(Calendar.getInstance().getTime()));
try {
cmShell.wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("[Consumer] Currently time is " + dateFmt.format(Calendar.getInstance().getTime()));
}
cmShell.use(Consumer.class.getName());
//cmShell.notify(); // note.
try {
cmShell.wait(100);
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
// Code end