Download presentation
Presentation is loading. Please wait.
Published byPhilippa Hodges Modified over 6 years ago
1
Object-Orientated Analysis, Design and Programming
Presentation by Dr. Phil Legg Senior Lecturer Computer Science 9: Concurrency Autumn 2016
2
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
3
Concurrency Main program Thread 1 Thread 2
4
Threads in Java Extending the Thread class
Implementing the Runnable interface
5
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.
6
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
7
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
8
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.
9
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
10
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.
11
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.
12
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)
13
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); }
14
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
15
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
16
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
17
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
18
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.
19
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.
20
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
21
Deposit as a Synchronised method
The first example shows how the shared resource is corrupted: The second example fixes this so that the shared balance is correct:
22
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
23
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
24
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
25
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
26
Flashing Rectangle Try to run the program from the source code
Can you follow to see how the Thread is used for the animation?
27
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.