Java Doc for ThreadLocal.java in  » 6.0-JDK-Modules » j2me » java » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.lang 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.lang.ThreadLocal

All known Subclasses:   java.lang.InheritableThreadLocal,
ThreadLocal
public class ThreadLocal (Code)
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).

For example, in the class below, the private static ThreadLocal instance (serialNum) maintains a "serial number" for each thread that invokes the class's static SerialNum.get() method, which returns the current thread's serial number. (A thread's serial number is assigned the first time it invokes SerialNum.get(), and remains unchanged on subsequent calls.)

 public class SerialNum {
 // The next serial number to be assigned
 private static int nextSerialNum = 0;
 private static ThreadLocal serialNum = new ThreadLocal() {
 protected synchronized Object initialValue() {
 return new Integer(nextSerialNum++);
 }
 };
 public static int get() {
 return ((Integer) (serialNum.get())).intValue();
 }
 }
 

Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).
author:
   Josh Bloch and Doug Lea
version:
   1.23, 10/10/06
since:
   1.2


Inner Class :static class ThreadLocalMap



Method Summary
 ObjectchildValue(Object parentValue)
     Method childValue is visibly defined in subclass InheritableThreadLocal, but is internally defined here for the sake of providing createInheritedMap factory method without needing to subclass the map class in InheritableThreadLocal.
static  ThreadLocalMapcreateInheritedMap(ThreadLocalMap parentMap)
     Factory method to create map of inherited thread locals.
 voidcreateMap(Thread t, Object firstValue)
     Create the map associated with a ThreadLocal.
public  Objectget()
     Returns the value in the current thread's copy of this thread-local variable.
 ThreadLocalMapgetMap(Thread t)
     Get the map associated with a ThreadLocal.
protected  ObjectinitialValue()
     Returns the current thread's initial value for this thread-local variable.
public  voidset(Object value)
     Sets the current thread's copy of this thread-local variable to the specified value.



Method Detail
childValue
Object childValue(Object parentValue)(Code)
Method childValue is visibly defined in subclass InheritableThreadLocal, but is internally defined here for the sake of providing createInheritedMap factory method without needing to subclass the map class in InheritableThreadLocal. This technique is preferable to the alternative of embedding instanceof tests in methods.



createInheritedMap
static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap)(Code)
Factory method to create map of inherited thread locals. Designed to be called only from Thread constructor.
Parameters:
  parentMap - the map associated with parent thread a map containing the parent's inheritable bindings



createMap
void createMap(Thread t, Object firstValue)(Code)
Create the map associated with a ThreadLocal. Overridden in InheritableThreadLocal.
Parameters:
  t - the current thread
Parameters:
  firstValue - value for the initial entry of the map
Parameters:
  map - the map to store.



get
public Object get()(Code)
Returns the value in the current thread's copy of this thread-local variable. Creates and initializes the copy if this is the first time the thread has called this method. the current thread's value of this thread-local



getMap
ThreadLocalMap getMap(Thread t)(Code)
Get the map associated with a ThreadLocal. Overridden in InheritableThreadLocal.
Parameters:
  t - the current thread the map



initialValue
protected Object initialValue()(Code)
Returns the current thread's initial value for this thread-local variable. This method will be invoked at most once per accessing thread for each thread-local, the first time the thread accesses the variable with the ThreadLocal.get() method. The initialValue method will not be invoked in a thread if the thread invokes the ThreadLocal.set(Object) method prior to the get method.

This implementation simply returns null; if the programmer desires thread-local variables to be initialized to some value other than null, ThreadLocal must be subclassed, and this method overridden. Typically, an anonymous inner class will be used. Typical implementations of initialValue will invoke an appropriate constructor and return the newly constructed object. the initial value for this thread-local




set
public void set(Object value)(Code)
Sets the current thread's copy of this thread-local variable to the specified value. Many applications will have no need for this functionality, relying solely on the ThreadLocal.initialValue() method to set the values of thread-locals.
Parameters:
  value - the value to be stored in the current threads' copy ofthis thread-local.



Methods inherited from java.lang.Object
public boolean equals(Object obj)(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.