Object-Orientated Analysis, Design and Programming

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
Multithreading Why multi-threads Defining and creating threads An example with two threads Life cycle of a thread An example with user interface Problem.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Block 3: Threads Jin Sa. Outline of block 3 Why multi-threads Defining and creating threads An example with two threads Life cycle of a thread An example.
Programming in Java; Instructor:Alok Mehta Threads1 Programming in Java Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Lecture 4 : JAVA Thread Programming
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
1 Java Threads Instructor: Mainak Chaudhuri
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
111 © 2002, Cisco Systems, Inc. All rights reserved.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
1 Web Based Programming Section 8 James King 12 August 2003.
Internet Software Development Controlling Threads Paul J Krause.
Concurrent Programming and Threads Threads Blocking a User Interface.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
Threading and Concurrency COM379T John Murray –
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Java Thread Programming
Principles of Software Development
CSCD 330 Network Programming
Multithreaded applets
Multithreading / Concurrency
Chapter 13: Multithreading
Multi Threading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
Threads and Multithreading
PA1 Discussion.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreading in Java
Chapter 19 Java Never Ends
Threads and Multithreading
Java Based Techhnology
Multithreading.
Threads and Multithreading
Threads and Multithreading
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
Threads and Multithreading
Chapter 15 Multithreading
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
CSCD 330 Network Programming
Threads.
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Object-Orientated Analysis, Design and Programming Presentation by Dr. Phil Legg Senior Lecturer Computer Science 9: Concurrency Autumn 2016

Concurrency Most of the java programs you have seen so far can only do one thing at a time. In the real world many actions are happening concurrently. Typical applications: User interfacing, e.g. One deals with the drawing, the other deals with user interface Many instances of similar behaviour, e.g. a concurrent server serves many clients Many different tasks In practice, we use Threads, to perform multiple actions simultaneously. Main program Thread 1 Thread 2

Concurrency http://docs.oracle.com/javase/tutorial/essential/concurrency/ http://www-lb.cs.umd.edu/class/spring2002/cmsc433-0101/Lectures/cpjslides.pdf http://www.javaworld.com/article/2078809/java-concurrency/java-concurrency-java-101-the-next-generation-java-concurrency-without-the-pain-part-1.html https://www3.ntu.edu.sg/home/ehchua/programming/java/j5e_multithreading.html Main program Thread 1 Thread 2

Threads in Java Extending the Thread class Implementing the Runnable interface

Extend the Thread class Define a class, e.g. NewThread, extending the Thread class Override the run() method to tell the system how the thread will be executed when it runs. Create an instance of NewThread, Invoke the start() method to tell the system to start the thread and to execute the run() method.

