ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm working with threads in java and have some questions about accessing an array concurrently.
The situation: I've an array a of doubles, which is "empty" in the beginning. Each double needs to be calculated and takes some amount of work to be calculated. Fortunately, they are independent from each other (the value of a[i] does not depend on the value of a[j]); hence I thought that I could use threads to calculate each a[i]. The idea is, with for example, use two threads, thus the first one calculates the values of a..a[a.length/2], and the second thread calculates the remaining values.
I've implemented this by creating classes that extend Threads and that, when constructed, receive a reference to the array as well as the first and last indices to calculate. To do the calculation the Threads also read some data from other objects (also shared), but do not change any attribute of those other objects.
The question(s): Does it work? I mean: Is there a problem with accessing the same array from two or more threads, even if I'm working with different elements of the array? Do you think it is a good idea to calculate the values like this, or is there a better schema for this? I'm guessing that the situation is fairly common...
Any help on these questions as well as pointers to literature (hopefully on the web, and free) will be truly appreciated.
I can't see any problem. Those doubles are at different indexes, so each of the thread works with it's own double. Theoretically it may be possible that both threads could access the array at the same time, but in reality ...
You could always use a threadsafe collection instead of an array. The advantage is that you'll never run into an IndexOutOfBoundsException as collections can grow dynamically. However, anything that involves threading also incurs overhead, which may make it less attractive than it seems at first. ArrayLists are faster than Vectors precisely because they are not threadsafe.
I agree with paulsm4. As long as you are certain that each thread will never access the same block based on your logic you should be fine. However, it never hurts to build in a failsafe. A semaphore would help ensure that the same block is never
accessed at the same time.
paulsm4. I was in the doubt because, even thought these are different indexes, it is still 'the same array'.
Aliem-Hominid, paulsm4. Indeed it 'will be' a multi-core machine. (I'm developing in PIV, and here, as expected, it actually takes longer with more than one thread).
jay73. I thought about using some safe containers, but the truth is the size if fixed and the values are disposable. Then I'm not sure that safe containers would allow to access different indexes in parallel.
terrio. How does the semaphore work? Is it possible to block only one element in the array?
I've done some tests and it *seems* to work for some simpler cases (where I've computed other values, easily checkable).
Just to give you some more background. I'm implementing one of these 'genetic algorithms' where you keep a population of individuals. Each individual has a cercain fitness (a double) and, accordingly to the fitness, they survive/cross, etc and based on this new generations are created, and then the fitness values for the next generation need to be recalculated... Thus, a lot of time is spent computing the fitness for each individual and that is why I'm trying to paralellize the calculation.
Thank you all for your replies!!
Last edited by enemorales; 11-30-2007 at 09:14 AM.
In your case, there are two ways to approach your semaphore. First, restrict access to the entire array to only one thread at a time, or be more fine grained and restrict access to each individual address in the array to one thread at a time.
The first case is the easiest to implement. In you thread class define a class variable that would be used to indicate when a thread is accessing the array. All threads can then check the value of this class variable to see if the array is available or not. If it is not available, wait until it is.
The second scenario would require a way of tracking which indexes in the array are currently being accessed (another Array possibly). Each thread would then look to see if the address it requires is already being accessed, if so, wait until the address is available.
Hope this helps. There are plenty of examples available as well so a quick google should get you on the right track.