Skip to content

Instantly share code, notes, and snippets.

@mxro
Created May 26, 2012 01:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mxro/2791639 to your computer and use it in GitHub Desktop.
Save mxro/2791639 to your computer and use it in GitHub Desktop.
Default Implementation of Concurrency API in GWT
package any;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import one.utils.concurrent.CollectionFactory;
import one.utils.concurrent.Concurrency;
import one.utils.concurrent.ExecutorFactory;
import one.utils.concurrent.OneExecutor;
import one.utils.concurrent.OneLock;
import one.utils.concurrent.OneTimer;
import one.utils.concurrent.TimerFactory;
import com.google.gwt.user.client.Timer;
/**
* Please note: this file will only compile if the GWT User API is in the classpath.
*/
public final class GwtConcurrency implements Concurrency {
@Override
public TimerFactory newTimer() {
return new TimerFactory() {
@Override
public OneTimer scheduleOnce(final int when, final Runnable runnable) {
final Timer timer = new Timer() {
@Override
public void run() {
runnable.run();
}
};
timer.schedule(when);
return new OneTimer() {
@Override
public void stop() {
timer.cancel();
}
};
}
@Override
public OneTimer scheduleRepeating(final int offsetInMs,
final int intervallInMs, final Runnable runnable) {
final Timer timer = new Timer() {
@Override
public void run() {
runnable.run();
}
};
timer.scheduleRepeating(intervallInMs);
return new OneTimer() {
@Override
public void stop() {
timer.cancel();
}
};
}
};
}
@Override
public ExecutorFactory newExecutor() {
return new ExecutorFactory() {
@Override
public OneExecutor newSingleThreadExecutor(final Object owner) {
return new OneExecutor() {
@Override
public void shutdown(final WhenExecutorShutDown callback) {
callback.thenDo();
}
@Override
public void execute(final Runnable runnable) {
newTimer().scheduleOnce(1, runnable);
}
};
}
@Override
public OneExecutor newParallelExecutor(
final int maxParallelThreads, final Object owner) {
return newSingleThreadExecutor(owner);
}
@Override
public OneExecutor newImmideateExecutor() {
return new OneExecutor() {
@Override
public void shutdown(final WhenExecutorShutDown callback) {
callback.thenDo();
}
@Override
public void execute(final Runnable runnable) {
runnable.run();
}
};
}
};
}
@Override
public void runLater(final Runnable runnable) {
}
@Override
public OneLock newLock() {
return new OneLock() {
@Override
public void lock() {
}
@Override
public void unlock() {
}
@Override
public boolean isHeldByCurrentThread() {
return true;
}
};
}
@Override
public CollectionFactory newCollection() {
return new CollectionFactory() {
@Override
public <GPType> Queue<GPType> newThreadSafeQueue(
final Class<GPType> itemType) {
return new LinkedList<GPType>();
}
@Override
public <KeyType, ValueType> Map<KeyType, ValueType> newThreadSafeMap(
final Class<KeyType> keyType,
final Class<ValueType> valueType) {
return new HashMap<KeyType, ValueType>();
}
@Override
public <ItemType> List<ItemType> newThreadSafeList(
final Class<ItemType> itemType) {
return new ArrayList<ItemType>();
}
@Override
public <ItemType> Set<ItemType> newThreadSafeSet(
final Class<ItemType> itemType) {
return new HashSet<ItemType>();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment