2 miaowei128 miaowei128 于 2015.07.12 08:35 提问

java 线程异常问题 并发性

package ListTest;

import java.util.ArrayList;

public class BookList {
ArrayList bl =new ArrayList();

public void chunBan(){
    Book b1= new Book();
    b1.setBid(1);
    b1.setName("java");
    b1.setPrice(32.00);
    bl.add(b1);

    Book b2 = new Book();
    b2.setBid(2);
    b2.setName("c++");
    b2.setPrice(56.00);
    bl.add(b2);
}

public void chaXun(int bid) {
    for (Book book : bl) {
        if (book.getBid()==bid) {
            System.out.println(book.getName()+"\t"+book.getPrice());
        }
    }
}

public void  see() {
    for (Book book : bl) {
        System.out.println(book.getName());
    }
}

public  void update(int bid,double price) {
    for (Book book : bl) {
        if (book.getBid()==bid) {
            book.setPrice(price);
        }
    }
}

public void remove2(int bid) {
    for (Book book : bl) {
        if (book.getBid()==bid) {
            bl.remove(book);
        }
    }
}

public void jiXunChuBan(int bid,String name,double price) {
    int k=bl.size();
    Book b3 = new Book(bid,name,price);
    bl.add(k,b3);
}
public void qingKong() {
    bl.clear();
}

}

package ListTest;

public class Book {
private int bid;
private String name;
private double price;
public Book(int bid, String name, double price) {
super();
this.bid = bid;
this.name = name;
this.price = price;
}
public Book() {
super();
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}

}

package ListTest;

public class BookMain {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    BookList blist = new BookList();
    blist.chunBan();
    blist.see();
    blist.jiXunChuBan(3, "c", 59.00);
    blist.see();
    blist.remove2(3);
    blist.see();


}

}

5个回答

tongyi55555
tongyi55555   2015.07.12 09:21

把remove2方法改成下面的。遍历list时执行了删除操作不能在进行遍历了。

public void remove2(int bid) {
        for (Book book : bl) {
            if (book.getBid() == bid) {
                bl.remove(book);
                break;
            }
        }
    }
oyljerry
oyljerry   Ds   Rxr 2015.07.12 11:06

你线程操作同一个list等时,需要做互斥保护

danielinbiti
danielinbiti   Ds   Rxr 2015.07.12 12:14
 public void remove2(int bid) {
    for (Book book : bl) {
        if (book.getBid()==bid) {
            bl.remove(book);
        }
    }
}
用foreach方式循环遍历删除会报错
用for的方式
for(int i=bl.size()-1;i>=0;i--){
    Book book = (Book)bl.get(i);
    if (book.getBid()==bid) {
            bl.remove(book);
        }
}
cuiwei1026522829
cuiwei1026522829   Ds   Rxr 2015.07.12 23:29

public void remove2(int bid) {
for (Book book : bl) {
if (book.getBid() == bid) {
bl.remove(book);
break;
}
}
}

CSDNXIAOD
CSDNXIAOD   2015.07.16 15:23

《Java并发》:第五节 线程异常处理
java 线程 捕获异常
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片