- Encapsulate access to static runner into synchronized methods to prevent

spawning of multiple threads.
- Make static runner field non-public.
This commit is contained in:
hns 2005-06-06 15:55:16 +00:00
parent 1a7a054894
commit 107aaa6a59

View file

@ -18,7 +18,6 @@ package helma.util;
import org.apache.commons.logging.*; import org.apache.commons.logging.*;
import java.io.*; import java.io.*;
import java.text.*;
import java.util.*; import java.util.*;
/** /**
@ -33,7 +32,7 @@ import java.util.*;
public class Logging extends LogFactory { public class Logging extends LogFactory {
// we use one static thread for all Loggers // we use one static thread for all Loggers
public static Runner runner; static Runner runner;
// the list of active loggers // the list of active loggers
static ArrayList loggers = new ArrayList(); static ArrayList loggers = new ArrayList();
@ -78,11 +77,7 @@ public class Logging extends LogFactory {
} }
} }
if ((runner == null) || !runner.isAlive()) { ensureRunning();
runner = new Runner();
runner.setDaemon(true);
runner.start();
}
return log; return log;
} }
@ -91,11 +86,7 @@ public class Logging extends LogFactory {
* Get a logger to System.out. * Get a logger to System.out.
*/ */
public static Log getConsoleLog() { public static Log getConsoleLog() {
if ((runner == null) || !runner.isAlive()) { ensureRunning();
runner = new Runner();
runner.setDaemon(true);
runner.start();
}
return consoleLog; return consoleLog;
} }
@ -154,16 +145,32 @@ public class Logging extends LogFactory {
shutdown(); shutdown();
} }
public static void shutdown() { /**
if (runner != null && runner.isAlive()) { * Make sure logger thread is active.
runner.interrupt(); */
runner = null; public synchronized static void ensureRunning() {
Thread.yield(); if ((runner == null) || !runner.isAlive()) {
runner = new Runner();
runner.setDaemon(true);
runner.start();
}
} }
/**
* Shut down logging, stopping the logger thread and closing all logs.
*/
public synchronized static void shutdown() {
if (runner != null && runner.isAlive()) {
runner.interrupt();
}
runner = null;
Thread.yield();
closeAll(); closeAll();
} }
/**
* Close all open logs.
*/
static void closeAll() { static void closeAll() {
consoleLog.write(); consoleLog.write();