62,635
社区成员




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();
}
}
}
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
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