Subj : Re: Concurrency Issues To : comp.programming.threads From : Eric Sosman Date : Thu Sep 29 2005 12:46 pm bernardpace@yahoo.com wrote On 09/29/05 03:39,: > Hi, > > I am writing a server and to make it concurrent, I am using threads. > > Now I have the following query: > > public void myMethod() > { > ... > } > > > main(...) > { > Thread t1 = new Thread(new ThreadStart(myMethod)); > Thread t2 = new Thread(new ThreadStart(myMethod)); > t1.Start(); t2.Start(); > } What language is this? It looks sort of Java-ish, but Java surely won't compile it. > Having this scenario, would the two threads calling the same method > conflict with each other? If the method myMethod accesses a global > variable, that would be a conflict, but in case no global variables are > being accesses would their be any conflicts between the two threads? In Java, multiple threads can run the same method code simultaneously without conflict. Conflicts arise when multiple threads try to manipulate the same object. If myMethod() modifies any object that both t1 and t2 have access to, there's a potential conflict that you must prevent by synchronizing. If myMethod() reads data from any object that another thread might be modifying, synchronization is also necessary. If all the "externally visible" objects accessed by myMethod() are immutable, you may not need to synchronize. If all the objects myMethod() touches are created within a single thread and not visible to other threads, you do not need to synchronize. That's Java. Your "Sumatra" may be different ;-) -- Eric.Sosman@sun.com .