Thread class - Example public class SeveralThreads { public static void main(String [] args) { MyThread t1,t2; //create threads t1 = new MyThread("t1"); t2 = new MyThread("t2"); //start threads t1.start(); t2.start(); }//main }//SeveralThreads class MyThread extends Thread { private String threadID; MyThread(String s) { threadID=s; } public void run() { for (int i=0;i<200;i++){ System.out.println(“Thread ” + threadID); }//for }//run } //MyThread http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/SeveralThreads.java

Thread lifecycle A thread can be in one of these states once it is created: ready, running, blocked, finish A ready thread is runnable, but not running, it needs allocation of CPU time. Common ways for a running thread to become blocked include: waiting for I/O, sleep A common way for a thread to finish is when it completes its execution of the run() method

Runnable Interface Define a class, e.g. NewTask, implementing the Runnable interface Implement the run() method to tell the system how the task will be executed when it runs. Create an instance of NewTask, e.g. t1 The task needs to be executed in a thread. Create an instance of Thread with t1 as the parameter Invoke the start() method of the thread to tell the system to start the thread.

Runnable Interface - Example public class SeveralThreads { public static void main(String[] args) { MyTask t1 = new MyTask("t1"); MyTask t2 = new MyTask("t2"); Thread tt1=new Thread(t1); Thread tt2=new Thread(t2); tt1.start(); tt2.start(); }//main }//SeveralThreads class MyTask implements Runnable { private String threadID; MyTask(String s) { threadID=s; } public void run() { for (int i=0;i<200;i++){ System.out.println(“Thread ”+ threadID); }//for }//run } //MyTask http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/SeveralRunnableThreads.java

Runnable or Extend Thread? Use Runnable if need to extend another class Class MyTask extends AnotherClass implements Runnable If we only want to define the run() method, not using the other thread methods Decoupling the logic, i.e. print id, from the thread workhorse.

Shared resources - Issues The Account class is a bank account, contains methods such as deposit and withdraw. Cardholder is a thread allows the card holder to deposit £10 repeatedly for 10 times. FamilyAccount has a main methods that creates one instance of an Account, two instances of Cardholders, both putting money into the same account. http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/BankAccountExample.java

Shared resources - Issues An instance of Account deposit(10) deposit(10) deposit(10) Balance=50 deposit(10) deposit(10) deposit(10) deposit(10) deposit(10)

Problem with accessing shared resources – example class Account { private int balance=0; public void deposit(int amount) { //balance = balance + amount; int tmp = balance; Thread.sleep(400); balance = tmp + amount; System.out.println("Current balance is "+balance); }

Problem with accessing shared resources – example class CardHolder extends Thread{ private Account acc; private String holderName; CardHolder(Account a,String nm) { acc=a; holderName=nm; } public void run() { for (int i=0;i<10;i++) { acc.deposit(10); } }//run }//CardHolder

Problem with accessing shared resources – example class BankAccountExample { public static void main(String [] args) { Account ourAccount=new Account(); CardHolder c1 = new CardHolder(ourAccount,"john"); CardHolder c2 = new CardHolder(ourAccount,"Kate"); c1.start(); c2.start(); } }// BankAccountExample

Problem with accessing shared resources – example The result should be: Current balance is : £10 Current balance is : £20 Current balance is : £30 … Current balance is : £200 But a shared resource may be corrupted if it is accessed simultaneously by multiple threads

Problem current balance in ourAccount is £50, balance=50 The c1 thread: tries to deposit £10; tmp =50 ;The thread is suspended; The c2 thread: tries to deposit £10; tmp=50; The thread is then suspended; c1 resumes: balance=tmp+10; (50+10=60); c1 is suspended c2 resumes: balance=tmp+10; (tmp=50) i.e. balance=60 Should be 70, not 60! balance=50 balance=50 balance=50 balance=tmp+10 balance=60 balance=tmp+10 balance=60

Race condition The problem is that the two threads (c1 and c2) are accessing the same resource (account) in a way that causes a conflict. This is a common problem, known as a race condition, in multithreaded programs. The block of code that accesses and updates the shared data is a critical region. A class is said to be thread-safe if an object of the class does not cause a race condition in the presence of multiple threads. The Account class is not thread-safe at the moment.

Synchronised methods There is a need to protect the shared data by ensuring that only one thread can access the critical region. Use synchronized methods so that there can be only one thread executing this method at a time. Java guarantees once a thread has gained access to a synchronized method, it will finish the method before any other thread gains access to that or any other synchronized method in that object. A synchronised method (implicitly) acquires a lock on the instance.

Deposit as a Synchronised method class Account { int balance=0; public synchronized void deposit(int amount) { int tmp = balance; Thread.sleep(200); balance = tmp + amount; System.out.println("Current balance is "+balance); } }//Account http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/BankAccountSynchronizedExample.java

Deposit as a Synchronised method The first example shows how the shared resource is corrupted: http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/BankAccountExample.java The second example fixes this so that the shared balance is correct: http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/BankAccountSynchronizedExample.java

Multithreading for user interfaces in GUI applications Flashing Example Press start, program displays a flashing rectangle until user presses the stop button. One thread is tied to display the flashing rectangle. In order to be able to accept user’s input, need more than one thread

Implementations Two classes: FlashStop and FlashRec FlashStop deals with the graphical interface and the interaction with the user Presents gui elements (behaves as a Frame) Implements the actonPerformed method (behaves as an ActionListener) To start a thread for displaying the “flashing” To stop the thread FlasRec deals with displaying the “flashing” effect. A thread repeatedly sets the background colour to be red/white

Flash rectangle class FlashRec extends JPanel implements Runnable{ private boolean keepgoing=false; public void run() { while (keepgoing) { try { setBackground(Color.red); Thread.sleep(800); setBackground(Color.white); } catch (Exception e) {…} }//while }//run public void pleaseStop() { keepgoing=false; } public void pleaseStart(){ keepgoing=true; }//FlashRec Flash rectangle

FlashStop class public class FlashStop extends JFrame implements ActionListener { private JButton go = new JButton(“go"); private JButton stop=new JButton("stop"); private FlashRec fr; private Thread recThread; FlashStop() { setSize(400,200); fr=new FlashRec(); Container c = getContentPane(); c.add(go, "North"); go.addActionListener(this); c.add(stop,"South"); stop.addActionListener(this); c.add(fr, "Center");// }//FlashStop public void actionPerformed(ActionEvent e) { if (e.getSource() == go ) { fr.pleaseStart(); recThread=new Thread(fr); recThread.start();} if (e.getSource() ==stop) { fr.pleaseStop();} }//actionPerformed FlashStop class

Flashing Rectangle Try to run the program from the source code Can you follow to see how the Thread is used for the animation? http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/FlashingRectangle.java http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/FlashRec.java http://www.cems.uwe.ac.uk/~pa-legg/teaching/ooadp/code/week9/FlashStop.java

Summary Understand how to define and create threads in Java: extends Thread and implement Runnable Be able to write simple Java thread applications Problem and solution with concurrent thread Try the example programs – take the time to study these and make sure that you understand how these work.