问题描述
importjava.nio.Buffer;importjava.nio.IntBuffer;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReentrantLock;publicclassSuperThread{//定义缓冲区的读写锁privateLockbufferLock=newReentrantLock();//共享缓冲区publicstaticIntBufferbuffer=IntBuffer.allocate(5);//生产者数组publicintarray[]={1,5,6,9,8,11,13,10,7,3};//生产者执行的次数publicstaticintcount=0;//两个生产者取出数据的累加publicstaticintconsumerSum=0;//记录消费者取数次数publicstaticintconsumerCount=0;//存放从缓冲区中取得的数据privatestaticintconsumerData;//定义线程名publicStringnameString;//线程互斥信号,值为0表示生产者进程,值为1表示消费者进程publicstaticintmutex=0;/*构造函数*/publicSuperThread(Stringname){//主要完成一些变量的初始化}/*获得线程名*/publicStringgetNameString(){returnnameString;}/*设置线程名*/publicvoidsetNameString(StringnameString){this.nameString=nameString;}/*获得互斥信号*/publicstaticintgetMutex(){returnmutex;}/*设置互斥信号*/publicstaticvoidsetMutex(intmutex){SuperThread.mutex=mutex;}/*判断是否有线程访问缓冲区,没有则对线程进行加锁*/publicbooleanwait(intmutex){if(mutex==1){returnfalse;}else{returntrue;}}/*释放进程操作完成信号,其实本质就是修改mutex的信号值*/publicstaticvoidsignal(){mutex=0;}publicbooleanisBufferEmpty(){//判断缓冲区是否为空intcount=0;for(inti=0;i<buffer.capacity();i++){if(buffer.get(i)==0){//根据缓冲区中的0的个数来判断,若缓冲区0的个数为5(缓冲区容量为5),则为空count++;}}if(count==5){returntrue;}else{returnfalse;}}publicbooleanisBufferFull(){//判断缓冲区是否为满intcount=0;for(inti=0;i<buffer.capacity();i++){if(buffer.get(i)!=0){//根据缓冲区中不为0的个数来判断,若缓冲区不为0的个数小于5(缓冲区容量为5)则不为满count++;}}if(count<5){returnfalse;}else{returntrue;}}/*消费数,即在缓冲区中取数*/publicvoidconsume(){if(wait(mutex)&&!isBufferEmpty()){bufferLock.lock();//对该程序段加锁,防止其他线程访问缓冲区try{setMutex(1);//将线程互斥信号值mutex设置为1,使之互斥intindex=0;//存放顺序查找第一个为0的数的下摆inti;for(i=0;i<buffer.capacity();i++){//顺序查找第一个为0的数if(buffer.get(i)==0){index=i;break;}elseif(buffer.get(4)>0){//缓冲区为满的情况index=5;}}consumerData=getBufferData(index-1);//取数据consumerCount++;if(consumerCount<=2){consumerSum=consumerSum+consumerData;//计算两次取数之和if(consumerCount==2){System.out.println("消费者取数之和为:"+consumerSum);consumerSum=0;consumerCount=0;}}buffer.put(index-1,0);//将已取过数的缓冲区重新置0//consumerPrintBufferData();//输出取数后的缓冲区signal();//释放互斥信号,将mutex设置为0}finally{bufferLock.unlock();//解锁}}}/*消费者取得缓冲区数据*/publicintgetBufferData(intindex){//完成取数操作returnbuffer.get(index);}/*生产数,即向缓冲区中存数*/publicvoidproduce(){if(wait(mutex)&&!isBufferFull()){bufferLock.lock();//对该程序段加锁,防止其他线程访问缓冲区try{setMutex(1);//将线程互斥信号值mutex设置为1,使之互斥intindex=0;for(inti=0;i<buffer.capacity();i++){//寻找存数的位置if(buffer.get(i)==0){index=i;break;}}putBufferData(index);//向缓冲区中存入数据signal();//释放互斥信号,将mutex设置为0}finally{bufferLock.unlock();//解锁}}}/*生产者向公共缓冲区放数据*/publicvoidputBufferData(intindex){buffer.put(array[index]);//完成想缓冲区存数操作}}publicclassTest{publicstaticvoidmain(String[]args){Producerproducer=newProducer("producer");Consumerconsumer1=newConsumer("consumer1");Consumerconsumer2=newConsumer("consumer2");newThread(producer).start();newThread(consumer1).start();newThread(consumer2).start();}}后面是测试包含main方法的类,producer和consumer中run方法只是执行superthread中prduce和consum方法,执行producer线程后貌似执行consumer1线程时总是break;
解决方案
解决方案二:
给点提示也好