Hi all,
I just saw a code fragment written by someone as follows:
public class MyThread extends Thread
{
public void run()
{
... (do something)
}
public static void main(String[] args)
{
Thread t= new MyThread ();
t.start();
... (do something)
}
}
I am new to Java multi-threading program, and haven't seen such a coding
style. Will putting run() and main() in the same class and executing this
class cause some unexpected problems?
Thanks!
Andreas Leitgeb - 13 Jan 2008 07:57 GMT
> I just saw a code fragment written by someone as follows:
> public class MyThread extends Thread
[quoted text clipped - 8 lines]
> style. Will putting run() and main() in the same class and executing this
> class cause some unexpected problems?
No. main and run can peacefully co-exist :-)
What types of problems did you fear? The only potential for
unexpected problems lies in the "... (do something)"-parts :-)
Arne Vajhøj - 13 Jan 2008 14:10 GMT
> I just saw a code fragment written by someone as follows:
>
[quoted text clipped - 16 lines]
> style. Will putting run() and main() in the same class and executing this
> class cause some unexpected problems?
Not necessarily.
Impossible to tell based on the available information.
It depends on whether run and main access something in
a thread unsafe manner or not.
Arne