Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java锁条件似乎无法正常工作_Java_Multithreading_Conditional Statements_Locks - Fatal编程技术网

Java锁条件似乎无法正常工作

Java锁条件似乎无法正常工作,java,multithreading,conditional-statements,locks,Java,Multithreading,Conditional Statements,Locks,我遇到了一个问题,即存在BoundedBuffer,存在消费者和生产者,生产者填充缓冲区,消费者从缓冲区中移除 我正在为消费者和生产者使用线程,并且我试图使用锁定条件来确保缓冲区对生产者来说不是满的,对消费者来说也不是空的 不幸的是,它没有按照我想要的方式工作,似乎消费者/生产者,当他们处于状态时。等待,不要让其他线程工作。难道他们不应该让他们这样做吗 这是我的密码 class main { public static void main (String[] args) throws

我遇到了一个问题,即存在
BoundedBuffer
,存在
消费者
生产者
,生产者填充缓冲区,消费者从缓冲区中移除

我正在为消费者和生产者使用线程,并且我试图使用锁定条件来确保缓冲区对生产者来说不是满的,对消费者来说也不是空的

不幸的是,它没有按照我想要的方式工作,似乎消费者/生产者,当他们处于状态时。等待,不要让其他线程工作。难道他们不应该让他们这样做吗

这是我的密码


class main
{
    public static void main (String[] args) throws InterruptedException
    {
        final int N = Integer.parseInt(args[0]); 
        BoundedBuffer teste = new BoundedBuffer(N);
        Thread c = new Consumidor(teste,N);
        Thread p = new Produtor(teste,N);
        c.start();
        p.start();
        c.join();
        p.join();
    }
}

class BoundedBuffer
{
    ArrayList<Integer> array;
    int index;
    int size;

    Lock l = new ReentrantLock();
    Condition notFull = l.newCondition();
    Condition notEmpty = l.newCondition();

    BoundedBuffer(int N)
    {
        this.array=new ArrayList<Integer>(N);
        this.index = 0;
        this.size=N;
    }

    public synchronized void put(int e) throws InterruptedException 
    {
        l.lock();
        try
        {
            while(this.index >= this.size)
            {
                notFull.await();
            }
            this.array.add(index,e);
            this.index++;
            notEmpty.signal();
        }
        finally
        {
            l.unlock();
        }       
    }

    public synchronized int get() throws InterruptedException 
    {
        int i;
        l.lock();
        try
        {   
            while(this.index <=0)
            {           
                notEmpty.await();
            }
            this.index--;
            notFull.signal();
            i = this.array.get(index);
        }
        finally
        {
            l.unlock();
        }
         return i;

    }
}

class Consumidor extends Thread
{
    private BoundedBuffer b;
    final int j;
    public Consumidor(BoundedBuffer b, int j)
    {
        this.b = b;
        this.j=j;
    }
    public void run() 
    { 
        int a;
        for (int i = 0; i < j ;++i)
        {  
            try
            {  
                a=b.get();
                System.out.println("GET: " +a); 
            }
            catch (Exception e) {}
        }
    }
}


class Produtor extends Thread
{
    private BoundedBuffer b;
    final int j;
    public Produtor(BoundedBuffer b, int j)
    {
        this.b = b;
        this.j=j;
    }
    public void run() 
    { 
        int a;
        for (int i = 0; i < j; ++i)
        {   
            try
            { 
                b.put(i);
                System.out.println("PUT: " +i);
            }
            catch (Exception e) {}
        }
    }
}

班长
{
公共静态void main(字符串[]args)引发InterruptedException
{
final int N=Integer.parseInt(args[0]);
BoundedBuffer teste=新的BoundedBuffer(N);
螺纹c=新的消耗品(测试仪,N);
螺纹p=新的生产商(测试仪,N);
c、 start();
p、 start();
c、 join();
p、 join();
}
}
类边界缓冲区
{
数组列表数组;
整数指数;
整数大小;
锁l=新的可重入锁();
条件notFull=l.newCondition();
条件notEmpty=l.newCondition();
BoundedBuffer(int N)
{
this.array=newarraylist(N);
该指数=0;
这个。大小=N;
}
公共同步的void put(int e)抛出InterruptedException
{
l、 锁();
尝试
{
而(this.index>=this.size)
{
未满。等待();
}
this.array.add(索引,e);
这个.index++;
notEmpty.signal();
}
最后
{
l、 解锁();
}       
}
public synchronized int get()引发InterruptedException
{
int i;
l、 锁();
尝试
{   

while(this.index不要将内部锁(意思是
同步的
)与reentrantLocks混合使用。此代码尝试获取内部锁,然后获取reentrantlock

synchronized
放在实例方法上需要调用该方法的线程获取实例上的内在锁。ReentrantLock是一个单独的锁构造,不使用该关键字。混合使用这两种机制是不必要的,只会造成麻烦

(具体地说,代码正在条件对象上调用wait,这会导致线程释放可重入锁,但线程一直保持内在锁,阻止另一个线程进入同步方法。)


解决方法是从代码中删除
synchronized
关键字。

当我运行它时,它不会完成程序,它会在notEmpty.await()停止,它应该让生产者负责并插入项目,但它会在消费者处停止