Threads. Parsing fasta sequences in parallel Threads in GUIs.

Slides:



Advertisements
Similar presentations
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
Advertisements

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.
Concurrency 101 Shared state. Part 1: General Concepts 2.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
Multithreading The objectives of this chapter are:
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Multithreading.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 GUI programming with threads. 2 Threads and Swing Swing is not generally thread-safe: most methods are not synchronized –correct synchronization is.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Concurrent Programming and Threads Threads Blocking a User Interface.
UID – Event Handling and Listeners Boriana Koleva
Java Thread and Memory Model
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Creating a GUI Class An example of class design using inheritance and interfaces.
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,
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
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.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
Monitor Pattern Read only Collection views Synchronized Collections views Concurrent HashMap The Vehicle Tracker Example.
Java Thread Programming
Starting on GUIs A version of the “Double or Nothing” gui is here:
Multithreading The objectives of this chapter are:
A brief intro to: Parallelism, Threads, and Concurrency
Threads and Multithreading
CS703 – Advanced Operating Systems
Java Programming Language
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
The University of Adelaide, School of Computer Science
Chapter 19 Java Never Ends
Introduction to Operating Systems
Threads and Multithreading
Multithreading.
Threads and Multithreading
Threads and Multithreading
Race conditions and Synchronization
Background and Motivation
Dr. Mustafa Cem Kasapbaşı
CSCI1600: Embedded and Real Time Software
Threads and Multithreading
Constructors, GUI’s(Using Swing) and ActionListner
9. Threads SE2811 Software Component Design
Threads in Java James Brucker.
Java Concurrency.
Threads and Multithreading
Threads and Multithreading
CS333 Intro to Operating Systems
Java Concurrency.
Using threads for long running tasks.
Threads.
Slide design: Dr. Mark L. Hornick
CSCI1600: Embedded and Real Time Software
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
More concurrency issues
Threads and concurrency / Safety
Presentation transcript:

Threads. Parsing fasta sequences in parallel Threads in GUIs.

There are two ways you can spawn multiple threads in Java. You can extend Thread or you can implement Runnable

run(). What this thread will do… run() can’t throw an Exception This thread sleeps for between 0 and 1000 msec

Thread 1Thread 2Thread 3 …. writingsleeping Each thread “wakes up”, dumps to the console and goes to sleep.

Another, more common way to instantiate a thread. Implement the interface Runnable. The runnable interface has a single method run()

We now implement Runnable instead of extending Thread This is exactly the same

Here we say new Thread(Runnable).start() Rather than te.start() as when ThreadExample was a Thread (since start() is in Thread but not Runnable) But the application works the same way

Multi-threading is tricky. Let’s take an insanely broken example… This is a map that is supposed to count the number of time an integer is observed… Counter shared among all threads HashMap shared among all threads Increment the shared counter Have we seen this number

Multi-threading is tricky. Let’s take an insanely broken example… Not thread safe This is thread safe This is thread safe (variable local to this thread)

This runs the method 10 times in sequence. No new thread is spawned And all is well! We see each increment of counter one time new MyClass() Add 10 numbers to map new MyClass() Add 10 numbers to map new MyClass() Add 10 numbers to map

This runs the method 10 times in parallel. The first 32 are skipped! Breaks in unpredictable ways The integrity of the HashMap is ruined! (Two equal values in the HashMap !) 34 is skipped! New thread spawned!

new MyClass() Add 10 numbers to map new MyClass() Add 10 numbers to map new MyClass() Add 10 numbers to map The threads all run at the same time So, on occasion, the same number gets added as a key to the hashmap ruining the integrity of the data structure. We say HashMap is not a threadsafe data structure

Or, and this is the thing that kills you, it could run perfectly (as it did the next time) …

Or it could run closer to perfectly HashMap integrity violation! Skipped 1 Skipped 5

Java has a keyword “synchronized” which you might think means “only one thread at a time” Nope! Still broken.

What went wrong? synchronized means synchronized(this). First you must obtain a lock on this object before the thread can proceed. So the code on the previous slide is equivalent to… Which you can see is no good. We are instantiating multiple instances of MyClass. They are each locking on themselves! So they can still run in parallel

This is safe, but at the cost of only allowing one thread at a time! May as well not use multi-threading! All the instances of MyClass share one “myObject” Only one thread at a time!

Threading is tough!

