Download presentation
Presentation is loading. Please wait.
Published bySherman Palmer Modified over 9 years ago
1
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and Real Time programming Completing the Java concurrency model
2
Instructore: Tasneem Darwish2 Outlines Thread Priorities and Thread Scheduling Delaying Threads Thread Groups Java event handling The Process and Runtime Classes Thread-related Exceptions Strengths and Limitations of the Java Concurrency Model
3
Instructore: Tasneem Darwish3 Thread Priorities and Thread Scheduling Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler when allocating resources
4
Instructore: Tasneem Darwish4 Thread Priorities and Thread Scheduling Java allows a thread's priority to be queried and set via the getPriority and setPriority methods. By default, a thread has the same priority as its parent. If the programmer has not allocated a priority to any thread, then all threads are given the NORM_PRIORITY the Java thread that executes the main method runs with a NORM_PRIORITY
5
Instructore: Tasneem Darwish5 Thread Priorities and Thread Scheduling From a real-time perspective, Java's scheduling and priority models are weak; in particular: 1.no guarantee is given that the highest priority runnable thread is always executing. 2.equal priority threads may or may not be time sliced; and 3.different Java priorities may be mapped to the same operating system priority. Strengthening the scheduling and priority models is a key aim of the RTSJ (Chapter 10).
6
Instructore: Tasneem Darwish6 Delaying Threads A relative delay allows a thread to wait for a future time It can be implemented by using one of the two sleep static methods (defined in the Thread class). Sleep only guarantees that the thread is released (made executable) after the period has expired Once the thread has been made runnable, it will then have to wait to be scheduled for execution. The length of the waiting time will depend on the other runnable threads in the system and their relative priorities
7
Instructore: Tasneem Darwish7 Delaying Threads A relative delay allows a thread to wait for a future time Java's sleep mechanisms support relative delays, but there is no support for an absolute delay. Absolute delay is to ask the JVM to delay a thread until 2 p.m. on January 31, 2010 (for example).
8
Instructore: Tasneem Darwish8 Delaying Threads A delay example using sleep consider a city's traffic light control system. In the morning "rush hour", say between 7 and 10 a.m., the controllers need to give more time to traffic entering the city centre than to traffic leaving the city centre. Between the 4 and 7 p.m. "rush hour", the situation is reversed.
9
Instructore: Tasneem Darwish9 Delaying Threads Suppose that the following class contains the appropriate duration values:
10
Instructore: Tasneem Darwish10 Delaying Threads
11
Instructore: Tasneem Darwish11 Delaying Threads
12
Instructore: Tasneem Darwish12 Delaying Threads There are two problems with this approach: 1. no attempt has been made to take into account the time taken to execute the setInboundTimes and setOutboundTimes method calls; 2.The thread may be preempted between finishing the calls to set the durations and the call to the next sleep method. Support for absolute delays is another key extension provided by the RTSJ (Chapter 7).
13
Instructore: Tasneem Darwish13 Delaying Threads a thread usually wait for an arbitrarily long period of time within a synchronized method for an associated notify there are occasions when the absence of the notify, within a specified period of time, requires that the thread take some alternative action This is achieved by using the wait method with a specified timeout
14
Instructore: Tasneem Darwish14 Delaying Threads the timeout is a relative time it is not possible to know for certain if the thread has been woken by the timeout expiring or by a notify
15
Instructore: Tasneem Darwish15 Thread groups Thread groups allow collections of threads to be grouped together and manipulated as a group rather than as individuals. Thread groups also provide a means of restricting who does what to which thread. Every thread in Java is a member of a thread group. There is a default group associated with the main program, all created threads are placed in this group (by default). ThreadGroup is the class which supports the thread groups in Java
16
Instructore: Tasneem Darwish16 Thread groups When a thread creates a new thread group, it does so from within a thread group. the new thread group is a child of the current thread group unless a different thread group is passed as a parameter to the constructor. Using the constructor methods in the ThreadGroup class allows hierarchies of thread groups to be created. Requests to destroy a thread group will be applied to all threads in the group.
17
Instructore: Tasneem Darwish17 Thread groups Setting the maximum priority of a thread group will cause the setPriority method in the Thread class to silently truncate the priorities if they are above the maximum value for the group.
18
Instructore: Tasneem Darwish18 Java event handling In Java, threads are the entities that are scheduled by the virtual machine there are entities scheduled for execution and have an imposed ordering. User-interface events are good examples for entities that have imposed ordering. the events are executed by a single thread that imposes the required ordering. The classes Timer and TimerTask are used for this style of programming.
19
Instructore: Tasneem Darwish19 Java event handling Timer object is an execution engine The Timer accepts requests to schedule tasks for execution After scheduling the Timer executes these tasks sequentially in an order determined by parameters passed with the schedule request. A task is an instance of any subclass of the TimerTask
20
Instructore: Tasneem Darwish20 The process and runtime classes Java virtual machine executes under control of an operating system. all modern operating systems support the concept of a process Java allows the programmer to create and interact with other processes under that host operating system. this interaction is heavily dependent on the actual host (the O.S) and consequently use of the facilities might reduce the portability of the Java program.
21
Instructore: Tasneem Darwish21 The process and runtime classes Java defines two classes to aid interaction with other processes: 1.the java.lang.Process class 2.the java.lang.Runtime class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.