borrow AsyncTask stuff from libgdx

- for html backend async tasks are just put on Queue and run on next main-loop iteration
This commit is contained in:
Hannes Janetzek
2013-07-26 23:35:05 +02:00
parent 8787ac7b48
commit 5b16f6b085
11 changed files with 422 additions and 26 deletions

View File

@@ -0,0 +1,60 @@
package org.oscim.utils.async;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.Timer;
/**
* GWT emulation of AsynchExecutor, will call tasks immediately :D
* @author badlogic
*
*/
public class AsyncExecutor implements Disposable {
/**
* Creates a new AsynchExecutor that allows maxConcurrent
* {@link Runnable} instances to run in parallel.
* @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("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;
}
return new AsyncResult(result);
}
/**
* 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
*/
public void post(Runnable task) {
Gdx.app.postRunnable(task);
}
/**
* Waits for running {@link AsyncTask} instances to finish,
* then destroys any resources like threads. Can not be used
* after this method is called.
*/
@Override
public void dispose () {
}
}

View File

@@ -0,0 +1,49 @@
/*******************************************************************************
* 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

@@ -0,0 +1,28 @@
/*******************************************************************************
* 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

@@ -0,0 +1,27 @@
/*******************************************************************************
* 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() {
}
}