This week's book giveaway is in the Agile and Other Processes forum. We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line! See this thread for details.
Senthil Kumar Sekar wrote:Does Main thread runs parallely with the thread being created in the same thread?
Yes. That's the whole point of multithreading.
However, that doesn't mean that both threads will be executing simultaneously. Depending on the hardware, the OS, what else is running on the system, and the threads' mix of CPU and I/O, they may run simultaneously, or they may take turns, or one may run to completion before the other one even starts. We can't predict that and we can't control it.
In your case, the main thread could look at the "b" variable before the other thread starts, after it's done, or anywhere in between. Also, since b is not declared volatile and you don't use syncing, any value the the second thread writes to b might not be visible to the main thread right away--if it reads b, it may still see the previous value.