[Multi-threading] Will this code cause problem? (run & main exist in the same class)

Discussion in 'Java' started by Haitao, Jan 13, 2008.

  1. Haitao

    Haitao Guest

    Hi all,

    I just saw a code fragment written by someone as follows:

    public class MyThread extends Thread
    {
    public void run()
    {
    ... (do something)
    }

    public static void main(String[] args)
    {
    Thread t= new MyThread ();
    t.start();
    ... (do something)
    }
    }

    I am new to Java multi-threading program, and haven't seen such a coding
    style. Will putting run() and main() in the same class and executing this
    class cause some unexpected problems?

    Thanks!
    Haitao, Jan 13, 2008
    #1
    1. Advertising

  2. Haitao <> wrote:
    > I just saw a code fragment written by someone as follows:
    > public class MyThread extends Thread
    > {
    > public void run() { ... (do something) }
    > public static void main(String[] args)
    > {
    > Thread t= new MyThread (); t.start(); ... (do something)
    > }
    > }
    > I am new to Java multi-threading program, and haven't seen such a coding
    > style. Will putting run() and main() in the same class and executing this
    > class cause some unexpected problems?


    No. main and run can peacefully co-exist :)

    What types of problems did you fear? The only potential for
    unexpected problems lies in the "... (do something)"-parts :)
    Andreas Leitgeb, Jan 13, 2008
    #2
    1. Advertising

  3. Haitao

    Arne Vajhøj Guest

    Re: [Multi-threading] Will this code cause problem? (run & main existin the same class)

    Haitao wrote:
    > I just saw a code fragment written by someone as follows:
    >
    > public class MyThread extends Thread
    > {
    > public void run()
    > {
    > ... (do something)
    > }
    >
    > public static void main(String[] args)
    > {
    > Thread t= new MyThread ();
    > t.start();
    > ... (do something)
    > }
    > }
    >
    > I am new to Java multi-threading program, and haven't seen such a coding
    > style. Will putting run() and main() in the same class and executing this
    > class cause some unexpected problems?


    Not necessarily.

    Impossible to tell based on the available information.

    It depends on whether run and main access something in
    a thread unsafe manner or not.

    Arne
    Arne Vajhøj, Jan 13, 2008
    #3
    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. Y.S.
    Replies:
    3
    Views:
    985
    strajan
    Sep 17, 2003
  2. LT
    Replies:
    7
    Views:
    2,077
    Phlip
    Jul 25, 2004
  3. Replies:
    38
    Views:
    1,256
    Dennis Lee Bieber
    Feb 15, 2005
  4. akineko
    Replies:
    3
    Views:
    2,597
    Jesse Noller
    Jan 29, 2009
  5. Roedy Green
    Replies:
    10
    Views:
    419
    ClassCastException
    Jul 19, 2010
Loading...

Share This Page