Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrency in First Year Judith Bishop University of Pretoria South Africa www.cs.up.ac.za/~jbishop.

Similar presentations


Presentation on theme: "Concurrency in First Year Judith Bishop University of Pretoria South Africa www.cs.up.ac.za/~jbishop."— Presentation transcript:

1 Concurrency in First Year Judith Bishop University of Pretoria South Africa www.cs.up.ac.za/~jbishop

2 Background n 500 students in Science, Maths, Engineering, Commerce and Arts n Prior intro to programming at K12 level or a one semester Pascal or C course n Java introduced in 1998 as lingua franca n Semester 1 – concentrates on oops, event-driven programming, applets and URL resources n Semester 2 – concentrates on multithreading, sockets, (plus computer “know how”, data structures and algorithms)

3 What kind of concurrency? n Multithreading n User interfacing –e.g. one thread watches the buttons, one does the animation n Many instances –e.g. viewing multiple yet independent copies of animations n Networking n Access to URL resources –primarily for images n Inter-socket connections –message passing

4 Animation and multiple threads example

5 Program structure

6 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) { } }

7 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.

8 The Mars Lander example

9 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"); }

10 A URL resource example

11 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();

12 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)

13 Concurrency in Java n comes in several guises (and levels) n is easy at the non-synchronised levels n enables students to create wonderful animations and interactive programs with little fuss Conclusion n We see threads as no more complex or less essential than than event listeners n We bring them into first year as and when needed by the applications being discussed


Download ppt "Concurrency in First Year Judith Bishop University of Pretoria South Africa www.cs.up.ac.za/~jbishop."

Similar presentations


Ads by Google