1

Let's assume that there are three methods in a class as follows and I need to call each method one after another one, but as each method is running on a separate Thread I get errors, please tell me a way to call each of following methods as,

..... methodOne(); methodTwo(); methodTree(); ..... public Class Test{ public void methodOne(){ new Thread(new Runnable(){ @Override public void run() { ..... } }).start(); } public void methodTwo(){ new Thread(new Runnable(){ @Override public void run() { ..... } }).start(); } public void methodThree(){ new Thread(new Runnable(){ @Override public void run() { ..... } }).start(); } } 

I get following errors

java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:778) at java.util.LinkedList$ListItr.next(LinkedList.java:713) at Manufacturing.MonthEnd.FinishMonth.fillPreviousMonthBalanceRec(FinishMonth.java:141) at Manufacturing.MonthEnd.FinishMonth.closeMonth(FinishMonth.java:52) at Manufacturing.MonthControllerView.btnCloseMonthActionPerformed(MonthControllerView.java:588) at Manufacturing.MonthControllerView.access$400(MonthControllerView.java:28) at Manufacturing.MonthControllerView$6.actionPerformed(MonthControllerView.java:465) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253) at java.awt.Component.processMouseEvent(Component.java:6203) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:5968) at java.awt.Container.processEvent(Container.java:2105) at java.awt.Component.dispatchEventImpl(Component.java:4564) at java.awt.Container.dispatchEventImpl(Container.java:2163) at java.awt.Component.dispatchEvent(Component.java:4390) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055) at java.awt.Container.dispatchEventImpl(Container.java:2149) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4390) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649) at java.awt.EventQueue.access$000(EventQueue.java:96) at java.awt.EventQueue$1.run(EventQueue.java:608) at java.awt.EventQueue$1.run(EventQueue.java:606) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116) at java.awt.EventQueue$2.run(EventQueue.java:622) at java.awt.EventQueue$2.run(EventQueue.java:620) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105) at java.awt.EventQueue.dispatchEvent(EventQueue.java:619) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java:138) 
1
  • 2
    What errors are you getting? Can you post the stacktrace / exceptions on your question? Commented Feb 15, 2012 at 8:17

5 Answers 5

3

If you need to call them "one after another one", why do you need separate threads? Can't they run all in the same one (the main)?

Sign up to request clarification or add additional context in comments.

1 Comment

I have separate progress bars for the tasks of each method, that is the case I had to use separate threads.
2

You can use method

new Thread().join(); 

And wait for previous thread to die. Then call next method.

Your error means that some thread modify your List object, and another thread at the same time iterating it. So thread can't iterate throught this list when list has changes its content. you should use synchronized block code, some thing like:

List listeners; ... synchronized(listeners){ for(Object o : listeners){ } } 

Comments

1

Before coding everyone shall clear his/her needs! Just clear yourself what is your need? Do you need sequential or concurrent execution? After that look at the tools you've got according to your need. If you need concurrent access, just look at java.util.concurrent package. There are good classes which would help you in that package.

Comments

1

Test this it's working fine.

public class ThreadTest { public static void main(String... args) { Test t = new Test(); t.methodOne(); t.methodTwo(); t.methodThree(); } } class Test { public void methodOne() { new Thread(new Runnable() { @Override public void run() { System.out.println("I AM A THREAD FROM METHOD ONE."); } }).start(); } public void methodTwo() { new Thread(new Runnable() { @Override public void run() { System.out.println("I AM A THREAD FROM METHOD TWO."); } }).start(); } public void methodThree() { new Thread(new Runnable() { @Override public void run() { System.out.println("I AM A THREAD FROM METHOD THREE."); } }).start(); } } 

As for the posted stackTrace, this error comes when you are trying to iterate over a collection and at the same time you trying to modify it's values. In order to modify the values always use Iterator and you need to synchronize your threads too, so that no two threads modify the same collection object at any given time.

Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

2 Comments

It is obvious that it's a typo! Just look at the stacktrace.
@AmirPashazadeh : Do check the answer too regarding that, before commenting half known things.
0

There's an instance of LinkedList that is being accessed and modified from different threads.

Quick fix is to add private setters and getters for the list, mark them as synchronized and access the list only using these synchronized methods.

1 Comment

That won't help, the code is iterating on a list in one thread and modifying the list (I think removing elements from list) in another. Best solution is to use a Concurrent support list, but I believe he must clarify his needs first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.