Threads Flashcards

1
Q

What are the possible states of a thread in Java?

A

New, Runnable, Waiting, Timed Waiting, Blocked, Terminated

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

What state does a thread enter when it is created but not yet started?

A

New

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

What happens when a thread is in the Runnable state?

A

It is considered to be executing its task

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

What is the Waiting state in a thread life cycle?

A

A state where a runnable thread waits for another thread to perform a task

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

What is the Timed Waiting state?

A

A state where a runnable thread waits for a specified interval of time

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

When does a thread return to the Runnable state from Timed Waiting?

A

When the time interval expires or when it is notified by another thread

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

What causes a thread to enter the Blocked state?

A

When it attempts a task that cannot be completed immediately

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

What is the Terminated state?

A

A state when a runnable thread successfully completes its task or terminates

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

What does the JVM see when a thread is in the Runnable state?

A

It sees only the Runnable state, not the underlying Ready and Running states

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

What is thread scheduling in an operating system?

A

The process that determines which thread runs next based on priorities

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

What is a thread’s priority in Java?

A

A value that helps determine the order in which threads are scheduled

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

True or False: Higher-priority threads are guaranteed to execute before lower-priority threads.

A

False

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

What is timeslicing in the context of thread execution?

A

A method that allows threads of equal priority to share a processor

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

What is the purpose of the Runnable interface?

A

To specify a task that can execute concurrently with other tasks

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

What does the Executor interface do?

A

It manages a group of threads called a thread pool to execute Runnables

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

What is the main advantage of using an Executor over creating threads manually?

A

It can reuse existing threads and optimize the number of threads for performance

17
Q

Define thread synchronization.

A

The coordination of access to shared data by multiple concurrent threads

18
Q

What is mutual exclusion in the context of thread synchronization?

A

Ensuring that only one thread accesses shared data at a time

19
Q

What type of data requires thread synchronization?

A

Shared mutable data

20
Q

What does marking a reference as final indicate?

A

The reference will not change, but it does not guarantee immutability of the object

21
Q

What are monitors in Java?

A

Built-in mechanisms for synchronization that ensure only one thread executes a block of code at a time

22
Q

What is the purpose of synchronized statements in Java?

A

To ensure that a thread must hold a monitor lock to execute a block of code

23
Q

What happens when a static method is declared synchronized?

A

The lock is on the class rather than on the instance of the object

24
Q

What is the problem with non-static synchronization?

A

Interference can occur between threads operating on different instances of the same class

25
Q

What does the synchronized keyword do?

A

It indicates that a method or block of code requires exclusive access