Java线程抛出异常
我尝试使用线程来实现超市结账系统。每个客户端都有一个id和等待时间。以最小的等待时间将客户端添加到签出。签出等待时间是来自该签出的客户端的等待时间之和。等待时间以秒为单位,每秒钟都在减少。当客户端等待时间为0时,我需要将其从队列中删除,这就是问题所在。它总是抛出一个异常Java线程抛出异常,java,multithreading,oop,arraylist,iterator,Java,Multithreading,Oop,Arraylist,Iterator,我尝试使用线程来实现超市结账系统。每个客户端都有一个id和等待时间。以最小的等待时间将客户端添加到签出。签出等待时间是来自该签出的客户端的等待时间之和。等待时间以秒为单位,每秒钟都在减少。当客户端等待时间为0时,我需要将其从队列中删除,这就是问题所在。它总是抛出一个异常 public class Checkout extends Thread{ public ArrayList<Client> clients = new ArrayList<Client>()
public class Checkout extends Thread{
public ArrayList<Client> clients = new ArrayList<Client>();
public ArrayList<Client> deleted = new ArrayList<Client>();
Iterator<Client> it = clients.iterator();
int sum=0;
View v;
Shop m;
public Checkout(String nume) {
setName(nume);
}
public void run() {
try {
while(true)
{
delete_client();
sleep(1000);
}
}catch(InterruptedException e) {
System.out.println(e.toString());
}
}
public synchronized void add_client(Client c) throws InterruptedException{
clients.add(c);
sum=sum+c.getWaintinTime();
notifyAll();
}
public synchronized void delete_client()throws InterruptedException{
while (clients.size()==0)
wait();
while(it.hasNext())
{
Client c = it.next();
if(c.getDecrement()==0)
{
v.display("\nTime: "+ m.curentTime +" Client "+Long.toString(c.getID()+1)+" leaved the checkout " + getName());
deleted.add(c);
}
clients.removeAll(deleted);
}
notifyAll();
}
public synchronized long waiting_time() throws InterruptedException{
notifyAll();
return sum;
}
}
public class Shop extends Thread {
private Checkout checkout[];
private int nr_checkouts;
static int id =0;
private int nr_clients;
public int waitingTime; // random from interval (wMin, wMax)
public int wMax,wMin; //get them from TextFields
View v;
Random r = new Random();
public Shop(View v, int wMin1, int wMax1, int nr_checkouts, Checkout checkout[], String name, int nr_clients) {
setName(name);
this.v=v;
this.wMax=wMax1;
this.wMin=wMin1;
this.nr_checkouts = nr_checkouts;
this.checkout= new Checkout[nr_checkouts];
this.nr_clients = nr_clients;
for(int i =0; i<nr_checkouts;i++) {
this.checkout[i]=checkout[i];
}
}
private int min_index() {
int index = 0;
try {
long min = checkout[0].waiting_time();
for(int i =1 ; i<nr_case;i++) {
long lung = casa[i].waiting_time);
if(lung<min) {
min = lung;
index = i;
}
}
}catch ( InterruptedException e ) {
System.out.println(e.toString());
}
return index;
}
public void run(){
try{
int i=0;
while( i<nr_clients ){
i++;
waitingTime = r.nextInt((wMax-wMin)+1)+wMin;
Client c = new Client(waitingTime,id++);
int m = min_index();
currentTime++;
checkout[m].add_client( c ); //add it ad the checkout with minimum waiting time
v.display("\nTime "+ currentTime +" Client " +Long.toString( id )+" | with waiting time " + waitingTime+" | came at checkout "+ Integer.toString(m));
sleep( 1000 );
}
}
catch( InterruptedException e ){
System.out.println(e.toString());
}
}
}
public class Client {
private int waitingTime;
private int id=0;
public Client(int waitingTime,int id)
{
this.id=id;
this.waitingTime = waitingTime;
}
public int getDecrement() {
return this.waitingTime--;
}
Exception in thread "Checkout 1" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at thread.thread.Checkout.delete_client(Checkout.java:55)
at thread.thread.Checkout.run(Checkout.java:35)
第35行:delete_client()代码>
第55行:Client c=it.next()
当您在迭代列表时调用修改方法(如removeAll
)时,它将抛出ConcurrentModificationException
您可以这样做:
it = clients.iterator();
while(it.hasNext())
{
Client c = it.next();
if(c.getDecrement()==0)
{
v.display("\nTime: "+ m.curentTime +" Client "+Long.toString(c.getID()+1)+" leaved the checkout " + getName());
deleted.add(c);
it.remove();
}
}
当两个线程希望同时使用同一资源时,将抛出ConcurrentModificationException
您使用了synchronized关键字,这很好,但是出现异常是因为您在同一个类中调用delete\u client,而没有另一个lock对象。报告说:
首先,同一对象上的两个同步方法调用不可能交错。当一个线程对一个对象执行同步方法时,所有其他线程对同一对象块调用同步方法(暂停执行),直到第一个线程对该对象执行完为止
因此,您可以执行以下操作:
private final Object countLock = new Object();
.
.
.
while(true)
{
synchronized (countLock) {
delete_client();
}
sleep(1000);
}
移动clients.removeAll(已删除)代码>退出循环,或者使用i modify,它仍然是相同的。@请尝试在while循环之前创建迭代器。请参阅我的最新答案。