为什么我的程序有时会卡住?
why does my program get stuck sometimes?
所以我在 java 中使用监视器编写了一个有界缓冲区问题,但我无法弄清楚我的程序有什么问题。有时它会在第三个循环结束之前将 运行 保持在无限循环中。大多数时候它运行完美。
该程序很简单,关于一个生产者和多个消费者。我将不胜感激任何帮助。这是我的 github 的 link,您可以在其中找到完整的代码。 the full code
有界缓冲区
public class BoundedBuffer {
public BoundedBuffer ()
{
int numWorms = 10;
int numBirds = 5;
Dish dish = new Dish (numWorms);
Parent parent = new Parent(dish);
parent.start();
Child child[] = new Child[numBirds];
for (int i = 0; i < numBirds; i++)
{
child[i] = new Child (dish);
child[i].start();
}
for (int i = 0; i < numBirds; i++)
{
try {child[i].join();}
catch (Exception ie) {System.out.println (ie.getMessage());}
}
System.out.println("bids done eating :D");
}
}
菜肴
public class Dish
{
int worms;
int copy;
public Dish (int worms)
{
this.worms = worms;
copy = worms;
}
public synchronized void eat ()
{
if (worms <= 0)
{
waitForFull();
}
worms --;
System.out.println("Bird " + Thread.currentThread().getName() + " has
eaten."
+ " The number of worms left is " + worms);
}
public synchronized void fill()
{
if (worms > 0)
{
waitForEmpty();
}
worms = copy;
System.out.println ("Parent filled the dish");
notifyAll();
}
public synchronized void waitForEmpty ()
{
while (worms > 0)
{
notifyAll();
try {wait();}
catch (Exception ie) {System.out.println (ie.getMessage());}
}
}
public synchronized void waitForFull ()
{
while (worms <= 0)
{
notifyAll();
try {wait();}
catch (Exception ie) {System.out.println (ie.getMessage());}
}
}
}
我无法重现 stuck
的情况。但是我在你的代码中发现了一些其他问题:
Parent
循环三遍后立即设置done = 1
,此时
时间,Dish
. 中还有Worms
waitForEmpty
和 fill
不是自动的,这可能会导致
有些不一致。
waitForFull
和 eat
不是自动的,这可能会导致
有些不一致。
两个解决这些问题,我觉得你需要:
- 将
waitForEmpty
和 fill
以及 waitForFull
和 eat
. 合并为一个方法
- 使用
shutdown
和isTerminate
作为信号。 Parent
将 shutdown
Dish
,而最后一个 Child
将 terminate
Dish
。
代码如下:
主要
public class Main {
public static void main(String[] args) {
new BoundedBuffer ();
}
}
有界缓冲区
public class BoundedBuffer {
public BoundedBuffer () {
int numWorms = 10;
int numBirds = 5;
Dish dish = new Dish (numWorms);
Parent parent = new Parent(dish);
parent.start();
Child child[] = new Child[numBirds];
for (int i = 0; i < numBirds; i++) {
child[i] = new Child (dish);
child[i].start();
}
for (int i = 0; i < numBirds; i++) {
try {child[i].join();}
catch (Exception ie) {System.out.println (ie.getMessage());}
}
System.out.println("bids done eating :D");
}
}
菜肴
public class Dish {
int worms;
int copy;
boolean shutDown;
boolean isTerminated;
public Dish (int worms) {
this.worms = worms;
copy = worms;
}
public synchronized void waitForEmptyToFill() {
while (worms > 0) {
try {
notifyAll();
wait();
} catch (Exception ie) {
System.out.println (ie.getMessage());
}
}
worms = copy;
System.out.println ("Parent filled the dish");
notifyAll();
}
public synchronized void waitForFullToEat () {
while (worms <= 0 && !isTerminated()) {
try {
notifyAll();
wait();
} catch (Exception ie) {
System.out.println (ie.getMessage());
}
}
if (worms > 0) {
worms--;
System.out.println("Bird " + Thread.currentThread().getName() + " has eaten."
+ " The number of worms left is " + worms);
if (worms == 0 && isShutDown()) {
setTerminated(true);
notifyAll();
}
}
}
public synchronized boolean isShutDown() {
return shutDown;
}
public synchronized void setShutDown(boolean shutDown) {
this.shutDown = shutDown;
}
public synchronized boolean isTerminated() {
return isTerminated;
}
public synchronized void setTerminated(boolean terminated) {
isTerminated = terminated;
}
}
Parent
public class Parent extends Thread {
private Dish dish;
public Parent (Dish dish) {
this.dish = dish;
}
public void run() {
for (int i = 0; i < 3; i++) {
dish.waitForEmptyToFill();
}
dish.setShutDown(true);
}
}
Child
public class Child extends Thread {
private Dish dish;
public Child (Dish dish)
{
this.dish = dish;
}
public void run () {
while (!dish.isTerminated()) {
dish.waitForFullToEat();
try {
sleep(100);
} catch (Exception ie) {
System.out.println (ie.getMessage());
}
}
}
}
输出:
Bird Thread-4 has eaten. The number of worms left is 9
Bird Thread-3 has eaten. The number of worms left is 8
Bird Thread-2 has eaten. The number of worms left is 7
Bird Thread-5 has eaten. The number of worms left is 6
Bird Thread-1 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-4 has eaten. The number of worms left is 2
Bird Thread-1 has eaten. The number of worms left is 1
Bird Thread-5 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-4 has eaten. The number of worms left is 9
Bird Thread-5 has eaten. The number of worms left is 8
Bird Thread-3 has eaten. The number of worms left is 7
Bird Thread-1 has eaten. The number of worms left is 6
Bird Thread-2 has eaten. The number of worms left is 5
Bird Thread-4 has eaten. The number of worms left is 4
Bird Thread-5 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-2 has eaten. The number of worms left is 1
Bird Thread-3 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-1 has eaten. The number of worms left is 9
Bird Thread-5 has eaten. The number of worms left is 8
Bird Thread-3 has eaten. The number of worms left is 7
Bird Thread-2 has eaten. The number of worms left is 6
Bird Thread-4 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-5 has eaten. The number of worms left is 1
Bird Thread-4 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-2 has eaten. The number of worms left is 9
Bird Thread-3 has eaten. The number of worms left is 8
Bird Thread-1 has eaten. The number of worms left is 7
Bird Thread-5 has eaten. The number of worms left is 6
Bird Thread-4 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-5 has eaten. The number of worms left is 1
Bird Thread-4 has eaten. The number of worms left is 0
bids done eating :D
所以我在 java 中使用监视器编写了一个有界缓冲区问题,但我无法弄清楚我的程序有什么问题。有时它会在第三个循环结束之前将 运行 保持在无限循环中。大多数时候它运行完美。 该程序很简单,关于一个生产者和多个消费者。我将不胜感激任何帮助。这是我的 github 的 link,您可以在其中找到完整的代码。 the full code
有界缓冲区
public class BoundedBuffer {
public BoundedBuffer ()
{
int numWorms = 10;
int numBirds = 5;
Dish dish = new Dish (numWorms);
Parent parent = new Parent(dish);
parent.start();
Child child[] = new Child[numBirds];
for (int i = 0; i < numBirds; i++)
{
child[i] = new Child (dish);
child[i].start();
}
for (int i = 0; i < numBirds; i++)
{
try {child[i].join();}
catch (Exception ie) {System.out.println (ie.getMessage());}
}
System.out.println("bids done eating :D");
}
}
菜肴
public class Dish
{
int worms;
int copy;
public Dish (int worms)
{
this.worms = worms;
copy = worms;
}
public synchronized void eat ()
{
if (worms <= 0)
{
waitForFull();
}
worms --;
System.out.println("Bird " + Thread.currentThread().getName() + " has
eaten."
+ " The number of worms left is " + worms);
}
public synchronized void fill()
{
if (worms > 0)
{
waitForEmpty();
}
worms = copy;
System.out.println ("Parent filled the dish");
notifyAll();
}
public synchronized void waitForEmpty ()
{
while (worms > 0)
{
notifyAll();
try {wait();}
catch (Exception ie) {System.out.println (ie.getMessage());}
}
}
public synchronized void waitForFull ()
{
while (worms <= 0)
{
notifyAll();
try {wait();}
catch (Exception ie) {System.out.println (ie.getMessage());}
}
}
}
我无法重现 stuck
的情况。但是我在你的代码中发现了一些其他问题:
Parent
循环三遍后立即设置done = 1
,此时 时间,Dish
. 中还有waitForEmpty
和fill
不是自动的,这可能会导致 有些不一致。waitForFull
和eat
不是自动的,这可能会导致 有些不一致。
Worms
两个解决这些问题,我觉得你需要:
- 将
waitForEmpty
和fill
以及waitForFull
和eat
. 合并为一个方法
- 使用
shutdown
和isTerminate
作为信号。Parent
将shutdown
Dish
,而最后一个Child
将terminate
Dish
。
代码如下:
主要
public class Main {
public static void main(String[] args) {
new BoundedBuffer ();
}
}
有界缓冲区
public class BoundedBuffer {
public BoundedBuffer () {
int numWorms = 10;
int numBirds = 5;
Dish dish = new Dish (numWorms);
Parent parent = new Parent(dish);
parent.start();
Child child[] = new Child[numBirds];
for (int i = 0; i < numBirds; i++) {
child[i] = new Child (dish);
child[i].start();
}
for (int i = 0; i < numBirds; i++) {
try {child[i].join();}
catch (Exception ie) {System.out.println (ie.getMessage());}
}
System.out.println("bids done eating :D");
}
}
菜肴
public class Dish {
int worms;
int copy;
boolean shutDown;
boolean isTerminated;
public Dish (int worms) {
this.worms = worms;
copy = worms;
}
public synchronized void waitForEmptyToFill() {
while (worms > 0) {
try {
notifyAll();
wait();
} catch (Exception ie) {
System.out.println (ie.getMessage());
}
}
worms = copy;
System.out.println ("Parent filled the dish");
notifyAll();
}
public synchronized void waitForFullToEat () {
while (worms <= 0 && !isTerminated()) {
try {
notifyAll();
wait();
} catch (Exception ie) {
System.out.println (ie.getMessage());
}
}
if (worms > 0) {
worms--;
System.out.println("Bird " + Thread.currentThread().getName() + " has eaten."
+ " The number of worms left is " + worms);
if (worms == 0 && isShutDown()) {
setTerminated(true);
notifyAll();
}
}
}
public synchronized boolean isShutDown() {
return shutDown;
}
public synchronized void setShutDown(boolean shutDown) {
this.shutDown = shutDown;
}
public synchronized boolean isTerminated() {
return isTerminated;
}
public synchronized void setTerminated(boolean terminated) {
isTerminated = terminated;
}
}
Parent
public class Parent extends Thread {
private Dish dish;
public Parent (Dish dish) {
this.dish = dish;
}
public void run() {
for (int i = 0; i < 3; i++) {
dish.waitForEmptyToFill();
}
dish.setShutDown(true);
}
}
Child
public class Child extends Thread {
private Dish dish;
public Child (Dish dish)
{
this.dish = dish;
}
public void run () {
while (!dish.isTerminated()) {
dish.waitForFullToEat();
try {
sleep(100);
} catch (Exception ie) {
System.out.println (ie.getMessage());
}
}
}
}
输出:
Bird Thread-4 has eaten. The number of worms left is 9
Bird Thread-3 has eaten. The number of worms left is 8
Bird Thread-2 has eaten. The number of worms left is 7
Bird Thread-5 has eaten. The number of worms left is 6
Bird Thread-1 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-4 has eaten. The number of worms left is 2
Bird Thread-1 has eaten. The number of worms left is 1
Bird Thread-5 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-4 has eaten. The number of worms left is 9
Bird Thread-5 has eaten. The number of worms left is 8
Bird Thread-3 has eaten. The number of worms left is 7
Bird Thread-1 has eaten. The number of worms left is 6
Bird Thread-2 has eaten. The number of worms left is 5
Bird Thread-4 has eaten. The number of worms left is 4
Bird Thread-5 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-2 has eaten. The number of worms left is 1
Bird Thread-3 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-1 has eaten. The number of worms left is 9
Bird Thread-5 has eaten. The number of worms left is 8
Bird Thread-3 has eaten. The number of worms left is 7
Bird Thread-2 has eaten. The number of worms left is 6
Bird Thread-4 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-5 has eaten. The number of worms left is 1
Bird Thread-4 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-2 has eaten. The number of worms left is 9
Bird Thread-3 has eaten. The number of worms left is 8
Bird Thread-1 has eaten. The number of worms left is 7
Bird Thread-5 has eaten. The number of worms left is 6
Bird Thread-4 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-5 has eaten. The number of worms left is 1
Bird Thread-4 has eaten. The number of worms left is 0
bids done eating :D