Solved

global variables - threadlocal

Posted on 2013-01-21
3
230 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

628 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