Monday 26 November 2012

threads in Java


Question:  How do Java threads make the environment asynchronous?

Answer:      The thread mechanism in Java begins with the main entrypoint thread the runtime environment creates to start a Java program. When you use that initial thread create secondary threads, each one runs independently of the other. The Java virtual machine manages the execution of the threads so they behave as if they all run at the same time, in fact each thread briefly takes turns at execution.
In its simplest form there may be no communication or synchronization between multiple threads in a Java program and they each run to completion independently of each other. In this respect Java threads are fundamentally asynchronous, there is no master clock that governs when threads will run and when they synchronize variables to “catch-up” with each other.
        It is often necessary and more useful if threads do check ready states before progressing, synchronize read and write access to shared variables and call-back to each other when their work is done. This is where thesynchronized keyword and the various sleep()wait() and notify()methods are used to more closely schedule the interaction between asynchronous threads.

No comments:

Post a Comment