Lecture 9 Flashcards
Three types of Map?
HashMap (no ordering), LinkedHashMap (linked pointers, traversing happens in specified order), TreeMap (sorted using compareTo of interface comparable, keys must implement comparable).
How to traverse an unordered set?
Use an iterator. Interface Collection provides a method iterator. Methods: hasNext(), next().
What happens after a traversal usin an iterator?
The used iterator is useless. Cannot be reset.
What’s a computer program:
Represents a set of instructions.
What is a process?
The sequence of instructions executed by CPU. An instance of the program. Each process consists of threads (sub processes).
What are threads Charachteristics?
Execute indecently of each other m. Multithreading is two or more threads in single process. All threads share same resources in process (ie memory). Depend on parent process. Also called lightweight processes.
What is concurrency?
Two or more processes/threads running simultaneously. Either at same time (parallel computing) or seemingly (switching back and forth). Concurrency achieved by multiple CPUs or CPU Cores, or frequent switching by scheduler.
Concurrency can happen on system level or…
Within a single process. System=multitasking, concurrency of processes, Single=multithreading, concurrency of threads.
Why are multithreading and concurrency useful?
To cooperate with other programs (a server with multiple clients), avoid users waiting for response, make programs faster.
Java allows programs to create multiple threads which execute either…
Concurrently (multiple CPU/Cores) or sequentially(Scheduler using time slicing).
4 types of threads relationships?
Unrelated threads: don’t interact. Run in parallel.
Related but unsynchronized: work on same data without ness for sync.
Mutually exclusive: work on shared resources, must avoid race conditions. Communicating mutually exclusive: must notify each other of their state change.
Toe create a Thread need a Thread class and a…
Task class which implements interface Runnable. This task class must implement run(). A thread performs a task, ie the run().
Create a thread:
Create object of class thread. Pass to the constructor of thread object which implements runnable. Thread myT = new Thread(myTask);
How to start a thread?
Invoke method start(); of the thread object (not run() of the task object!).
myT.start();
List six states a thread can be in.
New - created but not yet running. Runnable - start() invoked. Running - actually running. Blocked - sleeps or waits for event Interrupted - by another thread. Dead - has ended.