Merge pull request #143 from jentfoo/concurrency_classpath_extension

Expanding avian's classpath for java.util.concurrent
This commit is contained in:
Joel Dice 2013-12-24 10:50:30 -08:00
commit ef3e3d749b
12 changed files with 752 additions and 2 deletions

View File

@ -0,0 +1,23 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public class CancellationException extends IllegalStateException {
private static final long serialVersionUID = -9202173006928992231L;
public CancellationException() {
super();
}
public CancellationException(String message) {
super(message);
}
}

View File

@ -22,7 +22,7 @@ public class ConcurrentLinkedQueue<T> {
}
}
private volatile Node<T> head = new Node(null, null);
private volatile Node<T> head = new Node<T>(null, null);
private volatile Node<T> tail = head;
public void clear() {
@ -31,7 +31,7 @@ public class ConcurrentLinkedQueue<T> {
}
public boolean add(T value) {
Node<T> n = new Node(value, null);
Node<T> n = new Node<T>(value, null);
while (true) {
Node<T> t = tail;
Node<T> next = tail.next;

View File

@ -0,0 +1,15 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public interface Delayed {
public long getDelay(TimeUnit unit);
}

View File

@ -0,0 +1,31 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public class ExecutionException extends Exception {
private static final long serialVersionUID = 7830266012832686185L;
protected ExecutionException() {
super();
}
protected ExecutionException(String message) {
super(message);
}
public ExecutionException(String message, Throwable cause) {
super(message, cause);
}
public ExecutionException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,15 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public interface Executor {
public void execute(Runnable task);
}

View File

@ -0,0 +1,39 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
import java.util.List;
import java.util.Collection;
public interface ExecutorService extends Executor {
public void shutdown();
public boolean isShutdown();
public boolean isTerminated();
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
public <T> Future<T> submit(Callable<T> task);
public <T> Future<T> submit(Runnable task, T result);
public Future<?> submit(Runnable task);
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit) throws InterruptedException;
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException;
public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
}

View File

@ -0,0 +1,25 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public interface Future<V> {
public boolean cancel(boolean mayInterruptIfRunning);
public boolean isCancelled();
public boolean isDone();
public V get() throws InterruptedException, ExecutionException;
public V get(long timeout, TimeUnit unit) throws InterruptedException,
ExecutionException,
TimeoutException;
}

View File

@ -0,0 +1,31 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public class RejectedExecutionException extends RuntimeException {
private static final long serialVersionUID = -375805702767069545L;
public RejectedExecutionException() {
super();
}
public RejectedExecutionException(String message) {
super(message);
}
public RejectedExecutionException(String message, Throwable cause) {
super(message, cause);
}
public RejectedExecutionException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,15 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public interface ScheduledFuture<V> extends Delayed, Future<V> {
}

View File

@ -0,0 +1,416 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public enum TimeUnit {
NANOSECONDS {
@Override
public long toNanos(long d) {
return d;
}
@Override
public long toMicros(long d) {
return d / NANOSECONDS_PER_MICROSECOND;
}
@Override
public long toMillis(long d) {
return d / NANOSECONDS_PER_MILLISECOND;
}
@Override
public long toSeconds(long d) {
return d / NANOSECONDS_PER_SECOND;
}
@Override
public long toMinutes(long d) {
return d / NANOSECONDS_PER_MINUTE;
}
@Override
public long toHours(long d) {
return d / NANOSECONDS_PER_HOUR;
}
@Override
public long toDays(long d) {
return d / NANOSECONDS_PER_DAY;
}
@Override
public long convert(long d, TimeUnit u) {
return u.toNanos(d);
}
@Override
int excessNanos(long d, long m) {
return (int) (d - (m * NANOSECONDS_PER_MILLISECOND));
}
},
MICROSECONDS {
@Override
public long toNanos(long d) {
return scale(d, NANOSECONDS_PER_MICROSECOND);
}
@Override
public long toMicros(long d) {
return d;
}
@Override
public long toMillis(long d) {
return d / MICROSECONDS_PER_MILLISECOND;
}
@Override
public long toSeconds(long d) {
return d / MICROSECONDS_PER_SECOND;
}
@Override
public long toMinutes(long d) {
return d / MICROSECONDS_PER_MINUTE;
}
@Override
public long toHours(long d) {
return d / MICROSECONDS_PER_HOUR;
}
@Override
public long toDays(long d) {
return d / MICROSECONDS_PER_DAY;
}
@Override
public long convert(long d, TimeUnit u) {
return u.toMicros(d);
}
@Override
int excessNanos(long d, long m) {
return (int) ((d * NANOSECONDS_PER_MICROSECOND) - (m * NANOSECONDS_PER_MILLISECOND));
}
},
MILLISECONDS {
@Override
public long toNanos(long d) {
return scale(d, NANOSECONDS_PER_MILLISECOND);
}
@Override
public long toMicros(long d) {
return scale(d, MICROSECONDS_PER_MILLISECOND);
}
@Override
public long toMillis(long d) {
return d;
}
@Override
public long toSeconds(long d) {
return d / MILLISECONDS_PER_SECOND;
}
@Override
public long toMinutes(long d) {
return d / MILLISECONDS_PER_MINUTE;
}
@Override
public long toHours(long d) {
return d / MILLISECONDS_PER_HOUR;
}
@Override
public long toDays(long d) {
return d / MILLISECONDS_PER_DAY;
}
@Override
public long convert(long d, TimeUnit u) {
return u.toMillis(d);
}
@Override
int excessNanos(long d, long m) {
return 0;
}
},
SECONDS {
@Override
public long toNanos(long d) {
return scale(d, NANOSECONDS_PER_SECOND);
}
@Override
public long toMicros(long d) {
return scale(d, MICROSECONDS_PER_SECOND);
}
@Override
public long toMillis(long d) {
return scale(d, MILLISECONDS_PER_SECOND);
}
@Override
public long toSeconds(long d) {
return d;
}
@Override
public long toMinutes(long d) {
return d / SECONDS_PER_MINUTE;
}
@Override
public long toHours(long d) {
return d / SECONDS_PER_HOUR;
}
@Override
public long toDays(long d) {
return d / SECONDS_PER_DAY;
}
@Override
public long convert(long d, TimeUnit u) {
return u.toSeconds(d);
}
@Override
int excessNanos(long d, long m) {
return 0;
}
},
MINUTES {
@Override
public long toNanos(long d) {
return scale(d, NANOSECONDS_PER_MINUTE);
}
@Override
public long toMicros(long d) {
return scale(d, MICROSECONDS_PER_MINUTE);
}
@Override
public long toMillis(long d) {
return scale(d, MILLISECONDS_PER_MINUTE);
}
@Override
public long toSeconds(long d) {
return scale(d, SECONDS_PER_MINUTE);
}
@Override
public long toMinutes(long d) {
return d;
}
@Override
public long toHours(long d) {
return d / MINUETS_PER_HOUR;
}
@Override
public long toDays(long d) {
return d / MINUETS_PER_DAY;
}
@Override
public long convert(long d, TimeUnit u) {
return u.toMinutes(d);
}
@Override
int excessNanos(long d, long m) {
return 0;
}
},
HOURS {
@Override
public long toNanos(long d) {
return scale(d, NANOSECONDS_PER_HOUR);
}
@Override
public long toMicros(long d) {
return scale(d, MICROSECONDS_PER_HOUR);
}
@Override
public long toMillis(long d) {
return scale(d, MILLISECONDS_PER_HOUR);
}
@Override
public long toSeconds(long d) {
return scale(d, SECONDS_PER_HOUR);
}
@Override
public long toMinutes(long d) {
return scale(d, MINUETS_PER_HOUR);
}
@Override
public long toHours(long d) {
return d;
}
@Override
public long toDays(long d) {
return d / HOURS_PER_DAY;
}
@Override
public long convert(long d, TimeUnit u) {
return u.toHours(d);
}
@Override
int excessNanos(long d, long m) {
return 0;
}
},
DAYS {
@Override
public long toNanos(long d) {
return scale(d, NANOSECONDS_PER_DAY);
}
@Override
public long toMicros(long d) {
return scale(d, MICROSECONDS_PER_DAY);
}
@Override
public long toMillis(long d) {
return scale(d, MILLISECONDS_PER_DAY);
}
@Override
public long toSeconds(long d) {
return scale(d, SECONDS_PER_DAY);
}
@Override
public long toMinutes(long d) {
return scale(d, MINUETS_PER_DAY);
}
@Override
public long toHours(long d) {
return scale(d, HOURS_PER_DAY);
}
@Override
public long toDays(long d) {
return d;
}
@Override
public long convert(long d, TimeUnit u) {
return u.toDays(d);
}
@Override
int excessNanos(long d, long m) {
return 0;
}
};
private static final long NANOSECONDS_PER_MICROSECOND = 1000L;
private static final long MICROSECONDS_PER_MILLISECOND = 1000L;
private static final long MILLISECONDS_PER_SECOND = 1000L;
private static final long SECONDS_PER_MINUTE = 60;
private static final long MINUETS_PER_HOUR = 60;
private static final long HOURS_PER_DAY = 24;
private static final long NANOSECONDS_PER_MILLISECOND = NANOSECONDS_PER_MICROSECOND * MICROSECONDS_PER_MILLISECOND;
private static final long NANOSECONDS_PER_SECOND = NANOSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND;
private static final long NANOSECONDS_PER_MINUTE = NANOSECONDS_PER_SECOND * SECONDS_PER_MINUTE;
private static final long NANOSECONDS_PER_HOUR = NANOSECONDS_PER_MINUTE * MINUETS_PER_HOUR;
private static final long NANOSECONDS_PER_DAY = NANOSECONDS_PER_HOUR * HOURS_PER_DAY;
private static final long MICROSECONDS_PER_SECOND = MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND;
private static final long MICROSECONDS_PER_MINUTE = MICROSECONDS_PER_SECOND * SECONDS_PER_MINUTE;
private static final long MICROSECONDS_PER_HOUR = MICROSECONDS_PER_MINUTE * MINUETS_PER_HOUR;
private static final long MICROSECONDS_PER_DAY = MICROSECONDS_PER_HOUR * HOURS_PER_DAY;
private static final long MILLISECONDS_PER_MINUTE = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
private static final long MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * MINUETS_PER_HOUR;
private static final long MILLISECONDS_PER_DAY = MILLISECONDS_PER_HOUR * HOURS_PER_DAY;
private static final long SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUETS_PER_HOUR;
private static final long SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
private static final long MINUETS_PER_DAY = MINUETS_PER_HOUR * HOURS_PER_DAY;
private static long scale(long value, long conversion) {
long result = value * conversion;
if (value > 0 && result < value) {
return Long.MAX_VALUE;
} else if (value < 0 && result > value) {
return Long.MIN_VALUE;
} else {
return result;
}
}
public abstract long convert(long sourceDuration, TimeUnit sourceUnit);
public abstract long toNanos(long duration);
public abstract long toMicros(long duration);
public abstract long toMillis(long duration);
public abstract long toSeconds(long duration);
public abstract long toMinutes(long duration);
public abstract long toHours(long duration);
public abstract long toDays(long duration);
abstract int excessNanos(long d, long m);
public void timedWait(Object obj, long timeout) throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
int ns = excessNanos(timeout, ms);
obj.wait(ms, ns);
}
}
public void timedJoin(Thread thread, long timeout) throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
int ns = excessNanos(timeout, ms);
thread.join(ms, ns);
}
}
public void sleep(long timeout) throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
int ns = excessNanos(timeout, ms);
Thread.sleep(ms, ns);
}
}
}

