Solved

global variables - threadlocal

Posted on 2013-01-21
3
217 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
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now