Chapter 18: Multithreaded Object-Oriented Programming


OVERVIEW

Unlike C++, Java has support for multithreading built right into the language. However, even with C++, it is possible to write multithreaded programs by using external thread classes, as we will show in this chapter.

Multithreading facilitates the design of more responsive graphical interfaces, especially when they are intended for multimedia applications. Imagine you are downloading a long video or audio clip over the internet. If the download could be implemented in a separate thread, you would be able to start viewing the clip soon after downloading starts, as opposed to having to wait until the end. For another example, multiple threads of execution could allow many users to simultaneously access a common database, something that could be very useful for applications such as inventory management and airline reservation systems. To prevent corruption of data when multiple users are authorized to read and write into a common database, multithreading would need to allow for locks to be placed on objects so that only one thread modifies the state of an object at a time.

To the reasons we have listed above for multithreading, a reader might say how about using multiple processes for achieving similar results. The distinction between multiple processes running concurrently and multiple threads running concurrently is crucial. A thread of execution takes place within a process. While a process typically requires its own address space, multiple threads of execution share the same address space, which is the address space of the process in which they reside. This permits multiple threads to more easily access the same data in the memory. Also, the time it takes to switch between threads is shorter than the time it takes to switch between processes. Additionally, interthread communication is faster than interprocess communication.[1]

In a multithreaded program, different priority levels can be attached to different threads. But how these priority levels are actually taken into account varies considerably from system to system. Ordinarily, the highest priority thread is selected for execution. Such a thread continues running until a threadof higher priority seeks attention. However, in systems that use preemptive scheduling, the threads are timesliced so that every thread gets a chance at the processor. When timesliced, equal-priority threads get scheduled for execution in a round-robin fashion. An alternative way of dealing with threads of equal priority consists of letting a thread run to completion, unless the execution is preempted by a higher priority thread seeking time on the processor (in what may be referred to as priority-preemptive scheduling) or by a sleep or a wait command.

The performance of a multithreadable programming language depends ultimately on how the threads are mapped to the native threads of the underlying operating system. This mapping, kept mostly hidden from the application programmer, can vary from one implementation to another of a programming language. For example, for the case of Java, some implementations of the Java Virtual Machine use what are known as green threads,[2] which are all executed in a single native thread of the operating system. Other implementations of JVM may use directly the native threads, which allows Java threads to execute in parallel on multiprocessor machines. Yet other implementations may map n Java threads to m native threads dynamically at the discretion of the scheduler. In Linux, each Java thread may be mapped to a separate process of the operating system.

This chapter first uses Java to introduce the vocabulary of multithreading. This is followed by an introduction to POSIX threads. It is necessary to understand POSIX threads because they can be used directly in C++ programs and because they are important for gaining a full understanding of the C++-based thread classes. The Java part of this chapter also discusses the Event Dispatch Thread in Swing and how multithreading can be used to design animated applets.

[1]According to Stevens [53], thread creation can be 10 to 100 times faster than process creation.

[2]Apparently, green in green threads dates back to the Sun's Green Project, whose goal was to implement Java in a hand-held computer based on the Spare chip (source: John Brewer at jGuru.com).




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net