View File

@ -0,0 +1,23 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
public class TimeoutException extends Exception {
private static final long serialVersionUID = 1900926677490660714L;
public TimeoutException() {
super();
}
public TimeoutException(String message) {
super(message);
}
}

View File

@ -0,0 +1,117 @@
import java.util.concurrent.TimeUnit;
public class TimeUnitConversions {
private static void expect(long v1, long v2) {
if (v1 != v2) {
throw new RuntimeException(v1 + " != " + v2);
}
}
private static void toNanoConversionTest() {
long expectedValue = 1;
expect(TimeUnit.NANOSECONDS.convert(1, TimeUnit.NANOSECONDS), expectedValue);
expectedValue *= 1000;
expect(TimeUnit.NANOSECONDS.convert(1, TimeUnit.MICROSECONDS), expectedValue);
expectedValue *= 1000;
expect(TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS), expectedValue);
expectedValue *= 1000;
expect(TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS), expectedValue);
expectedValue *= 60;
expect(TimeUnit.NANOSECONDS.convert(1, TimeUnit.MINUTES), expectedValue);
expectedValue *= 60;
expect(TimeUnit.NANOSECONDS.convert(1, TimeUnit.HOURS), expectedValue);
expectedValue *= 24;
expect(TimeUnit.NANOSECONDS.convert(1, TimeUnit.DAYS), expectedValue);
}
private static void toMicroConversionTest() {
long expectedValue = 1;
expect(TimeUnit.MICROSECONDS.convert(1000, TimeUnit.NANOSECONDS), expectedValue);
expect(TimeUnit.MICROSECONDS.convert(1, TimeUnit.MICROSECONDS), expectedValue);
expectedValue *= 1000;
expect(TimeUnit.MICROSECONDS.convert(1, TimeUnit.MILLISECONDS), expectedValue);
expectedValue *= 1000;
expect(TimeUnit.MICROSECONDS.convert(1, TimeUnit.SECONDS), expectedValue);
expectedValue *= 60;
expect(TimeUnit.MICROSECONDS.convert(1, TimeUnit.MINUTES), expectedValue);
expectedValue *= 60;
expect(TimeUnit.MICROSECONDS.convert(1, TimeUnit.HOURS), expectedValue);
expectedValue *= 24;
expect(TimeUnit.MICROSECONDS.convert(1, TimeUnit.DAYS), expectedValue);
}
private static void toMilliConversionTest() {
long expectedValue = 1;
expect(TimeUnit.MILLISECONDS.convert(1000L * 1000, TimeUnit.NANOSECONDS), expectedValue);
expect(TimeUnit.MILLISECONDS.convert(1000, TimeUnit.MICROSECONDS), expectedValue);
expect(TimeUnit.MILLISECONDS.convert(1, TimeUnit.MILLISECONDS), expectedValue);
expectedValue *= 1000;
expect(TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS), expectedValue);
expectedValue *= 60;
expect(TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES), expectedValue);
expectedValue *= 60;
expect(TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS), expectedValue);
expectedValue *= 24;
expect(TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS), expectedValue);
}
private static void toSecondConversionTest() {
long expectedValue = 1;
expect(TimeUnit.SECONDS.convert(1000L * 1000 * 1000, TimeUnit.NANOSECONDS), expectedValue);
expect(TimeUnit.SECONDS.convert(1000L * 1000, TimeUnit.MICROSECONDS), expectedValue);
expect(TimeUnit.SECONDS.convert(1000, TimeUnit.MILLISECONDS), expectedValue);
expect(TimeUnit.SECONDS.convert(1, TimeUnit.SECONDS), expectedValue);
expectedValue *= 60;
expect(TimeUnit.SECONDS.convert(1, TimeUnit.MINUTES), expectedValue);
expectedValue *= 60;
expect(TimeUnit.SECONDS.convert(1, TimeUnit.HOURS), expectedValue);
expectedValue *= 24;
expect(TimeUnit.SECONDS.convert(1, TimeUnit.DAYS), expectedValue);
}
private static void toMinuteConversionTest() {
long expectedValue = 1;
expect(TimeUnit.MINUTES.convert(1000L * 1000 * 1000 * 60, TimeUnit.NANOSECONDS), expectedValue);
expect(TimeUnit.MINUTES.convert(1000L * 1000 * 60, TimeUnit.MICROSECONDS), expectedValue);
expect(TimeUnit.MINUTES.convert(1000L * 60, TimeUnit.MILLISECONDS), expectedValue);
expect(TimeUnit.MINUTES.convert(60, TimeUnit.SECONDS), expectedValue);
expect(TimeUnit.MINUTES.convert(1, TimeUnit.MINUTES), expectedValue);
expectedValue *= 60;
expect(TimeUnit.MINUTES.convert(1, TimeUnit.HOURS), expectedValue);
expectedValue *= 24;
expect(TimeUnit.MINUTES.convert(1, TimeUnit.DAYS), expectedValue);
}
private static void toHourConversionTest() {
long expectedValue = 1;
expect(TimeUnit.HOURS.convert(1000L * 1000 * 1000 * 60 * 60, TimeUnit.NANOSECONDS), expectedValue);
expect(TimeUnit.HOURS.convert(1000L * 1000 * 60 * 60, TimeUnit.MICROSECONDS), expectedValue);
expect(TimeUnit.HOURS.convert(1000L * 60 * 60, TimeUnit.MILLISECONDS), expectedValue);
expect(TimeUnit.HOURS.convert(60L * 60, TimeUnit.SECONDS), expectedValue);
expect(TimeUnit.HOURS.convert(60, TimeUnit.MINUTES), expectedValue);
expect(TimeUnit.HOURS.convert(1, TimeUnit.HOURS), expectedValue);
expectedValue *= 24;
expect(TimeUnit.HOURS.convert(1, TimeUnit.DAYS), expectedValue);
}
private static void toDayConversionTest() {
long expectedValue = 1;
expect(TimeUnit.DAYS.convert(1000L * 1000 * 1000 * 60 * 60 * 24, TimeUnit.NANOSECONDS), expectedValue);
expect(TimeUnit.DAYS.convert(1000L * 1000 * 60 * 60 * 24, TimeUnit.MICROSECONDS), expectedValue);
expect(TimeUnit.DAYS.convert(1000L * 60 * 60 * 24, TimeUnit.MILLISECONDS), expectedValue);
expect(TimeUnit.DAYS.convert(60L * 60 * 24, TimeUnit.SECONDS), expectedValue);
expect(TimeUnit.DAYS.convert(60L * 24, TimeUnit.MINUTES), expectedValue);
expect(TimeUnit.DAYS.convert(24, TimeUnit.HOURS), expectedValue);
expect(TimeUnit.DAYS.convert(1, TimeUnit.DAYS), expectedValue);
}
public static void main(String[] args) {
toNanoConversionTest();
toMicroConversionTest();
toMilliConversionTest();
toSecondConversionTest();
toMinuteConversionTest();
toHourConversionTest();
toDayConversionTest();
}
}