mxy414432523 2017-11-28 14:00 采纳率: 0%
浏览 718

萌新求教这个代码哪里有问题,如果没问题在dos命令行编译运行的时候打什么名字,因为导入了包。

import java.util.concurrent.locks.*;

class Resource
{

Lock lock=new ReentrantLock();
Condition produce=lock.newCondition();
Condition consumer=lock.newCondition();
Object[] a=new Object[10];
int count,input,output;
public void pro(Object x) 
{   lock.lock();
    try
    {
        try
        {
            while (count==a.length)
            {
                produce.await();

            }
        }
        catch (InterruptedException e1)
        {
            System.out.println();
        }
        a[input]=x;
        if(++input==a.length)
            input=0;
        count++;
        System.out.println(Thread.currentThread().getName()+"生产对象"+x);
        consumer.signal();


    }
    finally
    {
        lock.unlock();
    }


}
public Object con() 
{
    lock.lock();
    try
    {
        try
        {
            while(count==0)
                {consumer.await();}
        }
        catch (InterruptedException e2)
        {
            System.out.println();
        }
        Object y=a[output];
        if(++output==a.length)
            output=0;
        count--;

        produce.signal();
        return y;
    }
    finally
     { 
    lock.unlock();
      }



}

}
class Make implements Runnable
{ Resource r=new Resource();

public Object makemake()
{
    Object o=new Object();
    return o;
}

public void run() 
{
    r.pro(makemake());
    r.pro(makemake());
    r.pro(makemake());
    r.pro(makemake());
    r.pro(makemake());
    r.pro(makemake());
    r.pro(makemake());
    r.pro(makemake());
    r.pro(makemake());
    r.pro(makemake());
}

}
class Spend implements Runnable
{
Resource r=new Resource();
public void run()
{
for (int x =0;x<10 ;x++ )
{
System.out.println(Thread.currentThread().getName()+"消费对象"+r.con());
}

}

}

class ProConTest
{
public static void main(String[] args)
{
Spend s=new Spend();
Make m=new Make();
Thread t1=new Thread(m);
Thread t2=new Thread(s);
t1.start();
t2.start();

}

}

  • 写回答

1条回答 默认 最新

  • devmiao 2017-11-28 15:52
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料