生产者消费者中的死锁 [英] DeadLock in producer Consumer

查看:559
本文介绍了生产者消费者中的死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    I have following classes :

    package com.akshu.multithreading;

    public class ThreadResource {

        static int a;
     static boolean Value =false;
        public synchronized int getA() {
            while(Value == false){
                try {
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            Value= false;
            notify();


            return a;
        }

        public synchronized void setA(int a) {
            while(Value == true)
            {
                try {
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            ThreadResource.a = a;
            Value=true;
            notify();

        }


    }


    ------------------


    /**
     * 
     */
    package com.akshu.multithreading;

    /**
     * @author akshu
     *
     */
    public class MyThreadA implements Runnable  {

        int a = 0;
        ThreadResource tR= new ThreadResource();

        @Override
        public void run() {


        for (int i = 0; i < 15; i++) {



            tR.setA(++a);
            System.out.println(" value of a :"+a);





        }

        }
    }

    ------------

    package com.akshu.multithreading;

    public class MyThreadB implements Runnable {

        @Override
    public void run() {
        // TODO Auto-generated method stub
        ThreadResource tR =new ThreadResource();
        for (int i = 0; i < 15; i++) {

        System.out.println("getA()"+tR.getA());

                }
    }
    }
    ----

    package com.akshu.multithreading;

    public class ThreadExecutionPoint {

        public static void main(String args[])  {

            Thread th1 = new Thread(new MyThreadA());
            Thread th2 = new Thread(new MyThreadB());

            th1.start();
            th2.start();
        }
    }

我试图通过上面的代码来理解生产者消费者的问题.当我执行上面的代码时,我会得到

I am trying to understand producer consumer problem via above code .When i execute the above code i am getting

    value of a :1
    getA()1

程序仅卡在这里(不终止).

Program gets stuck here only (do not gets terminate).

有人请解释我在这里做错什么了吗

Someone Please explain what wrong i am doing here?

推荐答案

Value声明为volatile
IE. static volatile boolean Value =false;
您已经声明了set/get方法synchronized.这意味着它们已锁定在this上(对象的固有锁定).
但是在您的代码中,您为每个线程实例化了一个不同的ThreadResource,因此使它们成为synchronized,因为每种情况下this都是不同的.
如下更改您的代码:

Declare Value as volatile
I.e. static volatile boolean Value =false;
You have declared your set/get methods synchronized. This means that they are lock on this (the object's intrinsic lock).
But in your code you instantiate a different ThreadResource for each thread thereby not making them synchronized since this is different for each case.
Change your code as follows:

public class MyThreadA implements Runnable {  
    ThreadResource tR;  

     public MyThreadA(ThreadResource tr) {  
        this.tR = tr;  
    }  
// your run method here NOT declaring a ThreadResource anymore!!!
}   

MyThreadB

然后在ThreadExecutionPoint

ThreadResource tr = new ThreadResource();  
Thread th1 = new Thread(new MyThreadA(tr));  
Thread th2 = new Thread(new MyThreadB(tr));  

这篇关于生产者消费者中的死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