Thread not starting

R

rakeshgulati

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


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


Thanks
Rakesh
 
L

Lothar Kimmeringer

rakeshgulati said:
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: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
L

Lew

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 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top