![]() |
Concurrency and multiple commands
Code:
private int currentCommand;Code:
run: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. |
Re: Concurrency and multiple commands
Concurrency.
Your for loop is completing and incrementing currentCommand before your threads are actually starting. Despite calling thread.start() - they don't actually have to start "right away." This is why you also never see "Running = 0". When your 1st spawned thread finally runs, the for loop has already completed, and currentCommand was incremented to 1 before the thread even ran... A hack fix would be to add a thread.join(???) at the end of your for loop (after thread.start(), where ??? is just long enough to start your Command. Not pretty, but it'll likely work. Even better, pass currentCommand as a parameter to your threads... something like: http://stackoverflow.com/questions/8...-a-java-thread Good luck! |
Re: Concurrency and multiple commands
Also, WPILib has support for concurrent Commands using CommandGroups.
It's documented in the WPILib cookbook. Search for the AddParallel and AddSequential methods. It's possible you might be trying to re-invent something that already works pretty well! |
Re: Concurrency and multiple commands
Quote:
Quote:
My biggest issue is that when I tried to do this : Code:
private void runConcurrentCommands(final Vector<CommandBase> v) {To me, that would eliminate the problem by only incrementing the commandCount AFTER the command has run (And not incrementing it beforehand on the first run). |
Re: Concurrency and multiple commands
Sorry about that obvious bug in the code up there. I added a loop to go through the commands, but still no result.
|
Re: Concurrency and multiple commands
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;Quote:
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 :) |
| All times are GMT -5. The time now is 10:12. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi