..


Sponsored Links

Management of shared data in Java

Article written by Damiano Verda
Page 1 of 2

We can think of a shared data like any other information processing tasks to be exposed to more parties. Imagine, for example, store a numerical value representing the euro / dollar. We have a program that periodically checks what the exchange rate and updates the value of the variable. One or more other programs, however, will use the updated information to operate the exchange rate conversion price.

Notice how the management of a shared, even in this simple example, need special attention. Imagine for example that while an application needs to read the exchange rate to convert, is also received by another program, a request to change the exchange rate itself.

Which is executed first? Which exchange rate conversion is done? The problem, even in some cases slightly more complex, could be even more severe.

Imagine fact that the variable of interest is not elementary but, for example, consists of a set of data. It may now be performed only partial write, and then followed by a reading from the completion of the writing. The transactions would then be performed with a set of values ​​changed only partially, giving rise to meaningless results and therefore unacceptable.

Given the existence of this kind of problems many programming languages ​​provide specific tools for the management of shared variables. We examine below in particular as this category of problems is handled in the Java programming language, one of the most used of all.

You first need to understand how we can ensure that only one program at once, or more precisely only one thread at a time (one thread is the basic component of a process or program, in other words a program can consist of multiple threads, run simultaneously) can access a shared variable. The mechanism that allows us to offer this guarantee is called mutual exclusion.

Mutual exclusion

Imagine developing a class (or a set of data and methods, that is useful functions to process the data itself) called Variabile_Condivisa structured as follows:






 public class Variabile_Condivisa







 {



   



 euro_dollaro float;



   



 euro_sterlina float;





   



 Variabile_Condivisa ()



  



 {



     



 euro_dollaro = 1;



     



 euro_sterlina = 1;



  



 }





   



 set_euro_dollaro void (float e_d) = {euro_dollaro e_d;}



   



 set_euro_sterlina void (float e_s) = {euro_sterlina e_s;}





   



 get_euro_dollaro float () {return euro_dollaro;}



   



 get_euro_sterlina float () {return euro_sterlina;}







 }



The two statements, and that euro_dollaro euro_sterlina, representing the exchange rate euro / dollar and euro / sterling and that we wish to share information among multiple programs (or between multiple threads). The methods developed allow you to assign a value to these data (and set_euro_dollaro set_euro_sterlina) and read those values ​​(and get_euro_dollaro get_euro_sterlina).

Then we identify a particular method (Variabile_Condivisa), which is called the class constructor is executed and that the creation of each Variabile_Condivisa, in this case by setting the values ​​of variables and euro_dollaro euro_sterlina are set to 1.

Then we create in our program an object type named var Variabile_Condivisa (the class represents a set of entities with common characteristics, while an object represents a specific element of this set, which can be referenced within the program ) this way:






 Variabile_Condivisa Variabile_Condivisa var = new ();



As we can now ensure that there are no problems in the management of data shared var? The Java language provides the keyword (or keywords) synchronized, which accepts any object as a parameter. Through synchronized, you can define, as shown in the example, the block of code:





 synchronized (var)







 {



  



 / / Synchronized block of code delimited (var)

  





 }



Before performing the instructions in the synchronized block, any thread acquires the lock on the variable var, or block all further access to the same block of code until the lock is released, or until after running the entire block example of code delimited by curly braces.

In other words, the first thread (which we imagine to call first) that performs the synchronized statement (var) actually creates a barrier that prevents any other thread to execute the synchronized statement (var) until the first has not completed the execution of the synchronized block of code delimited. In these blocks are then inserted instructions read or write shared data.

In this way it is guaranteed mutual exclusion, which ensures that only one thread at a time can access a shared variable. It should be noted, however, that it is necessary to carefully choose the object to be passed as a parameter to synchronized. It must be an object common to all the threads in which you want to create a mechanism for mutual exclusion, for example, as in this case, the variable you want to read or edit.

Imagine, at this point, you want to set our program so that the threads that want to read the value of the variable var to be put on hold and evaluate the results only after the first update following their request. Imagine that you want to define a synchronization mechanism between reading and writing.

In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links