add TaskQueue interface to Map

- change AsyncTask to be able to post on main-thread
- rename Task.run() -> go()
This commit is contained in:
Hannes Janetzek
2014-04-20 00:24:26 +02:00
parent c161ea9cca
commit 64383a8406
10 changed files with 117 additions and 246 deletions

View File

@@ -10,6 +10,7 @@ import com.badlogic.gdx.utils.Disposable;
*
*/
public class AsyncExecutor implements Disposable {
private final TaskQueue mainloop;
/**
* Creates a new AsynchExecutor that allows maxConcurrent {@link Runnable}
@@ -17,32 +18,8 @@ public class AsyncExecutor implements Disposable {
*
* @param maxConcurrent
*/
public AsyncExecutor(int maxConcurrent) {
}
// FIXME TODO add wrap into 'FakeFuture' and run via Gdx.app.post()
/**
* Submits a {@link Runnable} to be executed asynchronously. If
* maxConcurrent runnables are already running, the runnable
* will be queued.
*
* @param task the task to execute asynchronously
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> AsyncResult<T> submit(final AsyncTask<T> task) {
T result = null;
boolean error = false;
try {
task.run();
result = task.getResult();
} catch (Throwable t) {
error = true;
}
if (error)
return null;
return new AsyncResult(result);
public AsyncExecutor(int maxConcurrent, TaskQueue mainloop) {
this.mainloop = mainloop;
}
/**
@@ -53,7 +30,12 @@ public class AsyncExecutor implements Disposable {
* @param task the task to execute asynchronously
*/
public boolean post(Runnable task) {
if (task instanceof AsyncTask) {
((AsyncTask) task).setTaskQueue(mainloop);
}
Gdx.app.postRunnable(task);
return true;
}

View File

@@ -1,50 +0,0 @@
/*******************************************************************************
* Copyright 2013 See libgdx AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.oscim.utils.async;
//import java.util.concurrent.ExecutionException;
//import java.util.concurrent.Future;
/**
* Returned by {@link AsyncExecutor#submit(AsyncTask)}, allows to poll
* for the result of the asynch workload.
*
* @author badlogic
*
*/
public class AsyncResult<T> {
private final T result;
AsyncResult(T result) {
this.result = result;
}
/**
* @return whether the {@link AsyncTask} is done
*/
public boolean isDone() {
return true;
}
/**
* @return the result, or null if there was an error, no result, or the task
* is still running
*/
public T get() {
return result;
}
}

View File

@@ -1,30 +0,0 @@
/*******************************************************************************
* Copyright 2013 See libgdx AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.oscim.utils.async;
/**
* Task to be submitted to an {@link AsyncExecutor}, returning a result of type
* T.
*
* @author badlogic
*
*/
public interface AsyncTask<T> extends Runnable {
public boolean cancel();
public T getResult() throws Exception;
}

View File

@@ -1,28 +0,0 @@
/*******************************************************************************
* Copyright 2013 See libgdx AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.oscim.utils.async;
/**
* GWT emulation of ThreadUtils, does nothing.
*
* @author badlogic
*
*/
public class ThreadUtils {
public static void yield() {
}
}