Looping in main vs. making a class and a thread?

Discussion in 'Java' started by Tim Smith, Jan 12, 2008.

  1. Tim Smith

    Tim Smith Guest

    I've seen quite a few examples where main() pretty much just makes an
    instance of some class, and then creates a thread and sets the thread to
    running the run() method of the aforementioned class, and then main is
    done. The only thing of substance in the aforementioned class is the
    run() method, which does some kind of loop acquiring work to do. When
    it acquires work, it makes an instance of some second class, which
    actually does the work.

    Is there actually a good reason to have a class whose sole function is
    to run a loop in a thread, as opposed to just running that loop in
    main()?

    For example, a network server might have a class that just loops in a
    thread, accepting connections on a listening socket, and then
    instantiating some processing class for each accepted connection.
    Why not just have main() create the listening socket, and then loop
    accepting connections, doling them out to new instances of a processing
    class?

    --
    --Tim Smith
     
    Tim Smith, Jan 12, 2008
    #1
    1. Advertising

  2. Tim Smith

    Roedy Green Guest

    On Fri, 11 Jan 2008 22:54:07 -0800, Tim Smith
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Is there actually a good reason to have a class whose sole function is
    >to run a loop in a thread, as opposed to just running that loop in
    >main()?


    If you have two CPUs, one can do Swing and the other the work of the
    spun off thread. Without that, you can't get any parallelism.
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 12, 2008
    #2
    1. Advertising

  3. Roedy Green <> writes:

    > On Fri, 11 Jan 2008 22:54:07 -0800, Tim Smith
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >>Is there actually a good reason to have a class whose sole function is
    >>to run a loop in a thread, as opposed to just running that loop in
    >>main()?

    >
    > If you have two CPUs, one can do Swing and the other the work of the
    > spun off thread. Without that, you can't get any parallelism.


    That would hold too, if you run the loop in the main thread.

    I see no reason to spawn another thread to do some work, and then
    stop the current thread immediately afterwards ... unless the new
    thread is in some way different (e.g., belongs to a threadgroup
    with an uncaught exception handler or something).
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jan 12, 2008
    #3
  4. Tim Smith

    Lew Guest

    Lasse Reichstein Nielsen wrote:
    > Roedy Green <> writes:
    >
    >> On Fri, 11 Jan 2008 22:54:07 -0800, Tim Smith
    >> <> wrote, quoted or indirectly quoted
    >> someone who said :
    >>
    >>> Is there actually a good reason to have a class whose sole function is
    >>> to run a loop in a thread, as opposed to just running that loop in
    >>> main()?

    >> If you have two CPUs, one can do Swing and the other the work of the
    >> spun off thread. Without that, you can't get any parallelism.

    >
    > That would hold too, if you run the loop in the main thread.
    >
    > I see no reason to spawn another thread to do some work, and then
    > stop the current thread immediately afterwards ... unless the new
    > thread is in some way different (e.g., belongs to a threadgroup
    > with an uncaught exception handler or something).


    Most of the examples of the type the OP described that I've seen have been
    Swing programs, where the idiom is used to guarantee that Swing work is done
    on the EDT. This particular scenario requires that idiom because GUI work
    must be done on the EDT, and the EDT is not the main thread.

    --
    Lew
     
    Lew, Jan 12, 2008
    #4
  5. Tim Smith

    Mark Rafn Guest

    Tim Smith <> wrote:
    >I've seen quite a few examples where main() pretty much just makes an
    >instance of some class, and then creates a thread and sets the thread to
    >running the run() method of the aforementioned class, and then main is
    >done.


    That's my recommendation for commandline apps. I tend to do argument parsing
    and error checking in main, and pass the relevant args to the class that does
    the work.

    >Is there actually a good reason to have a class whose sole function is
    >to run a loop in a thread, as opposed to just running that loop in
    >main()?


    Flexibility. What happens when you want another program to be able to run
    that loop? If it's in a main(String[] args) method, the other program is
    going to have to give it String arguments in a command-line-like way. If it's
    a separate function, it can just be run normally.

    Most nontrivial commandline apps will eventually be useful in some sort of
    container, rather than being stuck on the commandline forever.

    Note that there's not necessarily a reason to have a separate class - as long
    as the work functionality isn't in the main() method, it can be in the same
    class. The decision of separating the class will depend on size, use cases,
    etc. I generally split the class if there's significant argument processing
    and error checking the commandline use case needs and the inside-other-app
    case doesn't.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Jan 12, 2008
    #5
  6. On 12.01.2008 20:32, Mark Rafn wrote:
    > Tim Smith <> wrote:
    >> I've seen quite a few examples where main() pretty much just makes an
    >> instance of some class, and then creates a thread and sets the thread to
    >> running the run() method of the aforementioned class, and then main is
    >> done.

    >
    > That's my recommendation for commandline apps. I tend to do argument parsing
    > and error checking in main, and pass the relevant args to the class that does
    > the work.


    For that you do not necessarily need a second thread.

    >> Is there actually a good reason to have a class whose sole function is
    >> to run a loop in a thread, as opposed to just running that loop in
    >> main()?

    >
    > Flexibility. What happens when you want another program to be able to run
    > that loop? If it's in a main(String[] args) method, the other program is
    > going to have to give it String arguments in a command-line-like way. If it's
    > a separate function, it can just be run normally.
    >
    > Most nontrivial commandline apps will eventually be useful in some sort of
    > container, rather than being stuck on the commandline forever.
    >
    > Note that there's not necessarily a reason to have a separate class - as long
    > as the work functionality isn't in the main() method, it can be in the same
    > class. The decision of separating the class will depend on size, use cases,
    > etc. I generally split the class if there's significant argument processing
    > and error checking the commandline use case needs and the inside-other-app
    > case doesn't.


    You talk about modularization, but the OP wanted to know about
    concurrency. Both concepts are orthogonal i.e. you can have one without
    the other and vice versa.

    Cheers

    robert
     
    Robert Klemme, Jan 13, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,588
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. ravi
    Replies:
    28
    Views:
    1,030
    Richard Heathfield
    Sep 26, 2007
  3. Hugo
    Replies:
    4
    Views:
    1,828
    Logan Shaw
    Mar 27, 2008
  4. Jens Peter Hansen
    Replies:
    7
    Views:
    599
    Randy Webb
    Jun 19, 2004
  5. Replies:
    5
    Views:
    307
Loading...

Share This Page