这样吧,我是根据前面同学的思路以生产者-消费者方式完成的一个java小demo。
第一个类:
共享值类。
public class ShareValue {
private int count;
public ShareValue(int count) {
this.count = count;
}
public synchronized void plus() throws InterruptedException {
if (count <= 100) {
++count;
System.out.println("Producer:" + count);
this.notifyAll();
} else {
System.out.println("值超过100,停止增加!");
this.wait();
}
}
public synchronized void minus() throws InterruptedException {
if (count > 0) {
--count;
System.err.println("Consumer:" + count);
this.notifyAll();
} else {
System.out.println("值小或等于0,停止减少!");
this.wait();
}
}
}
第二个类:
生产者类
public class Producer implements Runnable {
private ShareValue value;
public Producer(ShareValue value) {
this.value = value;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
try {
value.plus();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
第三个类:
消费者类
public class Consumer implements Runnable {
private ShareValue value;
public Consumer(ShareValue value) {
this.value = value;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
try {
value.minus();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
第四个:
测试类
public class TestPC {
public static void main(String[] args) {
ShareValue value = new ShareValue(0);
Runnable pr = new Producer(value);
Runnable cr = new Consumer(value);
new Thread(pr).start();
new Thread(cr).start();
}
}
希望对你有所帮助,这只是能初步跑起来,可以观察运行,后面可以逐步调整。
典型的生产者-消费者问题,要深入理解此问题,不妨参考操作系统教材或在网上找,这是多线程的要点知识。
不知道出题人要求用伪代码、C语言还是什么来做?
如果增加或减少的单位为1,不妨用信号量实现。信号量的down、up操作分别对信号量实施加1和减1的操作,如果down发现信号量已经为0,那么线程被阻塞;如果up发现信号量为0,那么对其加1之后还要检查其他进程的状态,如果有其他线程因为对此信号量执行down而被阻塞,那么使其变为就绪态。我们不妨设置两个信号量:full和empty,full即为value的值,empty为100-value。可见线程A增加full,减少empty,如果empty等于0就会被阻塞;线程B增加empty,减少full,如果full等于0就会被阻塞。此外,由于对value是共享资源,一个线程对value进行操作时不能被另一线程打断,故需要使用一个二元信号量对临界区进行保护,再设置一个mutex信号量。如果每个线程在进入临界区前都执行一个down操作,并在刚刚退出时执行一个up操作,就能实现互斥。
我用C语言伪代码写一下:
int mutex = 1;
int full = value;
int empty = 100 - value;
void ThreadA()
{
while(true)
{
down(&empty);
down(&mutex);
value++;
up(&mutex);
up(&full);
}
}
void ThreadB()
{
while(true)
{
down(&full);
down(&mutex);
value++;
up(&mutex);
up(&empty);
}
}
这是脱离于具体实现环境之外的伪代码,实际上,对于具体的操作系统、编程语言和IDE,相同的机制有不同的实现形式,比如Linux和Windows有各自的线程同步对象和同步API,但其核心思想是相同的。
在WinForm下的C#代码:
int value = 0; // 公共变量
bool BADD = true;
void thdAddFunc()
{
for (int i = 0; i < 200; i++)
{
lock (this)
{
Thread.Sleep(1000);
if (!BADD || value > 100)
{
this.richTextBox1.Text += "value大于100,挂起" + "\r\n";
Monitor.Wait(this);
}
value++;
BADD = false;
Monitor.Pulse(this);
this.richTextBox1.Text += "Add1:" + value.ToString() + "\r\n";
}
}
}
void thdSubFunc()
{
for (int i = 0; i < 200; i++)
{
lock (this)
{
Thread.Sleep(1000);
if (BADD || value < 0)
{
this.richTextBox1.Text += "value小于0,挂起" + "\r\n";
Monitor.Wait(this);
}
value--;
BADD = true;
Monitor.Pulse(this);
this.richTextBox1.Text += "sub2:" + value.ToString() + "\r\n";
}
}
}
public Form1()
{
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
Thread thd1 = new Thread(new ThreadStart(thdAddFunc));
Thread thd2 = new Thread(new ThreadStart(thdSubFunc));
thd1.Start();
thd2.Start();
}