Running a threading class as a daemon

I am writing a multi-threaded solution in Java to connect two systems, A and B. System A is completely sequential, not streamed, and will supply data to send to system B. System B receives data from multiple sources asynchronously all at the same time.

I am using ThreadPoolExecutor for thread management. I'm using a static singleton instance of the TP class that wraps around ThreadPoolExecutor (which is also static) because System A cannot create new objects, but it can make calls to static objects.

This is where I am stuck. I do some very basic setup tests before I go all out. I created two classes for testing, T1 and T2. Each of these classes imports the TP class (where a static singleton is created). T1 adds some objects to the TP queue and then adds more T2.

Even though the TP object is declared static, it looks like the two versions are running in parallel. Objects submitted to the queue on T2 are executed before the object represented by T1 has been executed. Also, since neither T1 nor T2 calls shutdown () on ThreadPoolExector, they both hang and never exit.

How can I create a static instance of the protector daemon that basically wakes up whenever I submit something to be processed, even from different Java executables?

0


a source to share


3 answers


If you run two separate processes, you have two separate types and two separate instances, regardless of whether it is single.

If you want two different processes to talk to each other, you need to solve this problem completely separately. There are many different IPC mechanisms - networks, named pipes (tricky from Java IIRC), memory-mapped files, a simple shared directory where one process puts tasks for another to process, etc.



It is also unclear what is hanging or how the thread pool is configured. If the problem is indeed from the streaming side (not the IPC side), please post a short but complete program that demonstrates the problem.

+1


a source


If the thread pool size is greater than 1, then there is no guarantee that all T1 objects will be processed first.



0


a source


It seems to me that you are using two different "main" classes that use the same static singleton class. In this case, two instances of the generated singlelet will be created - one in each JVM.

I think you want the thread pool to be encapsulated in another process that runs as a service and provides some mechanism for IPC, as Skeet commented. The usual way to do this is to expose a JMS queue to receive requests from different producers and ask the consumer (your daemon) to send the requests it receives to the thread pool for processing.

To run this service as a daemon, you can host it in a container, or use something like Java Wrapper if you are running on Windows.

0


a source







All Articles