001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Elena Semukhina
020: * @version $Revision$
021: */package java.lang;
022:
023: public class InheritableThreadLocalSupport extends Thread {
024:
025: static Object localO = null;
026: static Integer localI = null;
027: static Integer newLocalI = new Integer(10);
028:
029: public boolean parentInheritableLocalObjectOK = false;
030: public boolean childInheritableLocalObjectOK = false;
031: public boolean grandChildInheritableLocalObjectOK = false;
032: public boolean parentInheritableLocalIntegerOK = false;
033: public boolean childInheritableLocalIntegerOK = false;
034: public boolean grandChildInheritableLocalIntegerOK = false;
035:
036: static InheritableThreadLocal<Object> itlo = new InheritableThreadLocal<Object>() {
037:
038: protected synchronized Object initialValue() {
039: localO = new Object();
040: return localO;
041: }
042: };
043:
044: static InheritableThreadLocal<Integer> itli = new InheritableThreadLocal<Integer>() {
045:
046: protected synchronized Integer initialValue() {
047: localI = new Integer(5);
048: return localI;
049: }
050:
051: protected synchronized Integer childValue(Integer parentValue) {
052: return new Integer(parentValue.intValue() * 2);
053: }
054: };
055:
056: public void run() {
057: Object valO = itlo.get();
058: if (valO.equals(localO)) {
059: parentInheritableLocalObjectOK = true;
060: }
061: Integer valI = itli.get();
062: if (valI.equals(localI)) {
063: parentInheritableLocalIntegerOK = true;
064: }
065: Child child = new Child();
066: child.start();
067: try {
068: child.join();
069: } catch (InterruptedException ie) {
070: return;
071: }
072: childInheritableLocalObjectOK = child.childInheritableLocalObjectOK;
073: grandChildInheritableLocalObjectOK = child.grandChildInheritableLocalObjectOK;
074: childInheritableLocalIntegerOK = child.childInheritableLocalObjectOK;
075: grandChildInheritableLocalIntegerOK = child.grandChildInheritableLocalObjectOK;
076: }
077: }
078:
079: class Child extends InheritableThreadLocalSupport {
080:
081: public void run() {
082: Object valO = itlo.get();
083: if (valO.equals(localO)) {
084: childInheritableLocalObjectOK = true;
085: }
086: Integer valI = itli.get();
087: if (valI.intValue() == newLocalI.intValue()) {
088: childInheritableLocalIntegerOK = true;
089: }
090: GrandChild gChild = new GrandChild();
091: gChild.start();
092: try {
093: gChild.join();
094: } catch (InterruptedException ie) {
095: return;
096: }
097: grandChildInheritableLocalObjectOK = gChild.grandChildInheritableLocalObjectOK;
098: grandChildInheritableLocalIntegerOK = gChild.grandChildInheritableLocalIntegerOK;
099: }
100: }
101:
102: class GrandChild extends InheritableThreadLocalSupport {
103:
104: public void run() {
105: Object valO = itlo.get();
106: if (valO.equals(localO)) {
107: grandChildInheritableLocalObjectOK = true;
108: }
109: Integer valI = itli.get();
110: if (valI.equals(newLocalI)) {
111: grandChildInheritableLocalIntegerOK = true;
112: }
113: }
114: }
|