您的位置:首页 > Web前端 > JavaScript

Napajs demo-多个生产者/消费者

2017-11-02 10:45 288 查看

生产者/消费者问题

多生产者和多消费者

多个生产者向一个缓冲区中存入数据,多个生产者从缓冲区中取数据。这是有界缓冲区问题,队列改写,生产者之间、消费者之间、生产者消费者之间互相互斥。 共享缓冲区作为一个环绕缓冲区,存数据到尾时再从头开始。

我们使用一个互斥量保护生产者向缓冲区中存入数据。由于有多个生产者,因此需要记住现在向缓冲区中存入的位置。

使用一个互斥量保护缓冲区中消息的数目,这个生产的数据数目作为生产者和消费者沟通的桥梁。 使

用一个条件变量用于唤醒消费者。由于有多个消费者,同样消费者也需要记住每次取的位置。

在选项中选择生产条目的数目,生产者的线程数目,消费者的线程数目。生产者将条目数目循环放入缓冲区中,消费者从缓冲区中循环取出并在屏幕上打印出来。

以上内容来自简书

作者:穹蓝奥义

链接:http://www.jianshu.com/p/b16296e9ac85

來源:简书

JavaScript实现多生产者和多消费者问题

使用napajs的来实现多生产者和多消费者问题

先使用npm安装napajs
npm install napajs

代码:

let napa = require('napajs');

/**
* producer 和 consumer 两个zone(也就是两个进程)
* 生产者和消费者各创建两个worker(相当于两个线程)
*/
let producer = napa.zone.create('producer', { workers: 2 });
let consumer = napa.zone.create('consumer', { workers: 2 });

let store = napa.store.create('store1');
store.set('mutex', true);
store.set('empty', 2);
store.set('full', 0);
store.set('list', '');

function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}

/**
* 生产
* @param index 生产者编号
*/
function produce(index) {
while (1) {
while (store.get('empty') === 0) {
sleep(1000);
}
store.set('empty', store.get('empty') - 1);
while (!store.get('mutex')) {
sleep(1000);
}
store.set('mutex', false);
console.log('producer ' + index + ' produce a product');
let list = '';
list = store.get('list') + '1';
store.set('list', list);
console.log('add to buffer');
store.set('mutex', true);
store.set('full', store.get('full') + 1);
}
}

/**
* 消费
* @param index 消费者编号
*/
function consume(index) {
while (1) {
while (store.get('full') === 0) {
sleep(1000);
}
store.set('full', store.get('full') - 1);
while (!store.get('mutex')) {
sleep(1000);
}
store.set('mutex', false);
let list = store.get('list');
list = list.substr(1);
store.set('list', list);
console.log('consumer ' + index + ' get a product from buffer');
store.set('mutex', true);
store.set('empty', store.get('empty') + 1);
console.log('consume a product');
}

}

let mainProcess = () => {
let code = 'let napa = require("napajs");\n' +
'  let store = napa.store.get(\'store1\');';

//广播napa和store的声明代码
producer.broadcast(code)
.then(() => {
console.log('producer broadcast success');
}).catch(error => {
console.log(error);
});
consumer.broadcast(code)
.then(() => {
console.log('consumer broadcast success');
}).catch(error => {
console.log(error);
});

//广播sleep、produce、consume等函数声明到napa workers
producer.broadcast(sleep.toString());
consumer.broadcast(sleep.toString());
producer.broadcast(produce.toString());
consumer.broadcast(consume.toString());

//执行两个produce
producer.execute('', 'produce',[1])
.then((result) => {
console.log('producer execute,' + result.value);
})
.catch((error) => {
console.log(error);
});
producer.execute('', 'produce',[2])
.then((result) => {
console.log('producer execute,' + result.value);
})
.catch((error) => {
console.log(error);
});
//执行两个consume
consumer.execute('', 'consume',[1])
.then((result) => {
console.log('consumer execute,' + result.value);
})
.catch((error) => {
console.log(error);
});
consumer.execute('', 'consume',[2])
.then((result) => {
console.log('consumer execute,' + result.value);
})
.catch((error) => {
console.log(error);
});
};
mainProcess();

输出

"F:\WebStorm\WebStorm 2017.2.2\bin\runnerw.exe" F:\nodejs\node.exe F:\WebStorm\practices\jsDoc-demo\producerConsumer.js
producer 2 produce a product
add to buffer
producer 2 produce a product
add to buffer
producer 1 produce a product
add to buffer
producer 2 produce a product
add to buffer
consumer 1 get a product from buffer
producer 2 produce a product
consume a product
add to buffer
consumer 2 get a product from buffer
consume a product
consumer 1 get a product from buffer
consume a product
producer 2 produce a product
add to buffer
consumer 2 get a product from buffer
producer 1 produce a product
consume a product
add to buffer
producer 2 produce a product
add to buffer
consumer 1 get a product from buffer
consumer 2 get a product from buffer
consume a product
consume a product
producer 2 produce a product
consumer 1 get a product from buffer
add to buffer
...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: