joelg236
31-05-2012, 23:50
private int currentCommand;
private void runConcurrentCommands(final Vector<CommandBase> v) {
Thread thread = null;
for(currentCommand = 0; currentCommand < v.size(); currentCommand++) {
System.out.println("Current = "+currentCommand);
thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Running = "+currentCommand);
CommandBase.getInstance().run(v.get(currentCommand ));
}
});
thread.start();
}
try {
thread.join(1000);
} catch (InterruptedException ex) {
} catch (NullPointerException ex) {
}
}
Why does this give me the output of
run:
Sequential: team4334.code.commands.ExampleCommand@5b86d4c1
Run
Sequential: team4334.code.commands.ExampleCommand@70f9f9d8
Run
Sequential: team4334.code.features.Feature$1@2b820dda
Concurrent: team4334.code.commands.modes.ExampleMode@675b7986
Concurrent: team4334.code.commands.ExampleCommand@2687816d
Concurrent: team4334.code.commands.modes.ExampleMode@a422ede
Current = 0
Current = 1
Running = 1
Current = 2
Running = 2
Run
Run
Running = 3
Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
Sequential: team4334.code.commands.modes.ExampleMode@7ca83b8a
Run
at java.util.Vector.get(Vector.java:721)
at team4334.code.features.Feature$2.run(Feature.java: 113)
at java.lang.Thread.run(Thread.java:679)
BUILD SUCCESSFUL (total time: 3 seconds)
Is this a logic error or a concurrency error? In either case, what would be the best way to fix it?
I feel like the new Runnable is changing the value of currentCommand even before the for loop notices it is out of the range. If that is the case, how could I solve this problem? I have tried making the variable volatile (With the same result) and making the methods synchronized. I'm stumped.
private void runConcurrentCommands(final Vector<CommandBase> v) {
Thread thread = null;
for(currentCommand = 0; currentCommand < v.size(); currentCommand++) {
System.out.println("Current = "+currentCommand);
thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Running = "+currentCommand);
CommandBase.getInstance().run(v.get(currentCommand ));
}
});
thread.start();
}
try {
thread.join(1000);
} catch (InterruptedException ex) {
} catch (NullPointerException ex) {
}
}
Why does this give me the output of
run:
Sequential: team4334.code.commands.ExampleCommand@5b86d4c1
Run
Sequential: team4334.code.commands.ExampleCommand@70f9f9d8
Run
Sequential: team4334.code.features.Feature$1@2b820dda
Concurrent: team4334.code.commands.modes.ExampleMode@675b7986
Concurrent: team4334.code.commands.ExampleCommand@2687816d
Concurrent: team4334.code.commands.modes.ExampleMode@a422ede
Current = 0
Current = 1
Running = 1
Current = 2
Running = 2
Run
Run
Running = 3
Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
Sequential: team4334.code.commands.modes.ExampleMode@7ca83b8a
Run
at java.util.Vector.get(Vector.java:721)
at team4334.code.features.Feature$2.run(Feature.java: 113)
at java.lang.Thread.run(Thread.java:679)
BUILD SUCCESSFUL (total time: 3 seconds)
Is this a logic error or a concurrency error? In either case, what would be the best way to fix it?
I feel like the new Runnable is changing the value of currentCommand even before the for loop notices it is out of the range. If that is the case, how could I solve this problem? I have tried making the variable volatile (With the same result) and making the methods synchronized. I'm stumped.