Lecture 9 Flashcards

1
Q

Three types of Map?

A

HashMap (no ordering), LinkedHashMap (linked pointers, traversing happens in specified order), TreeMap (sorted using compareTo of interface comparable, keys must implement comparable).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to traverse an unordered set?

A

Use an iterator. Interface Collection provides a method iterator. Methods: hasNext(), next().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens after a traversal usin an iterator?

A

The used iterator is useless. Cannot be reset.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s a computer program:

A

Represents a set of instructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a process?

A

The sequence of instructions executed by CPU. An instance of the program. Each process consists of threads (sub processes).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are threads Charachteristics?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is concurrency?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Concurrency can happen on system level or…

A

Within a single process. System=multitasking, concurrency of processes, Single=multithreading, concurrency of threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why are multithreading and concurrency useful?

A

To cooperate with other programs (a server with multiple clients), avoid users waiting for response, make programs faster.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Java allows programs to create multiple threads which execute either…

A

Concurrently (multiple CPU/Cores) or sequentially(Scheduler using time slicing).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

4 types of threads relationships?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Toe create a Thread need a Thread class and a…

A

Task class which implements interface Runnable. This task class must implement run(). A thread performs a task, ie the run().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create a thread:

A
Create object of class thread. Pass to the constructor of thread object which implements runnable.
Thread myT = new Thread(myTask);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to start a thread?

A

Invoke method start(); of the thread object (not run() of the task object!).

myT.start();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List six states a thread can be in.

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are race conditions?

A

When multiple threads access a shared resource concurrently in an unprotected way. Without proper synchronization operations interleave each other.

17
Q

What is thread safe?

A

Code is thread safe if it accesses share resources which doesn’t cause race conditions.

18
Q

What is an atomic operation?

A

Cannot be interrupted by other threads. And yet it’s not guaranteed, depends on JVM version.

19
Q

How best to avoid race conditions?

A

Use keyword synchronized.

synchronized(this) {
//Critical section
}
20
Q

What is the critical section?

A

Where the concurrent access of a shared resource takes place.

21
Q

How is synchronized typically used?

A
For entire methods:
Public synchronized void someMethod() {
// critical section
}
22
Q

How does synchronized work?

A

Every Java object comes with a lock. A thread tries to acquire the lock when using a method. Once acquired other threads block until the first thread releases the lock, ie leaves the synchronized method.

23
Q

What is the Kava approach to multithreading synchronization called?

A

The monitor approach. It provides for mutual exclusion (using synchronization keyword) and thread cooperation using condition variables.

24
Q

Another name for Maps?

A

Associate arrays, or dictionaries.