Thread not starting

Discussion in 'Java' started by rakeshgulati, Nov 10, 2008.

  1. rakeshgulati

    rakeshgulati Guest

    Hi,

    I am new to Java, just trying something with multithreading, but
    thread is not getting started. Please advice


    class Systhread implements Runnable {
    int JobId;
    Thread SysJob = new Thread();

    Systhread( ) {
    System.out.println ("Starting Job Id Constructor");
    }
    void subroute (int num) {
    this.JobId = num;
    System.out.println ("Assigning Job Id" + this.JobId);
    SysJob.start();
    }
    public void run () {
    System.out.println ("launching Job" + this.JobId);
    try {
    Runtime.getRuntime().exec("some command");
    } catch (Exception e) {
    System.out.println ("Exception launching Job" + this.JobId);
    }
    }
    }

    class caller {
    public static void main (String args[]) {
    Systhread Ob = new Systhread();
    System.out.println ("Starting Job Id " + 1);
    Ob.subroute(1);
    }
    }

    output that I get is

    >>Starting Job Id Constructor
    >>Starting Job Id 1
    >>Assigning Job Id1



    I am not able to understand why SysJob.start();
    is not able to start the thread and execute statments in run proc.


    Thanks
    Rakesh
    rakeshgulati, Nov 10, 2008
    #1
    1. Advertising

  2. rakeshgulati wrote:

    > class Systhread implements Runnable {
    > int JobId;
    > Thread SysJob = new Thread();

    [...]
    > public void run () {
    > System.out.println ("launching Job" + this.JobId);
    > try {
    > Runtime.getRuntime().exec("some command");
    > } catch (Exception e) {
    > System.out.println ("Exception launching Job" + this.JobId);
    > }
    > }
    > }


    > I am not able to understand why SysJob.start();
    > is not able to start the thread and execute statments in run proc.


    SysJob.start lead to the execution of the run-method of the
    Runnable-instance that has been set to the Thread. You never
    do that.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
    Lothar Kimmeringer, Nov 10, 2008
    #2
    1. Advertising

  3. rakeshgulati

    Lew Guest

    rakeshgulati wrote:
    >> class Systhread implements Runnable {
    >> int JobId;


    Variable names by convention should start with a lower-case letter.

    >> Thread SysJob = new Thread();

    > [...]
    >> public void run () {
    >> System.out.println ("launching Job" + this.JobId);
    >> try {
    >> Runtime.getRuntime().exec("some command");
    >> } catch (Exception e) {
    >> System.out.println ("Exception launching Job" + this.JobId);
    >> }
    >> }
    >> }

    >
    >> I am not able to understand why SysJob.start();
    >> is not able to start the thread and execute statments in run proc.


    "SysJob [sic]" did start. Its run() did nothing but exit immediately, but it
    started.

    Lothar Kimmeringer wrote:
    > SysJob.start lead to the execution of the run-method of the
    > Runnable-instance that has been set to the Thread. You never
    > do that.


    More clearly, you never provide a Runnable to the Thread, so the Thread runs
    its default runnable.

    Try something like this (not an SSCCE):

    package testme;
    public class BgSyster implements Runnable
    {
    private int jobId;

    public void startJob( int job )
    {
    this.jobId = job;
    new Thread( this ).start();
    }

    public void run ()
    {
    try
    {
    Runtime.getRuntime().exec("some command");
    }
    catch (Exception e)
    {
    System.err.println( "Exception launching job " + this.JobId );
    }
    }
    }

    Notes:
    - System.out.println() is a terrible debugging tool. System.err.println() is
    marginally better.

    - The space after "job" in "launching job " will make the debug output easier
    to read.

    - Consider making 'jobId' a final instance variable set in the constructor.

    --
    Lew
    Lew, Nov 10, 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. Lenny
    Replies:
    3
    Views:
    2,427
    Lenny
    Jul 8, 2003
  2. Adrian Bezzina
    Replies:
    0
    Views:
    394
    Adrian Bezzina
    Jan 18, 2005
  3. Gürkan Demirci

    Thread not starting

    Gürkan Demirci, Sep 29, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    702
    Chris Botha
    Sep 30, 2005
  4. Richard Lamboj

    starting a thread in a nother thread

    Richard Lamboj, Jan 27, 2010, in forum: Python
    Replies:
    7
    Views:
    260
  5. Replies:
    3
    Views:
    148
Loading...

Share This Page