gpt4 book ai didi

java - 消费品生产商

转载 作者:行者123 更新时间:2023-12-03 13:23:21 25 4
gpt4 key购买 nike

ProdCom.java(驱动程序类)

import static java.lang.System.out;


public class ProdCom{
static int full = 50;
static int mutx = 0;
static int empty = 0;
static int currentSize = 0;

public static void acquire(){
while (mutx == 1);

mutx++;

}

public static void release(){
mutx--;
}


public static void main(String args[]){
Thread t = new Thread(new Producerr());
Thread t1 = new Thread(new Consumerr());

t.start();
t1.start();
}


}
Producerr.java
class Producerr implements Runnable{


public void wwait(){
while (ProdCom.currentSize >= ProdCom.full){

}

} public void signal(){
ProdCom.currentSize++;
}

public void run(){
do{
this.wwait();

ProdCom.acquire();

out.println("Num elements" + ProdCom.currentSize);
out.println("producing!");

ProdCom.release();
this.signal();
} while (true);
}
}
Consumerr.java
class Consumerr implements Runnable{
public void wwait(){
while (ProdCom.currentSize <= 0){
out.println("inside consumer wait: ");
out.println("number of elements: " + ProdCom.currentSize);
}

} public void signal(){
ProdCom.currentSize--;

}


public void run(){
do{

this.wwait();
ProdCom.acquire();

out.println("Num elements" + ProdCom.currentSize);
out.println("Consuming!");

ProdCom.release();
this.signal();
} while (true);
}
}

以上是我对消费者生产者问题的解决方案。驱动程序类 ProdCom具有变量 fullemptymutx,用于控制生产者 t和消费者 t1对变量 currentSize的访问(从而模拟缓冲区中的当前项目数)。但是当我运行代码时,输​​出似乎表明t1和t并没有轮流更改 currentSize,而是其中之一永远重复并卡住了……我想知道为什么吗?谢谢。

最佳答案

我对您的代码进行了一些改进,您会注意到Joni提到的许多概念都已考虑在内。
ProdCom.java

import java.lang.*;

public class ProdCom{
static final int FULL = 50;
static final int EMPTY = 0;

static volatile int mutx = 0;
static volatile int currentSize = 0;
static Object lockObject = new Object();

public static void acquire(){
/* since mutx is defined volatile, the spinlock works,
but you reconsider this approach. There are cheaper
methods of heating the room */
while (mutx == 1);

mutx++;

}

public static boolean isEmpty() {
synchronized(lockObject) {
if (currentSize <= EMPTY) return true;
return false;
}
}

public static boolean isFull() {
synchronized(lockObject) {
if (currentSize >= FULL) return true;
return false;
}
}

public static int getCurrentSize() {
synchronized(lockObject) {
return currentSize;
}
}

public static void release(){
mutx--;
}

public static void incCurrentSize()
{
synchronized(lockObject) {
currentSize++;
}
}

public static void decCurrentSize()
{
synchronized(lockObject) {
currentSize--;
}
}


public static void main(String args[]){
Thread t = new Thread(new Producerr());
Thread t1 = new Thread(new Consumerr());

t.start();
t1.start();
}


}
Consumerr.java
import java.lang.*;

class Consumerr implements Runnable {

public void wwait() {
while (ProdCom.isEmpty()){
System.out.println("inside consumer wait: ");
System.out.println("number of elements: " + ProdCom.getCurrentSize());
try {
/* we don't spinlock here */
Thread.sleep(50);
} catch (Exception e) {
/* do nothing */
}
}

}

public void signal(){
ProdCom.decCurrentSize();

}


public void run(){
do{
this.wwait();
ProdCom.acquire();

System.out.println("Num elements " + ProdCom.getCurrentSize());
System.out.println("Consuming!");
this.signal();

ProdCom.release();
} while (true);
}
}
Producerr.java
import java.lang.*;

class Producerr implements Runnable {

public void wwait(){
while (ProdCom.isFull()){
try {
Thread.sleep(50);
} catch(Exception e) { /* do nothing */ }
}

}
public void signal(){
ProdCom.incCurrentSize();
}

public void run(){
do {
this.wwait();

ProdCom.acquire();

System.out.println("Num elements : " + ProdCom.getCurrentSize());
System.out.println("producing!");
this.signal();

ProdCom.release();
} while (true);
}
}

关于java - 消费品生产商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63448699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com