• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do I need a thread pool?

 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so I have a simple system. I have a "LogManager" and "Loggers" loggers are things that write to file/console/DB, whereas Log Managers hold loggers and dispatch messages between them... Log managers could be threaded, timed, etc...
So I am working on a threaded log manager. So far I've been working my ass of without any result or any useful help... So here's what I've come up with:

Ok so what happens here is: Whenever I am asked to log some data, I create a NEW thread that delivers message to all the loggers... the questions are:
1. Shouldn't I make that thread a daemon? I don't really understand daemons, but since it's running as a background thread, shouldn't htat be approporiate?
2. Don't I need a thread pool? Cause if the user does 10000 log() in a loop, I'll have 10000 threads, and all performance will be destroyed when threads will be switching back and forth.. is that true?
Before this I was thinking of making ONE background thread that would run forever with low priority and take messages from the queue and log them. Everything seemed to be fine except for one thing - whenever JVM would exit, the thread would stay in memory... and it's like java is so stupid... finalize() isn't reliable.. Runtime().getRuntime().addTerminationHook() doesn't work... so somebody said to pretty much abandon the idea of having 1 background thread...

Answers/suggestions?
Thank you in advance,
Ivan Jouikov
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Shouldn't I make that thread a daemon? I don't really understand daemons, but since it's running as a background thread, shouldn't htat be approporiate?

Well, this is a judgement call. The thing about daemon threads is that if only daemon threads are running an a JVM, then the JVM will exit. So if your log threads were daemons, then if a message hadn't been logged yet when the program completed, the JVM would exit and the message would disappear.

2. Don't I need a thread pool?

Creating a new thread for each message is, indeed, expensive.

Everything seemed to be fine except for one thing - whenever JVM would exit, the thread would stay in memory

As we said before, that's what daemon threads are for! If your background thread were a daemon thread, then it would not prevent the JVM from exiting. The one-log-thread architecture makes the most sense.
Now, a question for you: Why reinvent the wheel? why not use java.util.logging, or the very popular log4j?
 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not doing this to re-invent anything...
I just find practice the best way to learn java...
(3 quarters in classroom were worth less than one day writing java progarms)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!