You are on page 1of 5

I was reading that JavaDoc for Threadlocal here http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ThreadLocal.

html and it says "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). " favorite But my question is why did they choose to make it static (typically) - it makes it a bit confusing to have "per thread" state but its static? share [g+] share [fb] java multithreading share [tw] asked May 6 '10 at 19:54

link|improve this question kellyfj 144111 80% accept rate feedback

4 Answers
active oldest votes Because if it were an instance level field, then it would actually be "Per Thread - Per Instance", not just a guaranteed "Per Thread." That isn't normally the semantic you're looking for. up vote 8 down Usually it's holding something like objects that are scoped to a User vote accepted Conversation, Web Request, etc. You don't want them also subscoped to the instance of the class. One web request => one Persistence session. Not one web request => one persistence session per object.

java.lang

Class ThreadLocal
java.lang.Object java.lang.ThreadLocal

Direct Known Subclasses:

InheritableThreadLocal public class ThreadLocal extends Object 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). Since: 1.2

Constructor Summary
ThreadLocal()

Method Summary
Object get()

Returns the value in the current thread's copy of this thread-local variable.
protected Object initialValue()

Returns the current thread's initial value for this threadlocal variable.
void set(Object value)

Sets the current thread's copy of this thread-local variable to the specified value. Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail
ThreadLocal
public ThreadLocal()

Method Detail
initialValue
protected Object initialValue()

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 threadlocal, the first time the thread accesses the variable with the get() method. The initialValue method will not be invoked in a thread if the thread invokes the set(Object) method prior to the get method. This implementation simply returns null; if the programmer desires threadlocal 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. Returns: the initial value for this thread-local get

public Object get()

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. Returns: the current thread's value of this thread-local set

public void set(Object value)

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 initialValue() method to set the values of thread-locals.

Parameters: value - the value to be stored in the current threads' copy of this thread-local.

What exactly is a database engine?


I have gone through the definition on http://en.wikipedia.org/wiki/Database_engine several times: A database engine (or "storage engine") is the underlying software component that a database management system (DBMS) uses to create, read, update and delete (CRUD) data from a database. What I do not understand is what is left to do, isn't CRUD all that the databases do? If the database engine performs these functions, what does the rest of the database do? architecture database-engine crud database-structure feedback

2 Answers
active oldest votes CRUD is meant to define the characteristics necessary for a database as it relates to persistent storage. It is not meant to describe everything that could be done by a database engine. To make a comparison, fundamentally a vehicle is a device used for transport. While true, this definition certainly doesn't include all the detail entailed in a modern automobile. A database engine might handle multiple users, transactions, MVCC (Multiversion Concurrency Control), buffers and caches, ACID (atomicity, consistency, isolation, durability), as well as different isolation levels. A read may pull data from memory, remote databases, and multiple tables on disk processing it using SQL through multiple explicit and/or implicit code paths in order to present it to the requesting application. A create may allocate storage, provision structures, assign values, and do it's own processing before storing data. Etc.

The Database Engine is the core service for storing, processing, and securing data. The Database Engine provides controlled access and rapid transaction processing to meet the requirements of the most demanding data consuming applications within

your enterprise. Use the Database Engine to create relational databases for online transaction processing or online analytical processing data. This includes creating tables for storing data, and database objects such as indexes, views, and stored procedures for viewing, managing, and securing data. You can use SQL Server Management Studio to manage the database objects, and SQL Server Profiler for capturing server events. ySQL ( "My S-Q-L",[3] officially, but also commonly "My Sequel") is the world's most used[4] relational database management system (RDBMS)[5] that runs as a server providing multi-user access to a number of databases. It is named after developer Michael Widenius' daughter, My.[6] The SQL phrase stands for Structured Query Language.[7] MySQL was owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now owned by Oracle Corporation

You might also like