Added the easy to add interfaces and implementations for java.util.concurrent to pave the way for future expansion of avians java.util.concurrent classpath implementation.

This commit is contained in:
Mike Jensen 2013-12-23 14:19:41 -07:00
parent 4570195a9b
commit 8b7f689e1a
11 changed files with 635 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_MINUET;
}
@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_MINUET;
}
@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_MINUET;
}
@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_MINUET;
}
@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_MINUET);
}
@Override
public long toMicros(long d) {
return scale(d, MICROSECONDS_PER_MINUET);
}
@Override
public long toMillis(long d) {
return scale(d, MILLISECONDS_PER_MINUET);
}
@Override
public long toSeconds(long d) {
return scale(d, SECONDS_PER_MINUET);
}
@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_MINUET = 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_MINUET = NANOSECONDS_PER_SECOND * SECONDS_PER_MINUET;
private static final long NANOSECONDS_PER_HOUR = NANOSECONDS_PER_MINUET * 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_MINUET = MICROSECONDS_PER_SECOND * SECONDS_PER_MINUET;
private static final long MICROSECONDS_PER_HOUR = MICROSECONDS_PER_MINUET * MINUETS_PER_HOUR;
private static final long MICROSECONDS_PER_DAY = MICROSECONDS_PER_HOUR * HOURS_PER_DAY;
private static final long MILLISECONDS_PER_MINUET = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUET;
private static final long MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUET * 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_MINUET * 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);
}
}