You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/12 01:42:48 UTC
svn commit: r430920 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/lang/ThreadLocal.java
test/java/org/apache/harmony/luni/tests/java/lang/ThreadLocalTest.java
Author: ndbeyer
Date: Fri Aug 11 16:42:47 2006
New Revision: 430920
URL: http://svn.apache.org/viewvc?rev=430920&view=rev
Log:
Initial implementation of ThreadLocal.remove() and test case.
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ThreadLocal.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ThreadLocalTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ThreadLocal.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ThreadLocal.java?rev=430920&r1=430919&r2=430920&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ThreadLocal.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/ThreadLocal.java Fri Aug 11 16:42:47 2006
@@ -15,7 +15,6 @@
package java.lang;
-
/**
* A ThreadLocal is a variable that has a per-thread value. Different Threads
* may reference the same ThreadLocal object, but the values they observe will
@@ -24,34 +23,50 @@
* @see java.lang.Thread
*/
public class ThreadLocal<T> {
- /**
- * Constructs a new ThreadLocal object
- */
- public ThreadLocal() {
- super();
- }
+ /**
+ * Constructs a new ThreadLocal object
+ */
+ public ThreadLocal() {
+ super();
+ }
- /**
- * Return the value of this variable under
- * <code>Thread.currentThread()</code>
- */
+ /**
+ * Return the value of this variable under
+ * <code>Thread.currentThread()</code>
+ */
@SuppressWarnings("unchecked")
- public T get() {
- return (T)Thread.currentThread().getThreadLocal(this);
- }
+ public T get() {
+ return (T) Thread.currentThread().getThreadLocal(this);
+ }
+
+ /**
+ * Return the initial value of this variable under
+ * <code>Thread.currentThread()</code>
+ */
+ protected T initialValue() {
+ return null;
+ }
- /**
- * Return the initial value of this variable under
- * <code>Thread.currentThread()</code>
- */
- protected T initialValue() {
- return null;
- }
+ /**
+ * Set the value of this variable under <code>Thread.currentThread()</code>
+ */
+ public void set(T value) {
+ Thread.currentThread().setThreadLocal(this, value);
+ }
- /**
- * Set the value of this variable under <code>Thread.currentThread()</code>
- */
- public void set(T value) {
- Thread.currentThread().setThreadLocal(this, value);
- }
+ /**
+ * <p>
+ * Removes the current value, such that the next call to {@link #get()} will
+ * return the default value, as defined by {@link #initialValue()}.
+ * </p>
+ *
+ * @since 1.5
+ */
+ public void remove() {
+ /*
+ * TODO Consider adding an explicit removeThreadLocal method to Thread
+ * for VM implementations to take advantage of extra possible space.
+ */
+ Thread.currentThread().setThreadLocal(this, initialValue());
+ }
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ThreadLocalTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ThreadLocalTest.java?rev=430920&r1=430919&r2=430920&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ThreadLocalTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ThreadLocalTest.java Fri Aug 11 16:42:47 2006
@@ -15,32 +15,53 @@
package org.apache.harmony.luni.tests.java.lang;
-public class ThreadLocalTest extends junit.framework.TestCase {
+import junit.framework.TestCase;
- /**
- * @tests java.lang.ThreadLocal#ThreadLocal()
- */
- public void test_Constructor() {
- // Test for method java.lang.ThreadLocal()
+public class ThreadLocalTest extends TestCase {
- ThreadLocal l = new ThreadLocal();
- assertTrue("Failed to create ThreadLocal", l instanceof ThreadLocal);
-
- }
+ /**
+ * @tests java.lang.ThreadLocal#ThreadLocal()
+ */
+ public void test_Constructor() {
+ try {
+ new ThreadLocal<Object>();
+ } catch (Exception e) {
+ fail("unexpected exception: " + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.lang.ThreadLocal#remove()
+ */
+ public void test_remove() {
+ ThreadLocal<String> tl = new ThreadLocal<String>() {
+ @Override
+ protected String initialValue() {
+ return "initial";
+ }
+ };
+
+ assertEquals("initial", tl.get());
+ tl.set("fixture");
+ assertEquals("fixture", tl.get());
+ tl.remove();
+ assertEquals("initial", tl.get());
+ }
/**
* @tests java.lang.ThreadLocal#get()
*/
public void test_get() {
// Test for method java.lang.Object java.lang.ThreadLocal.get()
- ThreadLocal l = new ThreadLocal();
+ ThreadLocal<Object> l = new ThreadLocal<Object>();
assertNull("ThreadLocal's initial value is null", l.get());
// The ThreadLocal has to run once for each thread that touches the
// ThreadLocal
final Object INITIAL_VALUE = "'foo'";
- final ThreadLocal l1 = new ThreadLocal() {
- protected Object initialValue() {
+ final ThreadLocal<Object> l1 = new ThreadLocal<Object>() {
+ @Override
+ protected Object initialValue() {
return INITIAL_VALUE;
}
};
@@ -57,7 +78,8 @@
final ResultSlot THREADVALUE = new ResultSlot();
Thread t = new Thread() {
- public void run() {
+ @Override
+ public void run() {
THREADVALUE.result = l1.get();
}
};
@@ -83,7 +105,7 @@
// Test for method void java.lang.ThreadLocal.set(java.lang.Object)
final Object OBJ = new Object();
- final ThreadLocal l = new ThreadLocal();
+ final ThreadLocal<Object> l = new ThreadLocal<Object>();
l.set(OBJ);
assertTrue("ThreadLocal's initial value is " + OBJ, l.get() == OBJ);
@@ -96,7 +118,8 @@
final ResultSlot THREADVALUE = new ResultSlot();
Thread t = new Thread() {
- public void run() {
+ @Override
+ public void run() {
THREADVALUE.result = l.get();
}
};
@@ -115,19 +138,5 @@
assertNull("ThreadLocal's value in other Thread should be null",
THREADVALUE.result);
- }
-
- /**
- * Sets up the fixture, for example, open a network connection. This method
- * is called before a test is executed.
- */
- protected void setUp() {
- }
-
- /**
- * Tears down the fixture, for example, close a network connection. This
- * method is called after a test is executed.
- */
- protected void tearDown() {
}
}