MULTITHREADING Flashcards

1
Q

What is multithreading

A

Multi-threading is the process of executing multiple threads in a system, whereby
these executions can either be parallel or concurrent

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

What is a thread?

A

This is a basic unit of a process. It can also be defined as a process within a process

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

What are the different types of a thread

A

User-level thread:These are threads that are created and managed by users. They are commonly
used at the application level. An example is when we use threading in programming like in java,
python etc

Kernel-level Thread:These threads are implemented and supported by the operating system. They
generally take more time to execute than user threads, for example, Window Solaris.

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

Explain the two ways of creating a thread

A
The first way of creating a new thread is to extend the Thread class. The subclass
should override the run method of the Thread class. An instance of the subclass
can then be allocated and started.
The second way is to implement the Runnable interface. The class then implements the run
method. An instance of the class can subsequently be allocated, passed as an argument when
creating Thread, and started.

*See notes for code

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

What are the advantages of implementing the runnable interface over extending the Thread class

A
1.Inheritance - Java does not support multiple inheritance. As a result, extending the Thread class robs
the program the benefits of inheritance. Implementing Runnable allows for the class to extend another
one.
  1. Flexibility - Once you extend a Thread class, the action being performed will always be in a thread.
    Implementing runnable provides more flexibility. You can run the action in a thread, pass it to an
    executor service or even pass it as a task in a single-threaded application.

3.Composition - Both composition and inheritance are used to establish relationships in java.
Composition establishes a ‘has a’ relationship and is more malleable than inheritance. Implementing
Runnable provides composition.

4.Memory - When extending the thread class, each thread creates a unique object and gets associated
with it. This requires more memory. On the other hand, for Runnable, multiple threads share the same
objects hence less memory is required.

  1. Implementing Runnable gives a cleaner separation between the code and the implementation of the
    threads.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe the life-cycle of a thread

A
  1. New Thread: When a thread lies in the new state, its code is yet to be run and hasn’t started to execute.
  2. Runnable State: A thread that is ready to run is moved to a runnable state. In this state, a thread might actually be running or it might be ready to run at any instant of time. It is the responsibility of the thread scheduler to give the thread, time to run.
  3. Blocked/Waiting state: When a thread is temporarily inactive, then it’s in either blocked or waiting
  4. Timed Waiting: A thread lies in a timed waiting state when it calls a method with a time-out parameter. A thread lies in this state until the timeout is completed or until a notification is received. For example, when a thread calls sleep or a conditional wait, it is moved to a timed waiting state.

5.Terminated State: A thread terminates because of either of the following reasons:
Because it exits normally. This happens when the code of the thread has been entirely executed by the program.
Because there occurred some unusual erroneous event, like segmentation fault or an unhandled exception.

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

You stumble upon 3 threads that must execute at different times due to their varied priority. Explain the two thread scheduling techniques the JVM would provide as options for multithreading, then point out the preferred thread scheduler for your specific threads. Also, using Java code snippets, use the right method to assign these different threads with different scheduling constants that would ensure they are executed in the desired order.

A

Scheduling techniques:
1.First Come First Serve Scheduling:
In this scheduling algorithm, the scheduler picks the threads thar arrive first in the runnable queue. Time-slices are provided to the threads so that after some time, the running thread has to give up the CPU.
2.Preemptive-Priority Scheduling: Suppose there are multiple threads available in the runnable state. The thread scheduler picks that thread that has the highest priority. Since the algorithm is also preemptive, therefore, time slices are also provided to the threads to avoid starvation.

Methods tp set priorities:

t1. setPriority(2);
t2. setPriority(5);
t3. setPriority(8);

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

List methods that can change the state of a thread in java

A

void start()-This method will start a new thread of execution by calling run() method of Thread/runnable object.

void run()-This method is the entry point of the thread. Execution of thread starts from this method.

void sleep(int sleeptime)-This method suspend the thread for mentioned time duration in argument (sleeptime in ms)

stop() method

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

Multitasking can be obtained using either threads (multithreading) or processes (multiprocessing). Outlining at least two characteristics of each, distinguish between a thread and a process.

A
  • A process is a collection of code, memory, data and other resources. A thread is a sequence of code that is executed within the scope of the process. You can (usually) have multiple threads executing concurrently within the same process
  • Processes is isolated while threads share memory.
  • A system call is involved in processes while no system call is involved in a thread, it is created using APIs.
  • Individual processes are independent of each other while threads are parts of a process and so are dependent.
  • Processes have independent data and code segments while a thread shares the data segment, code segment, files etc. with its peer threads.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of a daemon thread. Using a Java code snippet, create a thread using any of the two common ways and assign it to be a Daemon thread
using the appropriate method.

A

Daemon threads are low-priority threads whose purpose is to provide services to user threads.
Thread daemonThread = new Thread(daemonRunner);
daemonThread.setDaemon(true);
daemonThread.start();

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