-
Essay / Jvm In Multithreading - 651
Java thread is an independent execution path through programming code. If we run multiple threads, the path of one thread will be different from others. For example, consider that an ordinary thread executes byte code according to the if part of the if-else statement, while another thread executes byte code according to an else part. Here the question arises how JVM (Java Virtual Machine) keeps track of each 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 local variables, the JVM passes to a method, and the method returns a value. In case multiple threads execute byte code instructions in the same program, this process is called multithreading. . It has specific advantages for programs: Multithreaded programs based on a graphical user interface are able to respond to users while performing other tasks. Threaded programs terminate virtually faster than their non-threaded counterparts. This usually happens when threads are running on a multiprocessor machine where each thread has its own processor. The multithreading process is accomplished in the Java class called java.lang.Thread. Each Thread object acts as a single thread of execution and this execution is done in the run() method of the Thread object. This is because the default run() method does nothing, we need to subclass Thread and override the run() method to complete the job. Synchronization is a process of ordering a thread in time for thread access that allows multiple threads to operate an instance and class. field variables. These sequences can be called critical code sections. Why is there a need for synchronization? For example, we are supposed to write... in the middle of a sheet of paper... the same critical section of code. Before technology developed so much, all program threads had their own single-processor machines to run their code, but nowadays, as we know, modern computers have many processors. Threads therefore often share one or more processors. There is a term called thread scheduling which decides how to share CPU resources among program threads. There are 2 specific things about thread scheduling that we need to mention: 1) We need to be careful when writing a Java program whose behavior depends on how threads are scheduled and should work consistently across different platforms because Java does not force the VM to schedule threads.2) When writing Java programs, we need to think about how Java schedules threads over a long period of time. To ensure that the calculation thread does not hog the CPU.