Multithreading Flashcards

1
Q

What are the four types of CPU architecture?

A

SISD, SIMD, MISD, MIMD

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

What are the parallel programming sub models of MIMD

A

Single program multiple data, SPMD and Multiple program multiple Data MPMD

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

Types of memory architecture

A

Shared and distributed

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

What are the types of shared memory architecture?

A

UMA, uniform memory access meaning that all CPUs access the memory at equal speed.
NUMA nonuniform memory access

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

What is symmetric multiprocessing or SMP

A

Is a type of UMA. Has two or more identical processors connected to a single shared memory thru a system bus.

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

What is cache coherency

A

cache coherence is the uniformity of shared resource data that ends up stored in multiple local caches

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

Four states of a thread in general?

A
  1. new state() - thread created but no CPU resources.
  2. Runnable state() - Thread is ready to be executed or is waiting for other states.
  3. blocked state () - thread is blocked from execution because of some operations to complete, like timers. Does not take cpu at this stage
  4. terminated stat ()- is when a thread finishes operation or is stopped.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the join method in threads()

A

The join() method in Java is provided by the java.lang.Thread class that permits one thread to wait until the other thread to finish its execution.

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

InterruptedException

A

Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity.

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

Difference between Thread and Runnable in Java?

A

Best option (P2I)

Thread is a class. It is used to create a thread
Runnable is a functional interface which is used to create a thread

Thread has multiple methods including start() and run()
Runnable has only abstract method run

Each thread creates a unique object and gets associated with it
Runnable - Multiple threads share the same objects.

Multiple Inheritance is not allowed in java hence after a class extends Thread class, it can not extend any other class
but
If a class is implementing the runnable interface then your class can extend another class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the Six states of a Thread in Java

A
  1. new
  2. runnable
  3. Blocked
  4. Waiting
  5. timed_waiting
    6 Terminated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the garbage collector

A

It’s automatic memory management that runs in the background and attempts to reclaim memory no longer in use by the program.
Its a daemon thread.

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

What is mutex or mutual exclusion?

A

it ensures that only one thread can execute the critical section of a computer program at a time.

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

What is Race condition?

A

Race condition in Java occurs in a multi-threaded environment when more than one thread try to access a shared resource, critical section of a program, (modify, write) at the same time.

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

What is a critical section in a program?

A

the part of the program which accesses the shared resource is known as the critical section.

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

What is an Atomic variable in java? And why would we use it over locking or Synchronization?

A

They are called atomic variables because they provide some operations that cannot be interfered by multiple threads, they are like wrapper classes. They are used instead of locks and Synchronization is because unlike in the case of locks, no other thread gets suspended; instead, they’re simply informed that they did not manage to update the value. The threads can then proceed to do further work and context switches are completely avoided. A

17
Q

What is intrinsic locking?

A

A locking mechanism attached to each object in java. It can be passed to the synchronized () method.

18
Q

Why are data races hard to identify?

A

Because they dont always happen in each execution. FOr example on small data they may not show up and they may on large data. That makes them very hard to debug.

19
Q

Does synchronize lock block other threads in java?

A

Yes it does. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.

20
Q

What is deadlock? how is it different from datarace?

A

Deadlock in Java is a condition where two or more threads are blocked forever, waiting for each other.
But data race is when two threads or processes access the data at the same time causing inconsistancy.

21
Q

What are the properties of a Java thread?

A

Each Java thread has following properties:
1. Identifier: An identifier of type long that is unique within
the JVM
2. Name: A name of type String
3. Priority: Priority of type int
4. State: A state of type java.lang.Thread.State
5. Group: A thread group the thread belongs to

22
Q

What is the purpose of Thread

Groups in Java?

A

In Java, every thread belongs to a group of threads.
The JDK class java.lang.ThreadGroup provides methods to handle
a whole group of Threads.
With the help of these methods we can interrupt all threads of a
group or set the maximum priority of all threads of a group.
So a thread group is used for taking collective actions on a group of
threads.

23
Q

what is the volatile keyword in java?

A

Volatile variables have the visibility features of synchronized but not the atomicity features. The values of the volatile variable will never be cached and all writes and reads will be done to and from the main memory.

24
Q

How can we prevent a Deadlock?

A
  1. Mutual exclusion: We can use optimistic locking to
    prevent mutual exclusion among resources.
  2. Resource holding: A thread has to release all its exclusive
    locks if it does not succeed in acquiring all exclusive locks
    for resources required.
  3. No preemption: We can use timeout period for an
    exclusive lock to get free after a given amount of time.
  4. Circular wait: We can check and ensure that circular wait
    does not occur, when all exclusive locks have been
    acquired by all the threads in the same sequence.
25
Q

What is a Fair lock in multithreading?

A
In Java there is a class ReentrantLock that is used for implementing
Fair lock. This class accepts an optional parameter fairness. When
fairness is set to true, the RenentrantLock will give access to the
longest waiting thread.

The most popular use of Fair lock is in avoiding thread starvation.
Since longest waiting threads are always given priority in case of
contention, no thread can starve.

26
Q
Which two methods of Object class can be used to implement a
Producer Consumer scenario?
A

For this scenario to start working, a Consumer has to know when
the Producer has produced. In Object class, there is a wait()
method. A Consumer calls wait method to wait on Producer. The
Producer used notify() method of Object class to inform Consumer
that it has produced.