Lecture 13 Flashcards
What is a process?
A program that is running
What are threads used for?
Doing multiple things at the same time, for one program.
What memory do threads use?
All threads for one program, use the same piece of memory.
What is concurrency?
Doing multiple tasks at the same time
What is parallelism?
Splitting one task over multiple processors
What are the three ways for your computer to handle multiple threads?
- Having multiple processors.
- Interleaving
- Hyperthreading
In interleaving, how does the order of threads get determined?
It is not pre-determined and it depends on a lot of unknown variables. Therefore the order changes per time that you execute, per OS etc.
What are Heisenbugs?
Bugs that appear because of wrong expectations with regards to the scheduling of threads.
What are 2 cons of multi-threading? In what cases is multi-threading useful? give an example
It is not necessary and risky.
It is useful for trivially parallelizable problems.
e.g. serving web pages,
processing multiple files with the same algorithm, accepting user input while waiting for something (e.g. a page to load)
What are the three important methods that the Java class ‘Thread’ contains?
Thread(string name), start(), run()
What are the two ways to create a thread in Java?
- Create a class that inherits from Thread
2. Create a class that implements runnable
How do you implement a thread in your MyThread class, if you’re inheriting from Thread?
You set the constructor to the Thread constructor (using super) and you redefine the run method, with what your thread should do.
How do you activate your thread?
You use the start() method (e.g. in your main) to create a thread and to execute run()