Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

[Multi-threading] Will this code cause problem? (run & main exist in the same class)

Thread view: 
Haitao - 13 Jan 2008 06:17 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.