多线程生产者消费者模型线程失控

AMX50B 2018-03-26 11:21:39
public class Resource {
private List res=new LinkedList();
private int count;
private boolean flag=false;
public synchronized void setRes(String name){
while (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
res.add(count++);
System.out.println(Thread.currentThread().getName()+"++++++"+"add"+count+"******"+res.size()+"inSize");
if(res.size()>=10){
this.flag=true;
this.notifyAll();
}
}
public synchronized void getRes(){
while (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"------"+"remove"+count--+"********"+res.size()+"outSize");
res.remove(0);
if(res.size()==0){
this.flag=true;
this.notifyAll();
}
}
}

分别两个线程执行该资源的setRes和getRes当是同步没控制住

Exception in thread "Thread-3" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
at java.util.LinkedList.remove(LinkedList.java:525)
at Resource.getRes(Resource.java:38)
at outThread.run(outThread.java:15)
at java.lang.Thread.run(Thread.java:748)
Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
at java.util.LinkedList.remove(LinkedList.java:525)
at Resource.getRes(Resource.java:38)
at outThread.run(outThread.java:15)
at java.lang.Thread.run(Thread.java:748)
Thread-1++++++add1******1inSize
Thread-1++++++add2******2inSize
Thread-1++++++add3******3inSize
Thread-1++++++add4******4inSize
Thread-1++++++add5******5inSize
Thread-1++++++add6******6inSize
Thread-1++++++add7******7inSize
Thread-1++++++add8******8inSize
Thread-1++++++add9******9inSize
Thread-1++++++add10******10inSize
Thread-3------remove10********10outSize
Thread-3------remove9********9outSize
Thread-3------remove8********8outSize
Thread-3------remove7********7outSize
Thread-3------remove6********6outSize
Thread-3------remove5********5outSize
Thread-3------remove4********4outSize
Thread-3------remove3********3outSize
Thread-3------remove2********2outSize
Thread-3------remove1********1outSize
Thread-3------remove0********0outSize
Thread-2------remove-1********0outSize
...全文
367 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
AMX50B 2018-03-26
  • 打赏
  • 举报
回复
引用 4 楼 lye2000000_super 的回复:
你想的有点复杂,改成这样吧 if(res.size()==10){ this.flag=true; this.notifyAll(); } else{ res.add(count++); System.out.println(Thread.currentThread().getName()+"++++++"+"add"+count+"******"+res.size()+"inSize"); } if(res.size()==0){ this.flag=true; this.notifyAll(); } else{ res.remove(0); System.out.println(Thread.currentThread().getName()+"------"+"remove"+count--+"********"+res.size()+"outSize"); }
已解决,出了个智障错误,下方的flag应该贴false标签搞错了
  • 打赏
  • 举报
回复
你想的有点复杂,改成这样吧 if(res.size()==10){ this.flag=true; this.notifyAll(); } else{ res.add(count++); System.out.println(Thread.currentThread().getName()+"++++++"+"add"+count+"******"+res.size()+"inSize"); } if(res.size()==0){ this.flag=true; this.notifyAll(); } else{ res.remove(0); System.out.println(Thread.currentThread().getName()+"------"+"remove"+count--+"********"+res.size()+"outSize"); }
AMX50B 2018-03-26
  • 打赏
  • 举报
回复
public class Resource {
//    private List res=new LinkedList();
    private int count =0;//
    private boolean flag=false;
    public synchronized void setRes(String name){
        while (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"++++++++++++"+(++count));
        this.flag=true;
        this.notifyAll();
//        res.add(count++);
//        System.out.println(Thread.currentThread().getName()+"++++++"+"add"+count+"******"+res.size()+"inSize");
//        if(res.size()>=10){
//            this.flag=true;
//            this.notifyAll();
//        }
    }
    public synchronized void getRes(){
        while (!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"-----------"+count);
        this.flag=false;
        this.notifyAll();
//        System.out.println(Thread.currentThread().getName()+"------"+"remove"+count--+"********"+res.size()+"outSize");
//        res.remove(0);
//        if(res.size()==0){
//            this.flag=true;
//            this.notifyAll();
//        }
    }
}
控制单个数据是没问题的,控制缓冲取出问题了,怀疑是不是我判断出问题了
Thread-0++++++++++++1
Thread-3-----------1
Thread-1++++++++++++2
Thread-3-----------2
Thread-0++++++++++++3
Thread-3-----------3
Thread-1++++++++++++4
Thread-3-----------4
Thread-0++++++++++++5
Thread-3-----------5
Thread-1++++++++++++6
Thread-3-----------6
Thread-0++++++++++++7
Thread-3-----------7
Thread-1++++++++++++8
Thread-3-----------8
Thread-0++++++++++++9
Thread-3-----------9
Thread-1++++++++++++10
Thread-3-----------10
Thread-0++++++++++++11
Thread-3-----------11
Thread-1++++++++++++12
Thread-3-----------12
AMX50B 2018-03-26
  • 打赏
  • 举报
回复
引用 1 楼 lye2000000_super 的回复:
https://www.cnblogs.com/uodut/p/6775419.html 参考下这个吧。不需要你写的这么复杂吧
我是想让存线程往list存数据10条数据,等存满十条数据后由唤醒取线程取数据,就是让list做个缓冲
  • 打赏
  • 举报
回复
https://www.cnblogs.com/uodut/p/6775419.html 参考下这个吧。不需要你写的这么复杂吧

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