Jvm In Multithreading

651 Words2 Pages

Java thread is an independent path of execution through programming code. If we execute multiple threads, one thread's path will differ from the rest. For instance, consider an ordinary thread executes the byte code according to if-else statement's if part, meanwhile another thread executes the byte code according to an else part. Here there is a question occuring how JVM (Java Virtual Machine) keeps track of every thread execution. The JVM gives the method called stack to each thread. In order to track the current byte code instruction, the stack method tracks the local variables, the JVM passes to a method and the method returns a value.
In case if multiple threads execute byte code instructions in the same program, this process is called multithreading. It has some specific advantages for programs :
The multithreaded GUI (Graphical User Interface) based programs are able to respond to the users while performing other tasks.
The threaded programs practially finish faster than non-threaded counterparts. It generally happens when threads run on a multiprocessor machine where every thread has its own processor.
The multithreading process is accomplished in java class called java.lang.Thread. Every Thread object acts as a single thread of execution and that execution is performed in Thread object’s run() method. It is because the default run() method actually does nothing, we have to subclass Thread and override run() method to finish the work.
Synchronization is a process of ordering a thread in time for thread access which enables multiple threads operate instance and class field variables. These sequences can be called critical code sections. Why is there a need for synchronization ? For instance, we are supposed to wri...

... middle of paper ...

...the same critical code section.
Before the technology has not developed this much, all program threads used to have their own single processor machines to run their code, but right now, as we know, modern computers have numerous of processors. So threads often share one or more processors. There is a term called thread schedulling which decides how to share the processor resources amongst program threads.
There are 2 specific things about thread schedulling that we have to mention :

1) We have to exercise care when writing a java program whose behavior depends on how threads are scheduled and must operate consistently different platforms, because java does not force VM to schedule threads.
2)When writing Java programs, we have to think about how Java schedules threads for long time. To make sure that the calculation thread does not monopolize the processor.

Open Document