A question on design...

I have a program that runs 3 threads

1) The main thread. (MyProg.java)

2) Some reader threads. (Reader.java)

3) Some worker threads. (Worker.java)

The main thread starts the Reader and Worker threads. Does some work on its own. Waits for Reader and Worker threads to complete and then does some more work.

Now I have lots of global variables (most of themfinal , say around 10) that need to be used by all 3 threads. Synchronized access is /not/ required because no one is going to change them once they are set by main.

Also I have some general methods which will be called from all 3 threads. So I have them as static methods in MyProg. (Assume synchronized access is not required)

Class MyProg{

privatestaticfinalint x = 1;

privatestaticfinalint y = 2;

privatestaticfinalint z = 3;

privatestaticint a;

privatestaticint b;

privatestaticint c;

/*

* A few more variables

*/

publicstaticvoid main (String args[]){

// set the value of a,b,c depending on args[]

// start reader and worker threads

// do some work

// wait for reader and worker threads to join

// do some more work

}

publicstatic method1(){

}

publicstatic method2(){

}

}

class Reader{

publicvoid run (){

/*

* use the global variables as follow...

*/

int a = MyProg.getA();

/*

* call the general methods as follows ..

*/

MyProg.method1();

MyProg.method2();

}

}

1) Is this a good idea to have all these static variables and methods in the class which has the main thread running.

2) Or should main() creante an instance of MyProg and send a reference to Reader and Writer ?

3) Or should I remove all the global variables/methods from MyProg and have them in a fourth class. Then main() can create an object of this 4th class and pass it on to the Reader and Worker threads ? (Or perhaps implement the 4th class as a singleton)

The problem with this approach is that I cannot think of a name for the 4th class. I could use MyProg for it but then what should I name the class which has main ?

4) Or should I use some other approach ?

[3938 byte] By [the_learnera] at [2007-11-27 10:32:52]
# 1

I'm suspicious of static *anything* beyond true constants. And since you are getting these values from the command line, they are not constants.

I would do this, mainly because it would make unit testing easier.

//assuming Reader needs the value of a.

public class Reader {

private int a;

public void setA(int a) {

this.a = a;

}

...

}

As for your static methods, again, I'm suspicious. Can you write Reader so that it doesn't know about MyProg?

BigDaddyLoveHandlesa at 2007-7-28 18:19:18 > top of Java-index,Java Essentials,Java Programming...
# 2

http://www.google.com/search?hl=en&q=Java+%2B+Observer+Pattern

Learn about the Observer pattern.

Cheers,

PS.

puckstopper31a at 2007-7-28 18:19:19 > top of Java-index,Java Essentials,Java Programming...
# 3

//assuming Reader needs the value of a.

public class Reader {

private int a;

public void setA(int a) {

this.a = a;

}

...

}

The problem with this is there are quite a few number of these global variables, so I'd have to pass all of them to the constructor !

the_learnera at 2007-7-28 18:19:19 > top of Java-index,Java Essentials,Java Programming...
# 4

> //assuming Reader needs the value of a.

> public class Reader {

>private int a;

>public void setA(int a) {

>this.a = a;

>

>

>...

>

>

>

> The problem with this is there are quite a few number

> of these global variables, so I'd have to pass all of

> them to the constructor !

How many are there?

BigDaddyLoveHandlesa at 2007-7-28 18:19:19 > top of Java-index,Java Essentials,Java Programming...
# 5

Around 3-4 final variables and 3 other variables that will be set by main.

Also, I have another question. Lets suppose there are a few variables that only the main method in MyProg class makes use of. But since main is static, I had to declare these variables and any other method in MyProg as static.

class MyProg {

private static int a;

private static int b;

private static int c;

private static void meth1() {

}

private static void meth2() {

}

public static void main (String args[]) {

/*

* Make use of a,b,c and call meth1() and meth2()

*/

}

}

Is this the right way to do it ? Or should main instantiate an object of its own class and then access those variables and methods ? Should I do it like this...

public static void main (String args[]) {

MyProg mp = new MyProg();

mp.a = 1;

mp.b = 2;

mp.meth1();

}

It just seems a little weird that main instantiates an object of its own class...

the_learnera at 2007-7-28 18:19:19 > top of Java-index,Java Essentials,Java Programming...
# 6

> Around 3-4 final variables and 3 other variables that

> will be set by main.

>

> Also, I have another question. Lets suppose there are

> a few variables that only the main method in MyProg

> class makes use of. But since main is static, I had

> to declare these variables and any other method in

> MyProg as static.

>

No. this is not right. You can declare and instantiate an object of the MyProg type, just like any other class, and use it just like any other object, as you have demonstrated. There is nothing weird about this, nothing at all. Avoid static junk unless you have a good reason to use them.

petes1234a at 2007-7-28 18:19:19 > top of Java-index,Java Essentials,Java Programming...
# 7

> Around 3-4 final variables and 3 other variables that will be set by main.

That's hardly "quite a few". My suggestion still makes sense.

BigDaddyLoveHandlesa at 2007-7-28 18:19:19 > top of Java-index,Java Essentials,Java Programming...