Solved

global variables - threadlocal

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
JUnit 4 @Before and @BeforeClass differences 3 60
egit plugin on eclipse 8 64
hibernate insert example 13 29
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

810 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