Thread Isolation in Java

Is there any reliable way to ensure that threads remain isolated from each other in Java? I had a problem for a week and a half when Threads that implement various third-party source codes continue to collide due to static variables and other similar things that really are beyond my control.

I know that one system can run as many project instances as I work on. But when you try to include everything in a single-threaded executable file, there are always errors and exceptions.

I'm almost going to start a new process for every instance of this program that I want, but I really would not go this route (this will eliminate a lot of the real-time data that I collect, as well as hamper my ability to kill the target process.)

Suggestions? Thanks!

+4
source share
7 answers

If the authors of the library you want to use have not developed your code to be thread safe, then you can do little but prevent your two threads from accessing them simultaneously.

You can pull some tricks with class loaders, but this usually leads to a whole new world of complexity. To elaorate, you can load the same class two times (or more) in one JVM if you use different class loaders - therefore, you can effectively obtain independent copies of static variables. This use of separate class loaders is used by some Java EE application servers. This, in turn, leads to the fact that classloader and classpath will be used when the libraries themselves begin to perform some reflection and dynamic loading of classes. I would not recommend this assessment if your need is not very great.

According to your preferences:

1). You have a single threaded worker for unsafe code. Try to do as much work as possible in your application with mulit-threaded by going into Worker as little as possible.

2). If the employee is the main part of your processing, and therefore you really need to perform parallel execution, pull the employee into several separate processes, use some IPC communication for sharing. It looks like a JMS outsourcing solution might work well.

3). If you cannot afford to outwit IPC, try finding an alternative to streams, an alternative to streaming, or if you have influence on authors, ask them to fix the code. It really should not be so difficult to increase their parallelism.

+9
source

Themes that implement various third-party source codes continue to collide due to static variables and other similar things that really are beyond my control.

If this is true, then I think you need to go this route with separate processes. If the code you are calling is not thread safe, all you can do is make sure that this code is called only by one process at a time. This basically eliminates the benefits of running it on a different thread.

and also impede my ability to kill the target process

I do not see your goal here. Only with processes can you safely kill processing, it cannot be done in a safe way with threads unless you have complete control over all running code.

See also this question for a discussion of a similar problem.

+4
source

Your requirement applies to JSR-121 , which was approved in 2006.

I have not heard much about the implementation of JSR-121, but a quick search led me to http://weblogs.java.net/blog/gczaj/archive/j2se/index.html .

+2
source

There is no way to do this, unfortunately. If threads access shared resources, they should block these resources as necessary, otherwise your program will certainly encounter corruption in general condition.

Perhaps you can wrap access to shared resources so that you can synchronize access?

0
source

I am almost exactly launching a new Process for every instance of this program that I want, but I would prefer not to go down the route (this will eliminate a lot of the real-time data that I collect, as well as hamper my ability to kill the Process.)

From the sounds of this code, the code you are trying to reuse is not really intended for use in a multi-threaded application. In this case, starting a separate process for each instance may be your best option. Instead of hindering your ability to kill every instance, this should actually make it easier; see Process.destroy() .

It is not yet clear what you mean by β€œreal-time”, if each child process writes to its standard output, you can program your control program to read and map the output as you write it.

0
source

You can use a separate classloader for each thread to load third-party libraries that you want to keep separate.

0
source

You can try placing resources in one executor, so that you never have two of these processes running in parallel.

How can you do this using Executors:

 class Foo { private ExecutorService executor = Executors.newSingleThreadExecutor(); public void addTask(Runnable bar) { executor.submit(bar); } } 
0
source

All Articles