How do Java threads make the environment asynchronous?
Answer:
The thread mechanism in Java begins with the main entry point thread, which the runtime environment creates to start a Java program.
The initial thread creates 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 the synchronized keyword and the various sleep(), wait() and notify() methods are used to more closely schedule the interaction between asynchronous threads.