MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have...
Transcript of MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have...
![Page 1: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/1.jpg)
MultiThreadingObject Orientated Programming in Java
Benjamin Kenwright
![Page 2: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/2.jpg)
Outline
Review
Essential Java Multithreading
Examples
Today’s Practical
Review/Discussion
![Page 3: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/3.jpg)
Question
Does the following code compile? What
would the output be?
![Page 4: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/4.jpg)
Answer
![Page 5: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/5.jpg)
Question
Does the following code compile? What
would the output be?
![Page 6: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/6.jpg)
Answer
78-7616
Explain why?
![Page 7: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/7.jpg)
Question
Does the following code compile? What
would the output be?
![Page 8: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/8.jpg)
Answer
![Page 9: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/9.jpg)
Question
Does the following code compile? What
would the output be?
![Page 10: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/10.jpg)
Answer
![Page 11: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/11.jpg)
Question
Does the following code compile? What
would the output be?
![Page 12: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/12.jpg)
Answer
Yes
abcd abc false
abcd abcd true
![Page 13: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/13.jpg)
Why Multithreading?
What is the rational?
Why make things complicated?
What would happen if we didn’t have
multithreading?
![Page 14: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/14.jpg)
![Page 15: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/15.jpg)
Concurrency & Parallelism
![Page 16: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/16.jpg)
Threading
Advantages & Disadvantages of Threads
Java Threads
Class: java.lang.Thread
Interface: java.lang.Runnable
Multithreaded Programming
![Page 17: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/17.jpg)
Thread Definition
Definition: A thread is a single sequential
flow of control within a program (also
called lightweight process)
![Page 18: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/18.jpg)
Thread
Each thread acts like its own sequential program
Underlying mechanism divides up CPU between multiple threads
Two types of multithreaded applications
Make many threads that do many tasks in parallel, i.e., no communication between the threads (GUI)
Make many threads that do many tasks concurrently, i.e., communication between the threads (data access)
![Page 19: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/19.jpg)
Advantages/Disadvantages
Advantages
Responsiveness, e.g., of user interfaces
Resource sharing
Economy
Utilization of multiprocessor hardware architectures
Disadvantages
More complicated code
Deadlocks (very hard to debug logical program errors)
![Page 20: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/20.jpg)
Single & Multithreaded
Processes
![Page 21: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/21.jpg)
User and Kernel Threads
Thread management done by user-level
threads library
Supported by the kernel
![Page 22: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/22.jpg)
Java Threads
Java threads may be created by
Extending Thread class
Implementing the Runnable interface
![Page 23: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/23.jpg)
Class Thread
The simplest way to make a thread
Treats a thread as an object
Override the run() method, i.e., the thread’s “main”
Typically a loop
Continues for the life of the thread
Create Thread object, call method start()
Performs initialization, call method run()
Thread terminates when run() exits
![Page 24: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/24.jpg)
Extending the Thread Class
![Page 25: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/25.jpg)
Extending the Thread Class
Example
javac ThreadTest.java
java -cp . ThreadTest
![Page 26: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/26.jpg)
Multithreaded Programming
![Page 27: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/27.jpg)
Sharing Resources
Single threaded programming: you own everything, no problem with sharing
Multi-threaded programming: more than one thread may try to use a shared resource at the same timeAdd and withdraw from a bank account
Using the speakers at the same time, etc.
Java provides locks, i.e., monitors, for objects, so you can wrap an object around a resourceFirst thread that acquires the lock gains control
of the object, and the other threads cannot call synchronized methods for that object
![Page 28: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/28.jpg)
Locks
One lock pr. object for the object’s methods
One lock pr. class for the class’ static methods
Typically data is private, only accessed through methodsMust be private to be protected against
concurrent access
If a method is synchronized, entering that method acquires the lockNo other thread can call any synchronized
method for that object until the lock is released
![Page 29: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/29.jpg)
Sharing Resources, cont.
Only one synchronized method can be
called at any time for a particular object
![Page 30: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/30.jpg)
Sharing Resources, cont.
Efficiency
Memory: Each object has a lock
implemented in Object
Speed and Overhead (e.g., calling)
• Older standard Java libraries used
synchronized a lot, did not provide any
alternatives
![Page 31: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/31.jpg)
Sharing Resources, Example
![Page 32: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/32.jpg)
Sharing Resources, Example
![Page 33: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/33.jpg)
Sharing Resources, Example cont.
![Page 34: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/34.jpg)
Sharing Resources, Example cont.
![Page 35: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/35.jpg)
Sharing Resources, Example cont.
![Page 36: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/36.jpg)
The Runnable Interface
To inherit from an existing object and
make it a thread, implement the
Runnable interface
A more classical, function-oriented way
to use threads
![Page 37: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/37.jpg)
The Runnable Interface, cont .
![Page 38: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/38.jpg)
The Runnable Interface, cont.
![Page 39: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/39.jpg)
Java Thread Management
suspend() – suspends execution of the currently running thread
sleep() – puts the currently running thread to sleep for a specified amount of time
resume() – resumes execution of a suspended thread
stop() – stops execution of a thread
![Page 40: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/40.jpg)
Synchronized Fields and Constructors
![Page 41: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/41.jpg)
Issues
Thread priority
Thread groups
Daemon (unix term)
similar to a service (on
Win32)
Deadlock
Very hard to detect
logical errors in programs
![Page 42: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/42.jpg)
Deadlocks
![Page 43: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/43.jpg)
Deadlocks, cont.
![Page 44: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/44.jpg)
Summary
Overview Multithreading with Java
Single-threaded programming: live by all by your self, own everything, no contention for resources
Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources
Multithreading is built-into the Java programming language
Multithreading makes Java programs complicatedMultithreading is by nature difficult, e.g., deadlocks
![Page 45: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/45.jpg)
This Week
Read Associated Chapters
Review Slides
Java Exercises
![Page 46: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/46.jpg)
Exercises
Chapter 29
Exercises 29.1 to 29.4
![Page 47: MultiThreading - GitHub Pages · Multithreading programming: suddenly “others” can have collisions and destroy information, get locked up over the use of resources Multithreading](https://reader033.fdocuments.us/reader033/viewer/2022050303/5f6bdab61576e64e3f69b3db/html5/thumbnails/47.jpg)
Questions/Discussion