Real-Time Systems definition of java threads, deadlocks, monitors, Synchronisation

...s not equal to null. When this has been done, run() method will be running as its own thread. A program can, and probably will have, multiple threads running on it at any one time. A problem now arises when it decides to run one of them. How does it know which one it should run? And once it has run it, how does it know what to do with it? The JVM has a scheduler built in it, which is how it solves this problem. When the CPU is executing multiple threads, it is called scheduling. Scheduling and priorities When a thread is created it is given a priority value, which it inherits from its parent thread. The priority is an integer and it ranges form 1 – 10. The priorities are pre set as these variables; MIN_PRIORITY which is set at 1 MAX_PRIORITY which is set at 10 NORM_PRIORITY which is set at 5 In order to ascertain the current value of a thread you can do so by calling the getpriority() method The scheduler determines which thread to run based on its priority value and where it is in the ReadyToRun state The thread with the highest priority becomes runnable, and will only stop running if any of the following instances occur; • A thread with a higher priority becomes available • If it yields • If its run method exits • If its time slice has expired. These techniques are discussed below. In the case that two threads have the same priority, then the scheduler will choose one of them to run in a round robin fashion, and stop running when one of the above conditions becomes true. There are two types of schedulers, non-pre-emotive, and pre-emptive. The CPU is fully pre-emptive, but the Java runtime is non-pre-emptive. Non-pre-emptive schedulers decide which thread to run, and then do this until the thread has completely run or exits. Pre-emptive schedulers run using a time-slice method. Time-slicing In addition to prioritisation, many systems implement time slicing to avoid selfish thread behaviour. Selfish thread behaviour can occur when the run method is in a tight loop or been badly implemented. The thread will be picked to run by the scheduler, but never relinquish control of the CPU. As a result the thread will only terminate when it has been pre-empted by a thread of higher priority, or it eventually exists the while loop. A time sliced system divides the CPU into time slots, and gives all of the highest and equal priority threads a certain time in which to run. The thread will continue to run throughout the time allocated, unless a thread with a higher priority pre-empts it or another one finishes. Time slicing, however, does not determine in what order, or how often threads are scheduled to run. Although this method can work, Java cannot guarantee time slicing, so when writing code it is best not to make it rely on this type of scheduling. It is better to allow the software to function under the default round robin scheduling. Round-robin scheduling Round-robin is a method used by the scheduler if different threads have the same priority value. It will basically run the thread for a certain period of time, and then move on to the next thread after it been asked to sleep, yield or wait for a lock. This diagram illustrates how Round robin scheduling works High Priority threads Low Yield This method has been mentioned a few times in this report. If a thread is well behaved, then it will use the yield () method to yield to the CPU, allowing another thread of the same priority to run. It will ignore any low priority threads. The yielding method, however, is entirely reliant on the CPU. It will only hint at the CPU that another thread should be running, the CPU will interpret this how it likes. This is dependable on the JVM implementation for the operating system. Question 3 Synchronisation Synchronisation when applied to a method or code block guarantees that at most one thread at a time executes that code. Java provides two built-in ways to synchronize access to data: with synchronized statements or synchronized methods. Synchronized statements To create a synchronized statement, you use the synchronized keyword with an expression that evaluates to an object reference. The statements contained within the synchronized block will not be executed until a lock is acquired on the current object. If instead of this reference, the expression yielded a reference to another object, the lock associated with that object would be acquired before the thread continued. Two opcodes, monitorenter and monitorexit, are used for synchronization blocks within methods. When monitorenter is encountered by the Java virtual machine, it acquires the lock for the object referred to by objectref on the stack. If the thread already owns the lock for that object, a count is incremented. Each time monitorexit is executed for the thread on the object, the count is dec...

Essay Information


Words: 1674
Pages: 6.7
Rating: None

All Papers Are For Research And Reference Purposes Only. You must cite our web site as your source.