簡體   English   中英

關於Java並發程序的一個奇怪的錯誤(使用信號量)

[英]A strange bug about a Java concurrency program (using semaphore)

我遇到了一個問題,即當生產者填滿arraylist緩沖區中的所有位置時,程序每次都會停止執行。 但是,從理論上講。 使用者進程仍然可以進入get()函數,導致notEmpty信號量現在的信號值達到10。 因此,在消費方進程將信號值釋放回信號量notFull之后,生產方進程仍可以工作。

但這只是停止了,我找不到問題所在。

這是程序:

import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Semaphore;
/**
 * A bounded buffer maintains a fixed number of "slots". Items can be
 * inserted into and removed from the buffer. The buffer has a maximum
 * size.
 */

class BoundedBuffer2
{
  // the maximum size of the bounded buffer
  final public static int MAXSIZE = 10;
  Semaphore notEmpty = new Semaphore(0);
  Semaphore notFull = new Semaphore(MAXSIZE);
  Semaphore sema = new Semaphore(1);


  // the buffer
  volatile List<Integer> buffer;

  public BoundedBuffer2()
  {
    buffer = new ArrayList<Integer>();
  }

  // add an element to the end of the buffer if it is not full
  public synchronized void put(int input)
    throws InterruptedException
  {
      notFull.acquire();
      sema.acquire();
      buffer.add(input);
      sema.release();
      notEmpty.release();
  }

  // take an element from the front of the buffer
  public synchronized int get()
    throws InterruptedException
  {
      notEmpty.acquire();
      sema.acquire();
      int result = buffer.remove(0);
      sema.release();
      notFull.release();
      return result;
  }

  public int size()
  {
    int result = buffer.size();
    return result;
  }
}

/**
 * An instance of the Producer class produces new integers at random
 * intervals, and inserts them into a bounded buffer.
 */

class Producer2 extends Thread
{
  // the buffer in which to insert new integers
  BoundedBuffer2 buffer;

  public Producer2(BoundedBuffer2 buffer)
  {
    this.buffer = buffer;
  }

  public void run()
  {
    Random random = new Random();

    try {
      while (true) {
    Thread.sleep(100);
    //insert a random integer

    int next = random.nextInt();
    buffer.put(next);



    System.err.println("b.size() increases to " + buffer.size());
      }
    }
    catch (InterruptedException e) {}
  }
}

/**
 * An instance of the Consumer class consumes integers from a bounded
 * buffer at random intervals.
 */

class Consumer2 extends Thread
{
  // the buffer in which to insert new integers
  BoundedBuffer2 buffer;

  public Consumer2(BoundedBuffer2 buffer)
  {
    this.buffer = buffer;
  }

  public void run()
  {
    Random random = new Random();

    try {
      while (true) {

    Thread.sleep(200);
          //get the next integer from the buffer
    int next = buffer.get();

    System.err.println("next = " + next);
    System.err.println("b.size() reducted to " + buffer.size());


      }
    }
    catch (InterruptedException e) {}
  }
}

public class UseBuffer2
{
  public static void main(String [] args)
  {
    BoundedBuffer2 buffer = new BoundedBuffer2();
    Producer2 p = new Producer2(buffer);
    Consumer2 c = new Consumer2(buffer);

    p.start();
    c.start();
  }
}

這是控制台的輸出:

b.size() increases to 1
b.size() increases to 2
next = 400524264
b.size() reducted to 1
b.size() increases to 2
b.size() increases to 3
next = 241523118
b.size() reducted to 2
b.size() increases to 3
next = -1618289090
b.size() reducted to 2
b.size() increases to 3
b.size() increases to 4
next = -316455080
b.size() reducted to 3
b.size() increases to 4
b.size() increases to 5
next = 338682909
b.size() reducted to 4
b.size() increases to 5
b.size() increases to 6
next = -961276708
b.size() reducted to 5
b.size() increases to 6
b.size() increases to 7
next = 2056804692
b.size() reducted to 6
b.size() increases to 7
b.size() increases to 8
next = -301063524
b.size() reducted to 7
b.size() increases to 8
b.size() increases to 9
next = -148582342
b.size() reducted to 8
b.size() increases to 9
b.size() increases to 10
next = -2076430410
b.size() reducted to 9
b.size() increases to 10

您的put()get()方法是同步的。 因此,如果生產者輸入put()方法,嘗試獲取notFull信號量,但由於緩沖區已滿而無法獲取,它將永遠阻塞,並保持進入同步方法時獲取的鎖。 因此,使用者不能輸入get()方法,也不能從緩沖區中刪除元素。

了解同步關鍵字的工作原理: https : //docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

相關部分:

同一對象上的兩個同步方法調用不可能交錯。 當一個線程正在執行對象的同步方法時,所有其他調用同一對象塊的同步方法的線程(掛起執行),直到第一個線程對該對象完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM