|
Hi all,
I am new to this topic and would like to consult any experts on this. I have a code where it shows
"
......
new Thread ( new Runnable()
{
public void run()
{
// boring coding }
} ).start();
........
// further down the code I have
....
Thread test = new Thread();
test.start();
public void run()
{
//runs test code
}
........
"
When it comes to the portion where class test is instianted, the function run() does not seem to run. Does anyone has thoughts on this?
Thanks
|
|
|
Does your class have:
class CLASS_NAME implements Runnable {
}
Give that a try!
|
|
|
Thread test = new Thread();
test.start();
|
|
The test Thread has no Runnable to run so it does nothing. In your first thread you had an inner class Runnable. To get this test Thread to run you can do as mmarab suggests
class Pseudo implements Runnable
{
...
Thread test = new Thread(this);
test.start();
...
/** Runnable (single method) implementation */
public void run()
{
//runs test code
}
}
|
|
or you can do
class Pseudo
{
...
Thread test = new Thread(runner);
test.start();
...
private Runnable runner = new Runnable()
{
public void run()
{
//runs test code
}
};
}
|
|
If there is no Runnable then there will not be a run method to run.
|
|
|
Hi all,
Thanks for the reply. I will give a try.
Merry Xmas y'll.
|
|
|
Hi all,
I've found a little tweak. It works when I add 'this' to Thread xxx = new Thread(this).
|
|
|
Yes the use of 'this' would refer to the object in which you wanted the thread for. so in your case the class.
Nice One.
Merry Xmas.
|
|
|