Thread Question

Hi,

I have a question regarding threads.

publicclass Aimplements Runnable{

Thread t;

public A()

{

//dont want to start thread in constructor

}

publicvoid methodtostartthread()

{

t=new Thread(this);

t.start();

}

publicvoid run()

{

// do something

}

}

class B{

A m_oA;

public B(){

m_oA=new A();

}

publicvoid statrt()

{

A.methodtostartthread();

}

}

Will the below statement start a new Thread in A ?

"t=new Thread(this);"

Or is there a better way to start a new Thread from a method;

thanks

@debug

[1661 byte] By [@debuga] at [2007-11-27 6:19:29]
# 1

hi...

A.methodtostartthread(); wont starts because class A is not static...

m_oA.methodtostartthread(); will....

another method is

public class A implements Runnable{

public A()

{

//dont want to start thread in constructor

}

public void run()

{

// do something

}

}

class B{

A m_oA;

Thread t;

public B(){

m_oA=new A();

}

public void statrt()

{

t = new Thread(m_oA);

t.start();

}

}

faheda at 2007-7-12 17:34:08 > top of Java-index,Java Essentials,New To Java...
# 2

>

> Will the below statement start a new Thread in A ?

>

> "t=new Thread(this);"

>

No, it will create a new instance of Thread, passing the instance of the class creating the Thread instance as a Runnable.

It does not start that Thread, you have to do that explicitly by calling its start() method.

jwentinga at 2007-7-12 17:34:09 > top of Java-index,Java Essentials,New To Java...