I am working on a coding exercise in which I have to create a logging framework in java. Steps for logging is like Log4J i.e. 1. Message is processed by a processor. 2. Message is appended to IO or in file or on server by an appender.
The requirement is class should handle multiple threads on same time (Asynchronous Manner). Class has two methods log() – used for logging and shutdown() – for termination. I have objects of message_processor and appender but, this process should done in FIFO manner.
My Job is when log() is called message_processor will process the message which is very time-consuming process and then the processed message is appended and logged.
I have a rough idea which is, I have to create a static data structure in which I will be adding a thread and its key. I will keep adding whenever log() is called. After adding into my data structure I will start the thread and keep checking if the thread is completed or not. When the thread is completed and the thread which is completed has no predecessors then, I will append it or else I will wait for the predecessors to be completed. If the thread is completed or not I have to maintain a flag.
Log() will spawn threads for message processing and will wait for threads to be completed their task and when the thread is completed, log() will append the messages in-order. The data structure will be read and write by multiple threads on same time.
My question is which data structure should I use to implement this kind of functionality in Java. For the flag should I create a Java class which implements a Runnable interface or Is there any data structure which these kind of functionality?
Is it an efficient idea to implement Asynchronous threading?
Any suggestions are welcomed.
I think, you should use concurrent data structures that provide by framework instead of doing your implementation.