Concurrency in First Year

cavalcadejewelΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

64 εμφανίσεις

Concurrency in First Year

Judith Bishop


University of Pretoria

South Africa

www.cs.up.ac.za/~jbishop

Background


500 students

in Science, Maths, Engineering,
Commerce and Arts


Prior intro

to programming at K12 level or a one
semester Pascal or C course


Java

introduced in 1998 as
lingua franca



Semester 1



concentrates on oops, event
-
driven
programming, applets and
URL resources


Semester 2



concentrates on
multithreading
,
sockets
, (plus computer “know how”, data structures
and algorithms)

What kind of concurrency?


Multithreading


User interfacing



e.g. one thread watches the buttons, one does the animation


Many instances


e.g. viewing multiple yet independent copies of animations


Networking



Access to URL resources


primarily for images


Inter
-
socket connections


message passing

Animation and multiple threads example

Program structure

Frame
Traffic1
Panel
constructor
SetOfLights
Gr aphics
buttons
close
main
Action
action
Performed
Thread
Start
run

area

x

draw
new set
lights
The run method


public void run() {


while (true) {


for

(int light = 0; light < 3; light++) {


if (light == red & walk) {


walkOn = false;


for (int i = 0; i < 11; i++) {


draw
(light);


try {
sleep
(time[3]);}


catch (InterruptedException e) { }


walkOn = !walkOn;


}


walk = false;


} else {


draw
(light);


try {
sleep
(time[light]); }


catch (InterruptedException e) { }


}


}


}


}

ActionPerformed


public void actionPerformed(ActionEvent e) {


Object event = e.getSource();


if (event == newSetButton) {


lights[nLights] =


new SetOfLights
(area, lightsPosition);


lights[nLights].
start
();


lightsPosition += lightsWidth;


nLights++;


if (nLights == 3)


newSetButton.setEnabled(false);




Each time the
NewSet
button is pressed, a new
thread is created.


The thread references are stored in an array.

The Mars Lander example

The run method


public void run () {


while (calc.s > 0 && !endthread) {


try {
sleep

(500);


calc.
integrate

();


draw
();


}


catch (InterruptedException e) {}


}


if (!endthread)


if (calc.v <
-
10)


drawEnd(crasher,


"You have crash
-
landed on Mars");


else


drawEnd(lander, "Congratulations: “+



“you have landed safely on Mars");


}



A URL resource example

A Socket example


ATM machine


ServerSocket listener = new ServerSocket(port);


int c = 0;


while (!done) {


// This is where the program waits


Socket client = listener.accept( );


c ++;


System.out.println("Card inserted on " +


client.getInetAddress().getHostName());


System.out.println("Starting a new “+


“client, numbered "+c);


new handler(client, c).start();


}


listener.close();


A typical exam question

c)

Write a method called
readPicture

which
gets a URL name from a text field and tries to return
the image at that location for three tries, after which
it returns a null image reference instead. (Hint: the
statement to get an image is:


f.createImage((ImageProducer)


imagename.getContent());


and it throws an
ImageException

if
unsuccessful).


(4)


Concurrency in Java


comes in several guises (and levels)


is easy at the non
-
synchronised levels


enables students to create wonderful animations and
interactive programs with little fuss


Conclusion


We see threads as no more complex or less essential
than than
event listeners



We bring them into
first year

as and when needed by
the applications being discussed