Solved

global variables - threadlocal

Posted on 2013-01-21
3
227 Views
Last Modified: 2013-01-25
hi guys

<<If you are porting a single-threaded application to multithreaded environment, you can preserve thread safety by converting shared global variables into ThreadLocals.
>>
Lines taken from book
Brian Goetz, Java concurrency in practise, page 46.

Can anyone please explain with an example how this works.

thanks
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 38804451
I will try :-)
lets assume that "shared global variable" is variable that could be accessed from every thread from everywhere. Converting such as variable into ThreadLocal variable means that every thread will have its own, independently initialized copy of the variable.

Now imagine this lines in the code:
myArray is array of ten elements from 0 to 9, and index is shared global variable!

if there is only one thread:
int index = 0;
index++;
print myArray[index] means print myArray[1] it means prints "1" - CORRECT!

if there are two threads, and index is still global sahred variable:
thread 1 executes:
int index = 0;
index++;
now thread 1 stops and thread 2 starts, meanwhile index is 1, increased by thread 1
thread 2 executes :
int index = 0;
index++;
now thread 2 stops and again thread 1 starts, meanwhile index is 2, increased by thread 2 one more time.
now thread 1 continiues working:
print myArray[index] means print myArray[2] it means prints "2" - NOT CORRECT!
when thread 1 was interupted index was 1, so it's expecting on the next line to print 1, but it prints 2!
If index was converted into thread local variable every thread will increase it's own copy of index and the above described scenario will never happen.
I also have to read this book, because I have heard that it's the best book for multithreading.
0
 

Author Comment

by:royjayd
ID: 38805939
thanks

>>If index was converted into thread local variable every thread will increase it's own copy of index

can you provide some code as to how this can be accheived?

thx.
0
 
LVL 16

Expert Comment

by:Valeri
ID: 38808692
int index; in single threaded application
ThreadLocal<Integer> index = new ThreadLocal<Integer>(); in multithreaded application
it's always good to write your own small applications when you learn about multuthreading, to see what's going on.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question