What are your options? (1)Don’t use threads. (But then no performance boost!) or (2) Favor immutable objects – they are inherently thread safe (3) Minimize or eliminate information sharing between threads If you are going to use threads a lot in Java, you need to read this book very carefully: We will be doing just that!

Threads. Parsing fasta sequences in parallel Threads in GUIs.

Let’s take an easy problem for our first multi-threading example We have 445,426 sequences and we want to read them into memory (filesize = 134 MB)

Not too hard to code up a single-core parser…. Takes about 6.5 seconds to parse 445,426 sequences

Can we do better on a multi-core machine? Strategy: Instead of parsing a single sequence file, we will break up the sequence file into multiple sequence files and have each thread parse a sequence file in parallel.

Here is the code to split up the file (keeping the sequences together)

Not hard to make a runnable to parse the FastaFile The volatile keyword is important. This ensures that when a new value is written to those fields, all threads can see it. Without the volatile keyword, this guarantee does not exist.

This kicks off the threads and gathers the results…

If any of the threads fail, everything fails This is thread safe because we walk through one thread at a time Wait until all threads are done (There are better ways to do this. We’ll cover later in the semester) 100 msec. Balance wasting CPU cycles vs. unnecessarily sleeping when all threads are done.

Let’s run some time trials… One thread per file in the split directory Kick off between 1 and 15 threads Run 5 time trials for each thread

Results for an 8-core machine Pretty dramatic speedup for not much programming effort. Sweet spot seems to be 7 threads (run on an 8 core box…) (around a 5.7X speedup with 7 threads….)

Some notes: Parsing these sequences is embarrassingly parallel. No synchronization between threads is needed. (Which makes it an easy first problem)

Either master threads (as we will start to do later) or as much as possible avoid them!

Threads. Parsing fasta sequences in parallel Threads in GUIs.

A place you can’t really avoid threads is GUIs There is an “AWT” thread that is the thread that calls the ActionListeners (and other Listeners). It’s also responsible for updating the GUI… If that thread hangs, the GUI freezes up…

Cancel buttons require understanding thread. Let’s build an app with a Cancel button Our usual collection of instance variables textArea goes center Our bottom Panel holds the buttons Monitor if the cancel button is pressed (actually should be volatile !)

Our bottom panel provider…

The listeners are clearly where the action is. The CancelListener is the easy one…

Here’s how NOT to do the SlowActionListener The “AWT” thread blocks. The application appears dead.

A “blocked” AWT thread means… -The “Do something slow” button doesn’t pop back up. -The text area doesn’t update -If the user switches away and switches back, the application doesn’t redraw…. -The user can’t cancel

The slow stuff needs instead to happen on a different thread AWT Thread ActionListener() spawn SlowActionRunnable Does the slow thing This thread is not blocked.. The code we just saw…. AWT Thread ActionListener() Start the slow thing AWT thread is blocked Application appears dead

We make a Runnable that will be the newly spawned thread… src.threads.CancelButtonExample

This is all fast This is still slow, but is in its own thread. So actionPerformed returns quickly and SlowActionRunnable() runs in a background thread… The slow stuff needs to happen on a different thread AWT Thread ActionListener() spawn SlowActionRunnable Does the slow thing This thread is not blocked..

We now have a responsive interface and a working cancel button

We are almost there. Our last remaining issues is that most Swing methods are not thread-safe. That is, they should only be called from the AWT thread. We are making a number of calls to Swing methods from the non-AWT thread

It turns out that sometimes this is ok…. We look in the JavaDocs…. so append (and it turns out setText()) can be called from any thread..

But setEnabled() is not marked as thread safe….

So if the AWT thread is doing something to the buttons when these methods are called, the results are undefined We need to place these lines of code on the AWT thread..

Place on AWT thread and block the current thread until the runnable has been executed

Place on AWT thread and this thread Doesn’t block (e.g. invokeLater() ) returns immediately Because we are at the end of the thread it doesn’t matter if we use invokeLater or invokeAndWait

The slow stuff need to happen on a different thread AWT Thread ActionListener() spawn SlowActionRunnable Does the slow thing. When finished This thread is not blocked.. Send a Runnable update GUI You will see many applets and Java applications that block when they are performing tasks… This kind of multi-threading isn’t too bad, because you are sending pretty simple messages to the GUIs…

Coming up: More advanced multi-threading… Fixing our HashMap example..