
|
If you were logged in you would be able to see more operations.
|
|
|
|
Consider the following code. It will crash
public function test():void {
var group:ConcurrentTaskGroup = new ConcurrentTaskGroup();
group.addTask(new CommandTask(new Command(first, [group])));
group.start();
}
public function first(group:TaskGroup):void {
group.addTask(new CommandTask(new Command(second)));
}
public function second():void {}
The error occurs since the loop in ConcurrentTaskGroup.doStart does not care if tasks have been created as a result of former tasks so that the allTasks collection changes. Thus, my recommended ConcurrentTask.doStart function looks like:
protected override function doStart () : void {
if (allTasks.isEmpty()) {
complete();
return;
}
var beginTasks:Array = new Array();
for (var i : Number = 0; i < allTasks.getSize(); i++)
beginTasks.push(allTasks.get(i));
for each(var t:Task in beginTasks)
startTask(t);
}
|
|
Description
|
Consider the following code. It will crash
public function test():void {
var group:ConcurrentTaskGroup = new ConcurrentTaskGroup();
group.addTask(new CommandTask(new Command(first, [group])));
group.start();
}
public function first(group:TaskGroup):void {
group.addTask(new CommandTask(new Command(second)));
}
public function second():void {}
The error occurs since the loop in ConcurrentTaskGroup.doStart does not care if tasks have been created as a result of former tasks so that the allTasks collection changes. Thus, my recommended ConcurrentTask.doStart function looks like:
protected override function doStart () : void {
if (allTasks.isEmpty()) {
complete();
return;
}
var beginTasks:Array = new Array();
for (var i : Number = 0; i < allTasks.getSize(); i++)
beginTasks.push(allTasks.get(i));
for each(var t:Task in beginTasks)
startTask(t);
} |
Show » |
|
Add two tasks to a concurrent task group that completes immediatly (in their doStart). Then the handleTaskComplete function will check the condition
activeTasks.isEmpty() && state == TaskState.ACTIVE
when the first task completes, and the statement above will be true since the second task hasn't been added to the activeTasks collection yet and thus the task group will complete, which is erronous.
Thus my new ConcurrentTaskGroup.doStart looks like:
protected override function doStart () : void {
if (allTasks.isEmpty()) {
complete();
return;
}
var beginTasks:Array = new Array();
for (var i : Number = 0; i < allTasks.getSize(); i++)
{
activeTasks.append(allTasks.get(i));
beginTasks.push(allTasks.get(i));
}
for each(var t:Task in beginTasks)
startTask(t);
}