Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: ThreadLocal Variables for Storing Thread-Specific State Information

ThreadLocal Variables for Storing Thread-Specific State Information MULTITHREADING IN SWING The Swing component set was first introduced in 1996, to be used in conjunction with the 1.1 version of what was then known as the Java Development Kit (JDK).

Tutorial Details:

THREADLOCAL VARIABLES FOR STORING THREAD-SPECIFIC STATE INFORMATION

Have you ever needed variables that were local to the scope of a thread, where each thread managed its storage, and it would be impossible for one thread to access the state information of another thread? The standard libraries offer two classes that provide this capability: ThreadLocal and InheritableThreadLocal.

Let\'s look at a simple use of the classes.

import java.util.Random;

public class ThreadLocal1 {

// Define/create thread local variable
static ThreadLocal threadLocal =
new ThreadLocal();
// Create class variable
static volatile int counter = 0;
// For random number generation
static Random random = new Random();

// Displays thread local variable, counter,
// and thread name
private static void displayValues() {
System.out.println (
threadLocal.get() + \"\\t\" + counter +
\"\\t\" + Thread.currentThread().getName());
}

public static void main (String args[]) {

// Each thread increments counter
// Displays variable info
// And sleeps for the random amount of time
// Before displaying info again
Runnable runner = new Runnable() {
public void run() {
synchronized(ThreadLocal1.class) {
counter++;
}
threadLocal.set(
new Integer(random.nextInt(1000)));
displayValues();
try {
Thread.sleep (
((Integer)threadLocal.get()).intValue());
displayValues();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};

// Increment counter, access thread local from
// a different thread, and display the values
synchronized(ThreadLocal1.class) {
counter++;
}
threadLocal.set(
new Integer(random.nextInt(1000)));
displayValues();

// Here\'s where the other threads
// are actually created
for (int i=0; i<5; i++) {
Thread t = new Thread(runner);
t.start();
}
}
}







 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
ThreadLocal Variables for Storing Thread-Specific State Information

View Tutorial:
ThreadLocal Variables for Storing Thread-Specific State Information

Related Tutorials:

Reading textual data: Fun with streams - JavaWorld - April 1999
Reading textual data: Fun with streams - JavaWorld - April 1999
 
Use JNDI to share objects between different virtual machines - JavaWorld July 1999
Use JNDI to share objects between different virtual machines - JavaWorld July 1999
 
Smart object-management saves the day - JavaWorld November 1999
Smart object-management saves the day - JavaWorld November 1999
 
Build an object database, Part 2: Object storage backend - JavaWorld April 2000
Build an object database, Part 2: Object storage backend - JavaWorld April 2000
 
Make a sweep with clean beans - JavaWorld November 1999
Make a sweep with clean beans - JavaWorld November 1999
 
XSL gives your XML some style - JavaWorld June 2000
XSL gives your XML some style - JavaWorld June 2000
 
Take control of the servlet environment, Part 2 - JavaWorld December 2000
Take control of the servlet environment, Part 2 - JavaWorld December 2000
 
Can ThreadLocal solve the double-checked locking problem?
Can ThreadLocal solve the double-checked locking problem?
 
JSP best practices
Follow these tips for reusable and easily maintainable JavaServer Pages
 
Master the Jxta shell, Part 1
Master the Jxta shell, Part 1
 
J2EE or J2SE? JNDI works with both
J2EE or J2SE? JNDI works with both
 
Discover and publish Web services with JAXR
Discover and publish Web services with JAXR
 
Very interesting
Very interesting
 
Very interesting
Very interesting
 
Fixing the Java Memory Model, Part 1
JSR 133, which has been active for nearly three years, has recently issued its public recommendation on what to do about the Java Memory Model (JMM).
 
Designing J2EE Applications for Real-Life Clustered Environments
Designing J2EE Applications for Real-Life Clustered Environments In this article, the authors draw from their practical experience to list and discuss some critical considerations when building J2EE applications so that they can be deployed in a cluste
 
When tears bring you back your beloved method...
Accessing platform-specific information hasn't always been easy. While you could certainly create processes with Runtime.exec(), dealing with differences across platforms to build parameter sets often led to headaches. In addition, the getenv() method of
 
SeSAm - Shell for Simulated Agent Systems
Multi-Agent Simulation Environment SeSAm (Shell for Simulated Agent Systems) provides a generic environment for modelling and experimenting with agent-based simulation. We specially focused on providing a tool for the easy construction of complex models,
 
An Introduction to Java Object Persistence with EJB
The 'impedance mismatch' between relational databases' tabular orientation and object-oriented Java's hierarchical one is a perennial problem for which the Java world has several good solution offerings. This article, the first in a three-part series, wil
 
Unweaving a Tangled Web With HTMLParser and Lucene
In this article I'll show you the basic technique in building a search engine using two powerful Open Source products: HTMLParser and Lucene.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.