Solved.
For the benefit of anyone that might have been curious as to the solution or are looking around for ways to do this - here it is.
Code:
private int currentCommands;
private synchronized void runConcurrentCommands(final Vector<CommandBase> v) {
try {
concurrentCommandsFinished = false;
Thread[] threads = new Thread[v.size()];
for(int x = 0; x < v.size(); x++) {
currentCommands = x;
threads[x] = new Thread(new Runnable() {
public final int commandNum = currentCommands;
@Override
public void run() {
System.out.println("Running "+commandNum);
//CommandBase.getInstance().run(v.get(commandNum));
}
});
}
for(int x = 0; x < threads.length; x++) {
threads[x].start();
}
for(int x = 0; x < threads.length; x++) {
while(threads[x].isAlive()) {
concurrentCommandsFinished = false;
}
}
concurrentCommandsFinished = true;
}catch(NullPointerException ex) {
}
}
private int lastThread = 0;
private boolean concurrentCommandsFinished = false;
private synchronized void waitForConcurrentCommands() throws InterruptedException {
while(!concurrentCommandsFinished) {
Thread.sleep(1);
}
}
public void enable() {
try {
this.interupted = false;
boolean adding = false;
Vector<CommandBase> concurrentCommands = new Vector<CommandBase>(1, 1);
for(int x = 0; x < commands.size(); x++) {
if(commandTypes.get(x).equals("Sequencial")) {
if(adding) {
runConcurrentCommands(concurrentCommands);
concurrentCommands = new Vector<CommandBase>(1, 1);
adding = false;
try {
waitForConcurrentCommands();
} catch (InterruptedException ex) {
}
}
System.out.println("Sequencial command "+x);
CommandBase.getInstance().run(commands.get(x));
}else if(commandTypes.get(x).equals("Concurrent")) {
adding = true;
concurrentCommands.add(commands.get(x));
}
}
if(adding) {
runConcurrentCommands(concurrentCommands);
adding = false;
}
}catch(NullPointerException ex) {
}
}
I created an array of threads - and did not run them until after they are all created. With this array, I needed a way to keep the variable of currentcommand in the state it was during creation of the object. That is why I have the
Quote:
|
public final int commandNum = currentCommands;
|
in there.
In terms of running it all, I included the rest of the current code that I have. There is a portion that waits for an array of concurrent commands to finish before doing another sequencial command. Basically, this has almost all of the features of the CommandGroup class in WPI, while not using Scheduler or any WPI libraries (So it can be ported).
Again, thank you Mr.Lim for your help
