0% found this document useful (0 votes)
9 views9 pages

PPL Unit-4

The document discusses concurrency in various functional programming languages, including Multilisp, Concurrent ML, and F#, highlighting their unique constructs for managing concurrent execution. It also covers statement-level concurrency in High-Performance Fortran and introduces event handling in Java and C#, explaining the event model and how event handlers work in GUI applications. Key concepts include the use of threads, message passing, and event listeners to create responsive applications.

Uploaded by

upender
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

PPL Unit-4

The document discusses concurrency in various functional programming languages, including Multilisp, Concurrent ML, and F#, highlighting their unique constructs for managing concurrent execution. It also covers statement-level concurrency in High-Performance Fortran and introduces event handling in Java and C#, explaining the event model and how event handlers work in GUI applications. Key concepts include the use of threads, message passing, and event listeners to create responsive applications.

Uploaded by

upender
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CONCURRENCY IN FUNCTIONAL LANGUAGES

The concurrency in several functional programming languages are


Multilisp:
 Multilisp (Halstead, 1985) is an extension to Scheme that allows the
programmer to specify program parts that can be executed
concurrently. These forms of concurrency are implicit; the programmer
is simply telling the compiler (or interpreter) some parts of the
program that can be run concurrently.
 One of the ways a programmer can tell the system about possible
concurrency is the pcall construct. If a function call is embedded in
a pcall construct, the parameters to the function can be evaluated
concurrently. For example, consider the following pcall construct:
(pcall f a b c d)
 The function is f, with parameters a, b, c, and d. The effect of pcall is
that the parameters of the function can be evaluated concurrently.
Unfortunately, whether this process can be safely used, that is, without
affecting the semantics of the function evaluation, is the responsibility
of the programmer.
 However, Multilisp does allow some side effects. If the function was not
written to avoid side effects, it may be difficult for the programmer to
determine whether pcall can be safely used. The future construct of
Multilisp is a more interesting and potentially more productive source
of concurrency. As with pcall, a function call is wrapped in
a future construct.

Concurrent ML:
 Concurrent ML (CML) is an extension to ML that includes a form of
threads and a form of synchronous message passing to support
concurrency.
 A thread is created in CML with the spawn primitive, which takes the
function as its parameter. In many cases, the function is specified as an
anonymous function.
 As soon as the thread is created, the function begins its execution in
the new thread. The return value of the function is discarded. The
effects of the function are either output produced or through
communications with other threads.
 Channels provide the means of communicating between threads. A
channel is created with the channel constructor. For example, the
following statement creates a channel of arbitrary type
named mychannel:
let val mychannel = channel()
 The two primary operations (functions) on channels are for sending
(send) and receiving (recv) messages.
 The type of the message is inferred from the send operation. The send
function that sends the integer value 7, can be written as
send(mychannel, 7)

 The recv function names the channel as its parameter. Its return value
is the value it received.
 In the Concurrent ML communications are synchronous. That means
the message is both sent and received only if both the sender and the
receiver are ready.
 If a thread sends a message on a channel and no other thread is ready
to receive on that channel, the sender is blocked and waits for another
thread to execute a recv on the channel.
 if a recv is executed on a channel by a thread but no other thread has
sent a message on that channel, the thread that ran the recv is
blocked and waits for a message on that channel.
F#:
 F# support for concurrency is based on the same .NET classes that are
used by C#, specifically System.Threading.Thread. For example,
suppose we want to run the function myConMethod in its own thread.
The following function, when called, will create the thread and start the
execution of the function in the new thread:

let createThread() = let newThread


= new Thread(myConMethod)newThread.Start()
 In F#, if a function expects a delegate as its parameter, a lambda
expression or a function can be sent and the compiler will behave as if
you sent the delegate. So, in the above code, the
function myConMethod is sent as the parameter to
the Thread constructor, but what is actually sent is a new instance
of ThreadStart.
 The Thread class defines the Sleep method, which puts the thread from
which it is called to sleep for the number of milliseconds that is sent to
it as a parameter.
 A mutable heap-allocated variable is of type ref. For example, the
following declaration creates such a variable named sum with the
initial value of 0:
let sum = ref 0
 A ref type variable can be changed in a lambda expression that uses
the ALGOL/Pascal/Ada assignment operator, :=. The ref variable must
be prefixed with an exclamation point (!) to get its value. In the
following, the mutable variable sum is locked while the lambda
expression adds the value of x to it:
lock(sum) (fun () -> sum := !sum + x)
 Threads can be called asynchronously, just as with C#, using the same
subprograms, BeginInvoke and EndInvoke, as well as
the IAsyncResult interface to facilitate the determination of the
completion of the execution of the asynchronously called thread.

STATEMENT-LEVEL CONCURRENCY
The statement level concurrency provides a mechanism that the
programmer can use to inform the compiler of ways it can map the program
onto a multiprocessor architecture.
High-Performance Fortran:
 High-Performance Fortran (HPF) is a collection of extensions to Fortran
90 that are meant to allow programmers to specify information to the
compiler to help it optimize the execution of programs on
multiprocessor computers.
 The primary specification statements of HPF are for specifying the
number of processors, the distribution of data over the memories of
those processors, and the alignment of data with other data in terms of
memory placement.
 The HPF specification statements appear as special comments in a
Fortran program. Each of them is introduced by the prefix !HPF$,
where ! is the character used to begin lines of comments in Fortran 90.
This prefix makes them invisible to Fortran 90 compilers but easy for
HPF compilers to recognize.
 The PROCESSORS specification has the following form:
!HPF$ PROCESSORS procs (n)
 This statement is used to specify to the compiler the number of
processors that can be used by the code generated for this program.
 The DISTRIBUTE statement specifies what data are to be distributed
