Author: desruisseaux
Date: Sun Jan 6 16:19:53 2013
New Revision: 1429550
URL: http://svn.apache.org/viewvc?rev=1429550&view=rev
Log:
Merge from the JDK6 branch.
Added:
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/DelayedExecutor.java
- copied unchanged from r1429549,
sis/branches/JDK6/sis-utility/src/main/java/org/apache/sis/internal/util/DelayedExecutor.java
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/DelayedRunnable.java
- copied unchanged from r1429549,
sis/branches/JDK6/sis-utility/src/main/java/org/apache/sis/internal/util/DelayedRunnable.java
Removed:
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/Executors.java
Modified:
sis/trunk/ (props changed)
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/DaemonThread.java
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/Threads.java
sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/Cache.java
sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java
sis/trunk/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
Propchange: sis/trunk/
------------------------------------------------------------------------------
Merged /sis/branches/JDK7:r1429461-1429547
Merged /sis/branches/JDK6:r1429462-1429549
Modified:
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/DaemonThread.java
URL:
http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/DaemonThread.java?rev=1429550&r1=1429549&r2=1429550&view=diff
==============================================================================
---
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/DaemonThread.java
(original)
+++
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/DaemonThread.java
Sun Jan 6 16:19:53 2013
@@ -47,9 +47,8 @@ abstract class DaemonThread extends Thre
/**
* The previous element in a chain of {@code DaemonThread}s. We maintain a
linked list of
* {@code DaemonThread} to be killed when {@link #killAll(DaemonThread)}
will be invoked.
- * We do not rely on the thread listed by the {@link
Threads#RESOURCE_DISPOSERS} group
- * because in an OSGi context, we need to handle separately the threads
created by each
- * SIS module.
+ * We do not rely on the thread listed by the {@link Threads#DAEMONS}
group because in an
+ * OSGi context, we need to handle separately the threads created by each
SIS module.
*/
private final DaemonThread previous;
@@ -76,20 +75,19 @@ abstract class DaemonThread extends Thre
* static {
* synchronized (MyInternalClass.class) {
* MyInternalClass.lastCreatedDaemon = myDaemonThread =
new MyDaemonThread(
- * Threads.RESOURCE_DISPOSERS, "MyThread",
MyInternalClass.lastCreatedDaemon);
+ * "MyThread", MyInternalClass.lastCreatedDaemon);
* }
* }
* }
*
* See {@link ReferenceQueueConsumer} for a real example.
*
- * @param group The thread group.
- * @param name The thread name.
+ * @param name The thread name.
* @param lastCreatedDaemon The previous element in a chain of {@code
DaemonThread}s,
* or {@code null}. Each SIS module shall maintain its own chain,
if any.
*/
- protected DaemonThread(final ThreadGroup group, final String name, final
DaemonThread lastCreatedDaemon) {
- super(group, name);
+ protected DaemonThread(final String name, final DaemonThread
lastCreatedDaemon) {
+ super(Threads.DAEMONS, name);
previous = lastCreatedDaemon;
setDaemon(true);
}
Modified:
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java
URL:
http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java?rev=1429550&r1=1429549&r2=1429550&view=diff
==============================================================================
---
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java
(original)
+++
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java
Sun Jan 6 16:19:53 2013
@@ -18,7 +18,6 @@ package org.apache.sis.internal.util;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
-
import org.apache.sis.util.Disposable;
import org.apache.sis.util.logging.Logging;
@@ -35,8 +34,7 @@ import org.apache.sis.util.logging.Loggi
* {@preformat java
* final class MyReference extends WeakReference<MyType> implements
Disposable {
* MyReference(MyType referent) {
- * super(referent, ReferenceQueueConsumer.DEFAULT.queue);
- * assert ReferenceQueueConsumer.DEFAULT.isAlive();
+ * super(referent, ReferenceQueueConsumer.QUEUE);
* }
*
* @Override
@@ -47,38 +45,36 @@ import org.apache.sis.util.logging.Loggi
* }
* }
*
- * @param <T> The type of objects being referenced.
- *
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.00)
* @version 0.3
* @module
*/
-public final class ReferenceQueueConsumer<T> extends DaemonThread {
+public final class ReferenceQueueConsumer extends DaemonThread {
+ /**
+ * List of references collected by the garbage collector. This reference
shall be given to
+ * {@link Reference} constructors as documented in the class javadoc.
Those {@code Reference}
+ * sub-classes <strong>must</strong> implement the {@link Disposable}
interface.
+ */
+ public static final ReferenceQueue<Object> QUEUE = new
ReferenceQueue<Object>();
+
/**
- * The singleton instance of the {@code ReferenceQueueConsumer} thread.
+ * Creates the singleton instance of the {@code ReferenceQueueConsumer}
thread.
*/
- public static final ReferenceQueueConsumer<Object> DEFAULT;
static {
synchronized (Threads.class) {
- Threads.lastCreatedDaemon = DEFAULT = new
ReferenceQueueConsumer<Object>(Threads.lastCreatedDaemon);
+ final ReferenceQueueConsumer thread;
+ Threads.lastCreatedDaemon = thread = new
ReferenceQueueConsumer(Threads.lastCreatedDaemon);
+ // Call to Thread.start() must be outside the constructor
+ // (Reference: Goetz et al.: "Java Concurrency in Practice").
+ thread.start();
}
if (Supervisor.ENABLED) {
Supervisor.register();
}
- // Call to Thread.start() must be outside the constructor
- // (Reference: Goetz et al.: "Java Concurrency in Practice").
- DEFAULT.start();
}
/**
- * List of references collected by the garbage collector. This reference
shall be given to
- * {@link Reference} constructors as documented in the class javadoc.
Those {@code Reference}
- * sub-classes <strong>must</strong> implement the {@link Disposable}
interface.
- */
- public final ReferenceQueue<T> queue = new ReferenceQueue<T>();
-
- /**
* Constructs a new thread as a daemon thread. This thread will be
sleeping most of the time.
* It will run only only a few nanoseconds every time a new {@link
Reference} is enqueued.
*
@@ -87,7 +83,7 @@ public final class ReferenceQueueConsume
* the benefit of the rest of the system, since they make more
resources available sooner.}
*/
private ReferenceQueueConsumer(final DaemonThread lastCreatedDaemon) {
- super(Threads.DAEMONS, "ReferenceQueueConsumer", lastCreatedDaemon);
+ super("ReferenceQueueConsumer", lastCreatedDaemon);
setPriority(Thread.MAX_PRIORITY - 2);
}
@@ -102,8 +98,8 @@ public final class ReferenceQueueConsume
* observed at shutdown time. If the field become null, assume that a
shutdown is
* under way and let the thread terminate.
*/
- ReferenceQueue<T> queue;
- while ((queue = this.queue) != null) {
+ ReferenceQueue<Object> queue;
+ while ((queue = QUEUE) != null) {
try {
/*
* Block until a reference is enqueued. The reference should
never be null
@@ -112,7 +108,7 @@ public final class ReferenceQueueConsume
* may be in the middle of a shutdown. Continue anyway as long
as we didn't
* received the kill event.
*/
- final Reference<? extends T> ref = queue.remove();
+ final Reference<?> ref = queue.remove();
if (ref != null) {
/*
* If the reference does not implement the Disposeable
interface, we want
Modified:
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/Threads.java
URL:
http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/Threads.java?rev=1429550&r1=1429549&r2=1429550&view=diff
==============================================================================
---
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/Threads.java
(original)
+++
sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/Threads.java
Sun Jan 6 16:19:53 2013
@@ -29,9 +29,9 @@ import org.apache.sis.util.logging.Loggi
* threads created by SIS together under the same parent tree node.
*
* {@section Note on dependencies}
- * This class shall not depend on {@link Executors} or {@link
ReferenceQueueConsumer}, because
- * initialization of those classes create new threads or threaded executor.
But it is okay to
- * have dependencies the other way around.
+ * This class shall not depend on {@link ReferenceQueueConsumer} or {@link
DelayedExecutor},
+ * because initialization of those classes create new threads. However it is
okay to have
+ * dependencies the other way around.
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.03)
@@ -84,11 +84,17 @@ final class Threads extends Static {
static DaemonThread lastCreatedDaemon;
/**
- * Executor to shutdown. This is a copy of the {@link
Executors#DAEMON_TASKS} field,
- * copied here only when the {@link Executors} class is loaded and
initialized. We
- * do that way for avoiding dependency from {@code Threads} to {@code
Executors}.
+ * Executor to shutdown. This is a copy of the {@code <removed class>}
executor static final
+ * field, copied here only when the {@code <removed class>} class is
loaded and initialized.
+ * We proceed that way for avoiding dependency from {@code Threads} to
{@code <removed class>}.
+ *
+ * <p>This field has been temporarily fixed to {@code null} since we
removed executor as of
+ * <a href="https://issues.apache.org/jira/browse/SIS-76">SIS-76</a>.
However we may revert
+ * to a modifiable field in a future version if we choose to use executor
again. In the main
+ * time, we declare this field as {@code final} for allowing the Javac
compiler to omit all
+ * compiled code inside {@code if (executor != null)} block.</p>
*/
- static ExecutorService executor;
+ private static final ExecutorService executor = null;
/**
* Do not allows instantiation of this class.
Modified:
sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/Cache.java
URL:
http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/Cache.java?rev=1429550&r1=1429549&r2=1429550&view=diff
==============================================================================
---
sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/Cache.java
(original)
+++
sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/Cache.java
Sun Jan 6 16:19:53 2013
@@ -30,13 +30,13 @@ import java.lang.ref.WeakReference;
import java.lang.ref.SoftReference;
import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;
-
import org.apache.sis.util.Disposable;
import org.apache.sis.util.ArgumentChecks;
import org.apache.sis.util.resources.Errors;
+import org.apache.sis.internal.util.DelayedRunnable;
import org.apache.sis.internal.util.ReferenceQueueConsumer;
-import static org.apache.sis.internal.util.Executors.executeDaemonTask;
+import static org.apache.sis.internal.util.DelayedExecutor.executeDaemonTask;
/**
@@ -402,7 +402,7 @@ public class Cache<K,V> extends Abstract
* looks for older strong references to replace by weak references so that
the total cost
* stay below the cost limit.
*/
- private final class Strong implements Runnable {
+ private final class Strong extends DelayedRunnable.Immediate {
private final K key;
private final V value;
@@ -440,7 +440,7 @@ public class Cache<K,V> extends Abstract
*/
public Handler<V> lock(final K key) {
final Work handler = new Work(key);
- handler.lock();
+ handler.lock.lock();
Object value;
try {
do {
@@ -456,7 +456,7 @@ public class Cache<K,V> extends Abstract
* values). We are done. But before to leave, lock again
for canceling the
* effect of unlock in the finally clause (we want the
lock to still active).
*/
- handler.lock();
+ handler.lock.lock();
return handler;
}
/*
@@ -489,13 +489,13 @@ public class Cache<K,V> extends Abstract
* handler.
*/
if (map.replace(key, ref, handler)) {
- handler.lock();
+ handler.lock.lock();
return handler;
}
// The map content changed. Try again.
} while (true);
} finally {
- handler.unlock();
+ handler.lock.unlock();
}
/*
* From this point, we abandon our handler.
@@ -510,7 +510,7 @@ public class Cache<K,V> extends Abstract
*/
@SuppressWarnings("unchecked")
final Work work = (Work) value;
- if (work.isHeldByCurrentThread()) {
+ if (work.lock.isHeldByCurrentThread()) {
throw new
IllegalStateException(Errors.format(Errors.Keys.RecursiveCreateCallForKey_1,
key));
}
return work.new Wait();
@@ -622,8 +622,12 @@ public class Cache<K,V> extends Abstract
* A handler implementation used for telling to other threads that the
current thread is
* computing a value.
*/
- @SuppressWarnings("serial") // Actually not intended to be serialized.
- final class Work extends ReentrantLock implements Handler<V>, Runnable {
+ final class Work extends DelayedRunnable.Immediate implements Handler<V> {
+ /**
+ * The synchronization lock.
+ */
+ final ReentrantLock lock;
+
/**
* The key to use for storing the result in the map.
*/
@@ -639,6 +643,7 @@ public class Cache<K,V> extends Abstract
* Creates a new handler which will store the result in the given map
at the given key.
*/
Work(final K key) {
+ lock = new ReentrantLock();
this.key = key;
}
@@ -647,13 +652,13 @@ public class Cache<K,V> extends Abstract
* method should be invoked only from an other thread than the one
doing the calculation.
*/
final V get() {
- if (isHeldByCurrentThread()) {
+ if (lock.isHeldByCurrentThread()) {
throw new IllegalStateException();
}
final V v;
- lock();
+ lock.lock();
v = value;
- unlock();
+ lock.unlock();
return v;
}
@@ -688,7 +693,7 @@ public class Cache<K,V> extends Abstract
done = map.remove(key, this);
}
} finally {
- unlock();
+ lock.unlock();
}
if (done) {
executeDaemonTask(this);
@@ -790,7 +795,7 @@ public class Cache<K,V> extends Abstract
/** Creates a references to be stored in the given map under the given
key. */
Soft(final ConcurrentMap<K,Object> map, final K key, final V value) {
- super(value, ReferenceQueueConsumer.DEFAULT.queue);
+ super(value, ReferenceQueueConsumer.QUEUE);
this.map = map;
this.key = key;
}
@@ -813,7 +818,7 @@ public class Cache<K,V> extends Abstract
/** Creates a references to be stored in the given map under the given
key. */
Weak(final ConcurrentMap<K,Object> map, final K key, final V value) {
- super(value, ReferenceQueueConsumer.DEFAULT.queue);
+ super(value, ReferenceQueueConsumer.QUEUE);
this.map = map;
this.key = key;
}
Modified:
sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java
URL:
http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java?rev=1429550&r1=1429549&r2=1429550&view=diff
==============================================================================
---
sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java
(original)
+++
sis/trunk/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java
Sun Jan 6 16:19:53 2013
@@ -80,8 +80,7 @@ abstract class WeakEntry<E> extends Weak
* Constructs a new weak reference.
*/
WeakEntry(final E obj, final WeakEntry<E> next, final int hash) {
- super(obj, ReferenceQueueConsumer.DEFAULT.queue);
- assert ReferenceQueueConsumer.DEFAULT.isAlive();
+ super(obj, ReferenceQueueConsumer.QUEUE);
this.next = next;
this.hash = hash;
}
Modified:
sis/trunk/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
URL:
http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java?rev=1429550&r1=1429549&r2=1429550&view=diff
==============================================================================
---
sis/trunk/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
(original)
+++
sis/trunk/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
Sun Jan 6 16:19:53 2013
@@ -273,7 +273,7 @@ public final strictfp class CacheTest ex
TestUtilities.printSeparator("CacheTest.stress() - testing
concurrent accesses");
out.print("There is "); out.print(threads.length); out.print("
threads, each of them"
+ " fetching or creating "); out.print(count);
out.println(" values.");
- out.println("Number of times a cached value has been reused, for
each thread:");
+ out.println("Number of times a new value has been created, for
each thread:");
for (int i=0; i<threads.length;) {
final String n = String.valueOf(threads[i++].addCount);
out.print(CharSequences.spaces(6 - n.length()));