Java线程抛出异常

时间:2018-04-08 15:04:03

标签: java multithreading oop arraylist iterator

我尝试使用线程进行超市结账系统。每个客户都有一个id和一个等待时间。使用最短的等待时间将客户端添加到结帐。结账等待时间是客户从该结账的等待时间的总和。等待时间以秒为单位,每秒都在减少。当客户端等待时间为0时,我需要从队列中删除它,这就是问题所在。它不断抛出异常

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

2 个答案:

答案 0 :(得分:2)

当两个线程想要同时使用相同的资源时,抛出 ConcurrentModificationException

您使用 synchronized 关键字,这很好,但是发生异常是因为您在没有其他锁定对象的同一个类中调用 delete_client https://wiki.openstreetmap.org/wiki/Libchamplain说:

  

首先,对同一对象的两个同步方法的调用不可能进行交错。当一个线程正在为一个对象执行一个synchronized方法时,所有其他线程都会调用同步方法。相同的对象块(暂停执行),直到第一个线程完成对象。

因此,您可以执行以下操作:

private final Object countLock = new Object();
.
.
.
while(true)
{
   synchronized (countLock) {
       delete_client();
   }
   sleep(1000);                
}

答案 1 :(得分:1)

在迭代removeAll时调用List等修改方法时,会抛出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();
    }
}