Threads Flashcards

1
Q

What is multi-threading?

A

Multithreading is a process of executing multiple threads simultaneously. thread. Thread is a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading - used. Both are used to achieve multitasking

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

In what ways can you create a thread?

A

1.) By implementing the Runnable interface and passing it to a new Thread objects constructor.

2) By extending the Thread class, and creating an object of that class.

3.) By using a lambda within the constructor of a new Thread instance.

4.) By executing tasks asynchronously with an ExecutorService.nts the runnable interface.

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

List the methods in the Thread class and Runnable interface

A

The Runnable interface contains the run method, which must be overridden in order to create a new thread. The Thread class contains setName(), setPriority(), join(), isAlive() and start().

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

Explain the lifecycle of a thread

A

New: When a thread is created. Runnable: When the run() method has been implemented. Active: When the start method has been invoked. Non runnable: When another thread is interfering with the thread and inhibiting its run() method. Terminated: the thread is done with its task.

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

What is deadlock?

A

A deadlock is a condition in which two or more threads are blocked (hung) forever because they are waiting for each other.

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

What is the synchronized keyword?

A

A block that is described with a synchronized keyword ensures that only a single thread executes at a particular time.

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