1 / 17

Multithreaded Programming in Java

Multithreaded Programming in Java. Multimedia Technology Programming http://mea.create.aau.dk/course/view.php?id=26 Medialogy, Semester 7 Aalborg University, Aalborg 2010 David Meredith dave@create.aau.dk. Sources.

kalin
Download Presentation

Multithreaded Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multithreaded Programming in Java Multimedia Technology Programming http://mea.create.aau.dk/course/view.php?id=26 Medialogy, Semester 7 Aalborg University, Aalborg 2010 David Meredith dave@create.aau.dk

  2. Sources • Chapter 14 of The Java Programming Language (Fourth Edition) by Ken Arnold, James Gosling and David Holmes (Addison-Wesley, 2006)

  3. Single-threaded programming • Single-threadedprogram • single sequence or thread of instructions, executed one after the other • only one instruction being carried out at any given instant in time • the single thread in a single-threaded program is called the main thread

  4. SingleThreadedProgram.java

  5. Multithreaded programming • Most of the programs you use every day are multithreaded programs because they have to do two or more things at the same time • For example • word processor has to listen for user input, update the screen, save periodic backups, look-up typed words in a dictionary as they are typed, etc. • web crawler crawls many different web pages simultaneously, creating a record for each one in the search engine’s database

  6. Multithreaded programming • Multithreaded program consists of more than one independent thread (sequence of instructions), running at the same time or concurrently • hence sometimes called concurrent programming • Two or more threads can access the same data • this is good in a web crawler where many different crawling threads can update the same central database • HOWEVER this can cause problems such as the race condition

  7. Race Condition • Suppose a is the joint bank account of Hr. and Fru Jensen and it starts off with a balance of 1000kr • Hr. Jensen goes to the branch near his work and deposits 100 kr • Fru Jensen goes to the branch near their home and withdraws 50 kr • The final balance should be 1050 kr...but is it? • It depends on whether the account is locked while a transaction is taking place

  8. Creating Threads in Java • Create a new thread of control in Java by instantiating a Thread object: Thread worker = new Thread(); • Can then set its initial priority, name, etc.: worker.setPriority(worker.getPriority()+1); worker.setName(“myThread”); • When ready call the Thread’s start method to set it off worker.start(); • This starts a new thread of control based on data in Thread object • VM invokes new Thread’s run method which makes the Thread active • Thread’s run method does nothing, but can override it in a subclass of Thread

  9. PingPong.java

  10. Creating threads by specializing Thread • In PingPong, we defined a subclass of Thread called PingPongThread and redefined the run method in this subclass to specify precisely what PingPongThreads should do • But what if we had wanted PingPongThread to inherit from a class other than Thread? • We couldn’t, because it has to inherit from Thread • There’s a better way...

  11. Using the Runnable interface • A Thread abstracts the concept of a worker • i.e., something that carries out some task • When you subtype Thread, the task carried out by the new subtype is defined in its run() method • Instead, can define a new class that implements the Runnable interface • Runnable interface abstracts the concept of a task • Runnable interface declares a method public void run(); • A class that implements Runnable must implement this run() method • A Thread (worker) can be assigned a Runnable object (task) to run • The Thread’s run() method calls the run() method of the Runnable object assigned to it when the Thread is started (with the start() method)

  12. PingPong2.java

  13. Thread runs Runnable objects and implements Runnable itself

  14. Interference and Critical Regions • Interference is when interleaved operations from different threads on shared data could corrupt the data • The code segments that contain the interfering actions are called critical sections or critical regions

  15. Synchronization and Locks • Prevent interference by synchronizing access to critical regions • A Thread should have to acquire a lock on a shared object before processing it • Here, would have to acquire a lock on the bank account, a, before changing it • Thread releases lock on an object when finished with it • A synchronized method or statement acquires a lock on an object before it executes and releases the lock once it has finished

  16. Synchronized Methods HrOgFruJensen.java

  17. Synchronized Statements HrOgFruJensen2.java

More Related