Solved

Constructor question

Posted on 1998-11-19
1
217 Views
Last Modified: 2010-03-30
I would like multiple thread within a process to access
a common data.
I put the common data in a class and declared it static
i.e public class A {
           static int data1;
           static string data2;

           public synchronized int makechanges()
           {
              //make changes to static data...
           }
   }
my question regards the constructor for this class.
Obviously I would like to initialize data1 & data2 at
the start of the program.
But I can't initialize data1 & data2 in the constructor since each instantiation of this class by a
thread will reset the existing static data.
I could just set a flag as another static field in the
class to deal with this problem.
But is this the best way to get multiple threads to share data ?

Thanks
0
Comment
Question by:kkandasa
1 Comment
 
LVL 5

Accepted Solution

by:
msmolyak earned 20 total points
ID: 1227895
What you need is a static initializer which is executed once when the class is loaded by the class loader.

public class A {
               static int data1;
               static string data2;
               static
               {
                 data1 = ...
                 data2 = ...
               }
.

This is of course not the only way for the threads to share data, but this is the simplest. other ways are passing a non-static object reference to each thread, making an object a singleton with a static method getXXX() which returns a reference to a single object instance (it is similar to making the object static but the user does not know it is static).

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 37
spring jars download 1 26
How do I remove an object from a 3 23
pagenation logic how it is working in my code 1 27
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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now