• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using java.util.logging.Logger in a multiThreaded application  RSS feed

 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have an app which creates and runs three threads. In each thread, I make a static object by calling MyLogger.getInstance(); (see code below)
My question is this, does the first call to getInstance() actually make the call to the MyLogger Constructor, and then subsequent calls to getInstance() just get the previously created instance?
Also, can all three threads get and use the same instance? I ask because when I look at my log files, I am missing messages from two of my threads, I am only seeing the messages from one thread in the log file.
Any help would be greatly appreciated,
Thanks.



 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh!
Just realized that the reason why I wasnt getting the log messages from two of my three threads was because I had commented out the call to run them as part of debugging something else and forgot to uncomment them. (If I had an embarressed looking smiley I would have inserted it here.

I am still interested however in the answer to my first question:
My question is this, does the first call to getInstance() actually make the call to the MyLogger Constructor, and then subsequent calls to getInstance() just get the previously created instance?


Thanks!
Kim
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly. When the MyLogger class is first loaded, its static variables are instantiated. That includes assigning the value to the INSTANCE variable. Then, any time the getInstance() method is called, that same object is returned.

It may happen that calling MyLogger.getInstance() causes the class to be loaded, because it is the first use of the class. So you could sort of say, in that case, that the first call to getInstance() causes the MyLogger constructor to be called. But you would be wrong to say that the call to getInstance() makes the call to the MyLogger constructor. What it actually does, in that case, is to cause the class to be loaded. And that in turn causes the constructor to be called.
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent, thanks so much for your reply.
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For future reference, the embarrassed smiley you're supposed to use is this one:


[ November 29, 2006: Message edited by: Ulf Dittmer ]
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thank you Ulf, I owe you one. The help on JavaRanch can't be beat.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!