Docsity

Concurrent Programming - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Concurrent Programming - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Concurrent Programming

• This is a chapter that does a good job of

explaining the technology. I want to add a little

about the reason for the technology.

• The initial design purpose for Java was originally

aimed at embedded controllers. Quite often,

new technology tends to go in unforeseen

directions. For Java, it was the World Wide Web

that changed things.

Controller Multitasking

• It is easy to understand the need for

concurrent programming in a controller. For

example, a washing machine will have to

monitor water levels and water temperature,

time a wash cycle, release detergent, accept

water and pump it out, control the agitator,

spin the tub, and several other tasks,

frequently more than one at a time.

Reactive Systems

• One class of applications for multithreaded

systems is reactive systems that monitor a

series of inputs such as sensors and react

whenever an input indicates a change that the

system needs to respond to. The text gives an

autopilot for an airplane and a patient

monitoring system in a hospital as examples of

reactive systems.

Patient monitoring

  • I have often visited patients in a hospital when they were hooked up to a patient monitoring system. The patient had a number of sensors attached such as electrodes on the chest to monitor cardiac functioning and a small cuff on their finger to monitor pulse. Patients on respirators had monitors for carbon dioxide and breathing rates. Whenever the value of an input was abnormal, an alarm was triggered that would bring a nurse to investigate it. This allowed fewer nurses to monitor more patients effectively.

Multiprocessor Systems

• Another key use for threads is in computers

with multiple CPUs. Each CPU can operate on

a different thread to efficiently perform many

operations at the same time. I have used IBM

and NCR computers that had from 8 to 16 CPU

chips.

Process Overhead

• One of the main time demands on a server is

starting up and shutting down processes. With

multithreading, one process can service many

users, and process overhead is reduced

dramatically. This greatly increases

throughput and reduces cost.

Active Object (Thread) Diagrams

  • Extending the Thread class - Implementing the Runnable interface

Thread Runnable

MyThread MyThread run( ) run( )

Run Methods

• In both the thread class and the runnable

interface, there is an abstract method, run().

This is a hook method or hot spot for you to

add functionality to the thread. A hook

method is an object oriented device for

inheritance that uses an abstract method as a

placeholder for a later implementation.

Example: extending Thread

The Runnable Interface

• Sometimes you cannot extend thread because

you need to inherit functionality from another

class and Java does not permit multiple

inheritance.

• In this case, you can implement the runnable

interface:

public class MyThread extends AnotherClass implements runnable { public void run() { …

java.lang.Thread methods

  • start( ) A New thread enters the Alive state and starts execution
  • sleep( ) A runnable thread enters the blocked state for a timed period
  • join( ) A runnable thread enters the blocked state and waits for another thread to finish
  • yield( ) A runnable thread allows other runnable thread an opportunity to run at the same time

java.lang.Thread methods (2)

  • interrupt( ) A runnable thread sets the interrupted flag. A blocked thread is awakened and enters the runnable state, and an InterruptedException is thrown.
  • isAlive( ) Returns true if the thread is in the Alive state
  • isInterrupted( ) Returns true if the Interrupted flag is set

Atomicity

• An operation that cannot be divided is called

an atomic operation. It may be a simple

operation like assigning a value to an attribute

or a complex one like transferring money from

one account to another, where you do not

want to add the money to the receiving

account if you cannot deduct it from the

sending account.

Thread Safety

  • An important concern in concurrent systems is preventing activities from interfering with each other. With threads, this is called thread safety.
  • Usually, thread safety requires that only one activity can use a process when it is in a state where an atomic operation is not yet complete, and data may be inconsistent. The Java solution is to provide a synchronization mechanism.