dtech
dtech

Reputation: 14060

Parallelizing class with static member variables

I'm currently working with an application that does some heavy computational work. It has been ported from C to Java years ago and it shows a bit. Among others it uses public static variables to share data between classes.

The work is very suited for parallelizing as multiple files are processed and every file can be done completely independ of others. But just starting multiple threads doesn't work because of the static variables. I would like to prevent a rewrite because the classes are quite fast, mature and bug-free.

Is there an easy way for me to start multiple threads/processes from within the java program wherein each thread will have it's own copy of the static variables or will I have to resort to just calling the JVM multiple times by executing commands?

Upvotes: 0

Views: 196

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533590

Yes, You can use multiple class loaders, or start multiple processes.

However, I suggest just fixing the code, it would be much simpler. Make all the static fields, non static and have a ThreadLocal variable which hold the instance copy for that thread.

Upvotes: 3

Related Questions