and the kind of distribution that is to be used. Its form is as follows:
!HPF$ DISTRIBUTE (kind) ONTO
procs :: identifier_list
 The FORALL statement specifies a sequence of assignment statements
that may be executed concurrently. For example,
FORALL (index = 1:1000)
list_1(index) = list_2(index)
END FORALL
 C# 4.0 (and the other .NET languages) include two methods that
behave somewhat like FORALL. They are loop control statements in
which the iterations can be unrolled and the bodies executed
concurrently. These are Parallel.For and Parallel.ForEach.

INTRODUCTION TO EVENT HANDLING


 An event is a notification that something specific has occurred, such
as a mouse click on a graphical button.
 An event handler is a segment of code that is executed in response
to the appearance of an event. Event handlers enable a program to be
responsive to user actions.
 Event handling is similar to exception handling. In both cases, the
handlers are implicitly called by the occurrence of something, either an
exception or an event.

EVENT HANDLING WITH JAVA


In addition to Web applications, non-Web Java applications can present GUIs
to users. GUI components in java collectively called Swing.
Java Swing GUI Components:
 Swing creates highly interactive GUI applications.
 It is the most flexible and robust approach.
 The Swing collection of classes and interfaces, defined in javax.swing,
includes GUI components, or widgets. Because our interest here is event
handling, not GUI components, we discuss only two kinds of widgets: text
boxes and radio buttons.
 A text box is an object of class JTextField. The simplest JTextField constructor
takes a single parameter, the length of the box in characters. For example,
JTextField name = new JTextField(32);
The JTextField constructor can also take a literal string as an
optional first parameter.
 Radio buttons are special buttons that are placed in a button group
container. A button group is an object of class ButtonGroup, whose
constructor takes no parameters. The JRadioButton constructor, used
for creating radio buttons, takes two parameters: a label and the initial
state of the radio button.
 After the radio buttons are created, they are placed in their button
group with the add method of the group object. Consider the following
example:
ButtonGroup payment = new ButtonGroup();
JRadioButton box1 = new JRadioButton("Visa", true);
JRadioButton box2 = new JRadioButton("Master Charge");
JRadioButton box3 = new JRadioButton("Discover");
payment.add(box1);
payment.add(box2);
payment.add(box3);
 A JFrame object is a frame, which is displayed as a separate window.
The JFrame class defines the data and methods that are needed for
frames. So, a class that uses a frame can be a subclass of JFrame.
A JFrame has several layers, called panes.
 Predefined graphic objects, such as GUI components, are placed
directly in a panel. The following creates the panel object used in the
following discussion of components:
JPanel myPanel = new JPanel();
 After the components have been created with constructors, they are
placed in the panel with the add method, as in
myPanel.add(button1);
 In order to display any JComponent on the GUI, it is necessary to add
this component to the container first. If you do not add these
components to container then it will not be displayed on the GUI.
 There are two important features of swing components - Firstly, all the
component classes begin with the letter J and secondly all these GUI
components are descendant of JComponent class.

The Java Event Model:


 When a user interacts with a GUI component, for example by clicking a
button, the component creates an event object and calls an event
handler through an object called an event listener, passing the event
object.
 The event model is based on the Event Source and Event Listeners.
 Event Listener is an object that receives the messages/events.
 The Event Source is any object which creates the message / event.
 The Event Delegation model is based on - The Event Classes, The
Event Listeners, Event Objects.
 There are three participants in event delegation model in Java –
1. Event Source - the class which broadcasts the events.
2. Event Listeners - the classes which receive notifications of events
3. Event Object - the class object which describes the event.

 One class of events is ItemEvent, which is associated with the event of


clicking a checkbox, a radio button, or a list item.
 The ItemListener interface prescribes a method, itemStateChanged,
which is a handler for ItemEvent events. The listener is created with
addItemListener.
 radio button named button1 in a panel named myPanel that
implements the ItemEvent event handler for buttons, we would
register the listener with the following statement:
button1.addItemListener(this);
 Each event handler method receives an event parameter, which
provides information about the event.
 All the event-related classes are in the java.awt.event package, so it is
imported to any class that uses events.
 Example program of event handling in java.
Program Explanation
In above program,

1. When a button is clicked the action of changing the background color


occurs. Hence this program must implement the ActionListener
interface.
2. This listener class must have a method called actionPerformed which
has a parameter an object- ActionEvent.
3. By invoking the method getActionCommand we can recognize that
the event has occurred by clicking the button.

EVENT HANDLING IN C#
 In event handler, in C#, is a method that contains the code that gets
executed in response to a specific event that occurs in an application.
 Event handlers are used in Graphical User interface. (GUI) applications
to handle events such as button clicks and menu selections, raised by
controls in the user interface.
 The C# event model is based on a "publish- subscribe" pattern in
which a class (publisher) triggers an event, while another class
(subscriber) receives that event.
 Delegate: An event is nothing but an encapsulated delegate. We can
declare the delegate as shown below:

public delegate void someEvent();


public someEvent some Event;

 . Declare: To declare an event, use the event keyword before declaring


a variable of delegate type, as below: public delegate void
someEvent();

public event someEvent someEvent;

 Using Windows Forms, a C# application that constructs a GUI is


created by subclassing the Form predefined class, which is defined in
the System.Windows
 Text can be placed in a Label object and radio buttons are objects of
the RadioButton class. The size of a Label object is not explicitly
specified in the constructor; rather it can be specified by setting
the AutoSize data member of the Label object to true, which sets the
size according to what is placed in it. the following code creates a radio
button with the label Plain at the (100, 300) position in the output
window:
private RadioButton plain = new RadioButton();
plain.Location = new Point(100, 300);
plain.Text = "Plain";
Controls.Add(plain);
 Example program of event handling in C#.

You might also like