Solved

Constructor question

Posted on 1998-11-19
1
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

688 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