Concurrent Programming and Threads Threads Blocking a User Interface.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Graphical User Interfaces (Part IV)
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Graphic User Interfaces Layout Managers Event Handling.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CS 280 Data Structures Professor John Peterson. Quiz 4 Recap Consider the following array: {2, 6, 7, 3, 4, 1, 5, 9}. Draw this in tree form and then show.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Object-Oriented Analysis and Design
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Creating GUIs in Java using Swing David Meredith Aalborg University.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Welcome to CIS 083 ! Events CIS 068.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
CS12420 – Swing and threads Lynda Thomas
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
C20: Threads see also: ThreadedBallWorld, DropTest, Tetris source examples Not covered: advanced stuff like notify/notifyAll.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
UID – Event Handling and Listeners Boriana Koleva
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
SurfaceView.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Singleton Pattern Command Pattern
Lecture 28 Concurrent, Responsive GUIs
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Event loops.
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Event loops.
CMSC 202 Threads.
Event loops 19-Aug-19.
Presentation transcript:

Concurrent Programming and Threads Threads Blocking a User Interface

Concurrent Programming concurrent = in parallel or at the same time Concurrent programming –multiple operations can be performed at the same time –Only computers with multiple processors can execute operations concurrently –Single processor computers ‘simulate’ concurrency –(Not be mixed up with concurrency in databases) Java provides multithreading as part of the language –facilitates concurrent programming

Multithreading Application = multiple threads Portion of the application assigned to each thread Threads may run concurrently –each gets a time slice of CPU time Example: Java’s garbage collector

Multithreading Task 1 Task 2 Tasks3 Application Single Threaded Application CPU time Multi-Threaded Application CPU time

In a Java application… main Almost all code executes in the main thread event For GUI applications an event thread is also created and runs in parallel with main thread Event thread uses an event fifo queue. Processing an event means calling the appropriate event handler. events mouse click button press paint menu item select button press

Blocking a user interface JFrame Window –single JLabel (msg) in content pane –“Start” menu item –menu item event handler performs a lengthy job loops 10 times sleeps for a second inside each loop text of msg label is updated in each loop output also sent to console Display some text here.. Menu

// set up content pane with single label private JPanel createContentPane() { // set up the message label with “Display text here” etc } // set up menu private JMenuBar createMenuBar(){ JMenuBar bar = new JMenuBar(); JMenu menu = new JMenu("Menu"); // set up the “start” menu item // and assign the listener etc Etc } // event handler for menu item public void actionPerformed(ActionEvent e) { //set up the message test to appear on the screen to say “starting..” // initiate some “slow “processing } JFrame class

// Method to perform work that takes a while private void performLengthyWork() { { // set up “slow” loop processing and get thread to sleep.. Printing out a message on each loop both to the console AND to the screen } msg.setText("Done...."); }

Expect to see? Display some text here.. Starting… Working1 Working2 etc.. done

But actually… Display some text here.. Done Start Menu GUI Blocked for 5 seconds… then (The Console updates though…)

Console Running in thread main Working 0 Running in thread AWT-Event Querue-0 Working 2 Running in thread AWT-Event Querue-0 Working 3 Running in thread AWT-Event Querue-0 etc

Blocking Window All code is executing on the event thread so has to wait until the “menu item select” event has finished Select “Start” Paint opened menu Click on “Menu”” paint menu paint label paint label invokes the actionPerformed method which performs the lengthy processing But the “paint” method to redraw the screen with the retracted menu goes next.. So open menu is left on the screen waiting for Lengthy work to finish.. Paint closed menu Repaint label Repaint label

Blocking window When menu is clicked, command to repaint the GUI with the opened out menu added to event queue. –Repaint executes.. And menu item “start” appears When menu item Start selected – call to Action Performed is added to event queue Command to redraw the GUI with the closed menu is added to the queue behind the call to action Performed.. For each “for” loop, a command to draw the label with updated text is added to the queue behind the repaint command…

Non Blocking Window Put lengthy processing in a separate thread Click on “Menu” paint menu Select “Start” paint menu paint label paint label starts a new Thread running to perform lengthy processing – no need to wait till it finishes

Thread Class useful instance methods –start() – starts the execution of the thread, must be invoked to get the thread running –run() – contains the executable code of the thread useful static method –sleep(int time) – causes thread to suspend execution for a specified period of time

Non Blocking Window Put lengthy processing in a separate thread so it no longer blocks the event thread which contains the various repaints.. Thread code often done as an inner class (but doesn’t have to be..) Java has a Thread class.. –Start () method has to be called to get it running –Run() method has to be filled up with whatever it is supposed to do.. In our example.. performLengthWork will go in the thread..

Non Blocking Window // inner class - new thread to perform the lengthy work // As displayed in class.. Subclass the Thread class.. And get this class to perform the “slow” work.. } Make your own thread class.. Constructor optional… Run() method

Non Blocking Window // event handler for menu item public void actionPerformed(ActionEvent e) { msg.setText("Starting..."); // Instantiate your own thread class, and start it... etc } private void performLengthyWork(){ Etc } Instantiate thread Start it..!

Now you’ll see.. Display some text here.. Starting… Working1 Working2 etc.. done

Non blocking window Start menu item clicked:Actionperformed –Creates the worker thread and starts it.. –BUT it doesn’t generate changes to the GUI –Finishes.. Menu is retracted.. Follow on repaint label processed on the event thread… Worker Thread is separate from event thread.. No longer blocking event thread…

Note: Other way to implement threading Used Thread class – and extended it.. Can also use runnable interface.. Contains a run() method.. –Create a class e.g. myThread that implement runnable, and contains the run() method –When you need a threat, instantiate the class, and pass to new Thread( myThread )

Threads.. Good for splitting processing to let things happen in the background Instiantiate Thread class or use Runnable interface Use run() method for what thread should do and start() method to kick it off..