Java MultiThreading Question

Dear All,

I have a problem with multi-thread programming in Java.

Here i have 3 classess:

Executor.java
----------------------------------------------------------------------
import      java.util.*;

public class Executor
{
      public ArrayList<Item> myItemList;
      public boolean myToLoop;
      public Executor()
      {
            myItemList      = new ArrayList<Item>();
            myToLoop      = true;
            new Thread(new Processor(this)).start();
            while (Item.CURRENT_ID < 200)
            {
                  Item item = new Item(Item.getNextId(), Item.MESSAGE_DATABASE[Item.CURRENT_ID % Item.MESSAGE_DATABASE.length]);
                  myItemList.add(item);
                  System.out.println ("**** Executor : " + item.toString());
                  try
                  {
                        Thread.sleep(10);
                  }
                  catch (InterruptedException e)
                  {
                        System.out.println(e.getMessage());
                  }
            }
            myToLoop      = false;
      }
      public static void main(String[] args)
      {
            Executor      executor      = new Executor();
      }
}

Processor.java
----------------------------------------------------------------------
import      java.util.*;
public class Processor implements Runnable
{
      public static final int TIME_OUT      = 100;
      public Executor myExecutor;
      public Processor(Executor executor)
      {
            myExecutor = executor;
      }
      public void run()
      {
            while (myExecutor.myToLoop)
            {
                  try
                  {
                        Thread.sleep(TIME_OUT);
                        Iterator      iterator      = myExecutor.myItemList.iterator();
                        while (iterator.hasNext())
                        {
                              Object obj = iterator.next();
                              System.out.println ("#### Processor : " + obj.toString());
                        }
                        myExecutor.myItemList.clear();
                  }
                  catch (InterruptedException e)
                  {
                        System.out.println (e.getMessage());
                  }
            }
      }
}

Item.java
----------------------------------------------------------------------
public class Item
{
      public static int CURRENT_ID      = 0;
      public static String[] MESSAGE_DATABASE      = {"Message A", "Message B", "Message C", "Message D", "Message E", "Message F", "Message G", "Message H"};
      public int myId;
      public String myMessage;
      public Item()
      {
            myId = 0;
            myMessage = "";
      }
      public Item(int id, String message)
      {
            myId = id;
            myMessage = message;
      }
      public String toString()
      {
            return "[" + myId + ", " + myMessage + "]";
      }
      
      public static int getNextId()
      {
            return CURRENT_ID++;
      }
}

This program suppose to work in a way that
* Executor
Once an Executor object has been created
1) Creates a new Thread with a Processer object and start the thread
2) Displays and adds new Item objects into an arraylist in every 10ms until condition has been fulfilled
*Processor
1) Keep looping until the boolean value "myToLoop" is set to false (in this case after the executor exits the while loop)
2) Loop through the ArrayList in an executor object and display the data onto the screen
3) Empty the ArrayList and sleep for 100ms

When i executes the program, a ConcurrentModificationException throws from "Object obj = iterator.next();" (Processer: 267)
**** Executor : [0, Message B]
**** Executor : [1, Message C]
**** Executor : [2, Message D]
**** Executor : [3, Message E]
**** Executor : [4, Message F]
**** Executor : [5, Message G]
**** Executor : [6, Message H]
**** Executor : [7, Message A]
**** Executor : [8, Message B]
**** Executor : [9, Message C]
#### Processor : [0, Message B]
#### Processor : [1, Message C]
#### Processor : [2, Message D]
#### Processor : [3, Message E]
#### Processor : [4, Message F]
..............
...............
**** Executor : [35, Message E]
**** Executor : [36, Message F]
#### Processor : [28, Message F]
#### Processor : [29, Message G]
#### Processor : [30, Message H]
#### Processor : [31, Message A]
**** Executor : [37, Message G]
Exception in thread "Thread-0" java.util.ConcurrentModificationException
        at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
        at java.util.AbstractList$Itr.next(Unknown Source)
        at Processor.run(Processor.java:26)
        at java.lang.Thread.run(Unknown Source)
**** Executor : [38, Message H]
**** Executor : [39, Message A]
**** Executor : [40, Message B]

What does the exeception actually means???and how do i fix the problem.???

Thanks a lot~!
SlowangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You need to synchronize access to the List

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SlowangAuthor Commented:
isnt ArrayList thread-safe by default??????
CEHJCommented:
No. See API docs:

"Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally."
CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.