Solved

Constructor question

Posted on 1998-11-19
1
219 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

680 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