problem using thread

M

manoj

here r 2 simple programs i have written

/////////progaram1//////////////////////////
public class db2 implements Runnable
{
private Thread dbThread ;
db2( String ipAddress )
{
System.out.println( "Got here1" ) ;
dbThread = new Thread( this , ipAddress ) ;
dbThread.start() ;
System.out.println( "Got here2" ) ;
}


public void run( )
{
System.out.println( "Got here3" ) ;
}
}
/////progarm 1 ends/////////////////////////////////

//////////////////program2///////////////
public class db extends db2 implements Runnable
{
private Thread dbThread ;
db()
{
super( "10.100.250.1" ) ;

dbThread = new Thread( this , "10.100.250.1" ) ;
dbThread.start() ;
}

public void run()
{
System.out.println( "In this run" ) ;
System.out.println( "in thread 2" ) ;
}

public static void main( String args[] )
{
db DB = new db() ;
}
}
/////program2 ends/////////////////////////
when i run "db" ie program2 the output is
//output/////////////////////
Got here1
Got here2
In this run
in thread 2
In this run
in thread 2
/////output ends////////////////

can anyone explain the output
can anyone explain why i am not getting "got here3" in program1
thanku
 
B

Bjorn Abelli

...

[code snipped to a minimum for explanation]
public class db2 implements Runnable
{ [snip]
public void run( )
{
System.out.println( "Got here3" ) ;
}
}
public class db extends db2 implements Runnable
{ [snip]
public void run()
{
System.out.println( "In this run" ) ;
System.out.println( "in thread 2" ) ;
}
}
can anyone explain why i am not getting "got here3"
in program1 thanku

Simply because the method "run" implemented in "db2" never executes.

It's because of the polymorphic behaviour in Java, where we can "override"
methods in superclasses with new implementations in the subclasses.

As you make an instance of db, the call to "run" will only execute the
implementation of "run" in db, not the implementation in db2, as it's an
instance of db.

If you want to include the execution of db2.run, you need to make an
explicit call to that method in the superclass, e.g.:

// In class db:

public void run()
{
super.run(); // <--

System.out.println( "In this run" ) ;
System.out.println( "in thread 2" ) ;
}

// Bjorn A
 
R

Ryan Stewart

Bjorn Abelli said:
Simply because the method "run" implemented in "db2" never executes.

It's because of the polymorphic behaviour in Java, where we can "override"
methods in superclasses with new implementations in the subclasses.

As you make an instance of db, the call to "run" will only execute the
implementation of "run" in db, not the implementation in db2, as it's an
instance of db.
To be more specific, when you construct a Thread in the db2 constructor and pass
it "this", consider what "this" is a reference to. It is not a reference to a
db2 object. It is a reference to a db object.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top