Help needed with logging API and threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Huzefa

    Help needed with logging API and threads

    I am working on a amll project in Java that includes many classes.
    Each of the classes has a Logger object. I have associated a
    FileHandler with
    each of these Logger objects. The file is the same for each of these
    classes "log.xml" Now I want all the classes to log to the same file.
    However, this
    does not happen. Each class creates its own log file. The base class
    uses the file "log.xml". Each subsequent class creates a log file
    "log.xml.1" , "log.xml.2" and so on. Can I get all my classes to log to
    the same file? If so how?

    The second query is about threads. My project uses the internet to get
    data. Now I want only a specific number of threads to access the net
    at any given time (the exact number will be specified in the
    preferences). The other threads not accessing the net should function
    normally. How can I achieve this sort of behaviour? Please keep in
    mind that this is the first time I am working with threads.

    Thanx,

    Huzefa
  • Kristian Bisgaard Lassen

    #2
    Re: Help needed with logging API and threads

    On Thu, 02 Oct 2003 11:21:24 -0700, Huzefa wrote:
    [color=blue]
    > I am working on a amll project in Java that includes many classes.
    > Each of the classes has a Logger object. I have associated a
    > FileHandler with
    > each of these Logger objects. The file is the same for each of these
    > classes "log.xml" Now I want all the classes to log to the same file.
    > However, this
    > does not happen. Each class creates its own log file. The base class
    > uses the file "log.xml". Each subsequent class creates a log file
    > "log.xml.1" , "log.xml.2" and so on. Can I get all my classes to log to
    > the same file? If so how?[/color]
    Declare your OutputStream/FileHandler (the thing you have chosen to write
    to files with) static in your Logger class. This means that
    their will only be one instance of the OutputStream object in your Logger objects.
    Alternatively you could instanciate all you objects with the samme
    OutputStream and syncronize acces to the write methods if concurrent
    processes are logging at the same time.
    Doing the last refactoring can be done using the the
    factory pattern for creating you Logger objects, but there are other ways.[color=blue]
    >
    > The second query is about threads. My project uses the internet to get
    > data. Now I want only a specific number of threads to access the net
    > at any given time (the exact number will be specified in the
    > preferences). The other threads not accessing the net should function
    > normally. How can I achieve this sort of behaviour? Please keep in
    > mind that this is the first time I am working with threads.[/color]
    A simple polling technique would be this:

    1. Start as many processes as you want running at the same time.
    2. Wait some time.
    3. Test if any of them has died.
    4. If pending procceses exist or running process exist goto 1, otherwise
    you are done.

    I have an example you can look at if you need some actual code to
    understand the algorithm. The code is not pretty I know (and could be made
    more efficient), but I think it is readable:

    import java.util.*;

    public class test extends Thread {

    class Dummy extends Thread {
    int count;
    public Dummy (int count) {this.count = count;}
    public void run () {
    try {
    sleep (1000);
    System.err.prin tln (count+" doing som stuff");
    sleep (1000);
    System.err.prin tln (count+" ending...");
    } catch (Exception e) {e.printStackTr ace (); }
    }
    }

    public static void main (String[] args) throws Exception {
    new test (new Integer (args[0]).intValue ());
    }

    int max_running = 3;
    LinkedList processes, running;

    public test (int size) throws Exception {
    processes = new LinkedList ();
    for (int i = 0; i < size; i++) {
    processes.add (new Dummy (i));
    }
    run ();
    }

    public void run () {
    running = new LinkedList ();
    while (processes.size () > 0 || running.size () > 0) {
    System.err.prin tln ("Start of loop...");
    // start threads if possible
    int new_threads = max_running - running.size ();
    for (int i = 0; i < new_threads && processes.size () >0; i++) {
    Thread t = (Thread) processes.getFi rst ();
    t.start ();
    running.add (t);
    processes.remov e (t);
    }
    // wait some time
    try {sleep (50);}
    catch (Exception e) {e.printStackTr ace ();}

    // remove dead threads
    Object[] threads = running.toArray ();
    for (int i = 0; i < threads.length; i++) {
    Thread t = (Thread) threads[i];
    if (! t.isAlive ()) running.remove (t);
    }
    System.err.prin tln ("End of loop...");
    }
    }
    }

    If you want to run this put it in a file test.java and start it with java
    <number of processes>. A more effecient implementation would be making the
    threads use a callback for when they have died. That is a push algorithm
    instead, making the processes tell when they are finished thereby you wont
    have to test if a thread has died again and again. But I will leave that
    to you to implement that - it is not hard.

    Best Regards
    Kristian

    Comment

    Working...