Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

global variables - threadlocal

Posted on 2013-01-21
3
Medium Priority
?
234 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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

715 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