1个回答
展开全部
import java.util.concurrent.Semaphore;
class Q { //仓库
int n; //仓库中的产品
// Start with consumer semaphore unavailable.
static Semaphore semCon = new Semaphore(0);
static Semaphore semProd = new Semaphore(1);
void get() {
try {
semCon.acquire();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
semProd.release();
}
void put(int n) {
try {
semProd.acquire();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
System.out.println("Put: " + n);
semCon.release();
}
}
class Producer implements Runnable { //生产者
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
for(int i=0; i < 20; i++) q.put(i);
}
}
class Consumer implements Runnable { //消费者
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
for(int i=0; i < 20; i++) q.get();
}
}
class ProdCon { //测试类
public static void main(String args[]) {
Q q = new Q();
new Consumer(q);
new Producer(q);
}
}
}
class Q { //仓库
int n; //仓库中的产品
// Start with consumer semaphore unavailable.
static Semaphore semCon = new Semaphore(0);
static Semaphore semProd = new Semaphore(1);
void get() {
try {
semCon.acquire();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
semProd.release();
}
void put(int n) {
try {
semProd.acquire();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
System.out.println("Put: " + n);
semCon.release();
}
}
class Producer implements Runnable { //生产者
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
for(int i=0; i < 20; i++) q.put(i);
}
}
class Consumer implements Runnable { //消费者
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
for(int i=0; i < 20; i++) q.get();
}
}
class ProdCon { //测试类
public static void main(String args[]) {
Q q = new Q();
new Consumer(q);
new Producer(q);
}
}
}
中研普华
2023-10-12 广告
2023-10-12 广告
消费者需求调查的主要方式包括以下几种:1. 问卷调查法:通过制定详细、全面的问卷,收集消费者的意见、需求等信息。2. 访谈法:通过与消费者进行面对面的交流,了解他们的需求和期望。3. 观察法:观察消费者的行为,从中发现他们的需求和偏好。4....
点击进入详情页
本回答由中研普华提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询