4

I am playing around quickfix and I have a design question.

I process messages received in a function below:

void processFixMessage(Message message){
   //do stuff here
}

There's almost a certain chance that I cosume(process) messages slower. My question is, is there a way to handle such a situation where, If I haven't finished a message and received another message, a different thread should pick up and start processing.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kaleb Blue
  • 487
  • 1
  • 5
  • 20
  • looks like you may want to look into using a thread pool, that way you can distribute your messages to a thread in your pool. once the thread finishes the message it can be used again within the pool. This allows you to process messages with multiple threads, the next question to ask yourself is how many threads do you want to use? – RAZ_Muh_Taz Dec 02 '16 at 20:00
  • Thank you @RAZ_Muh_Taz..i'll start researching about thread pool – Kaleb Blue Dec 02 '16 at 20:04

1 Answers1

1

You can hop the thread in your processFixMessage(Message message). Depending on the rate of incoming message and time to process a single message you can choose how many threads you would like to create. One way is to create a ThreadPool of n threads and submit your message parsing to that pool. You can refer code: https://www.journaldev.com/1069/threadpoolexecutor-java-thread-pool-example-executorservice https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html

You can have dynamic number of threads based on machine as: int cores = Runtime.getRuntime().availableProcessors();

Hemant Singh
  • 1,487
  • 10
  • 15