What defines a program as concurrent?
A program is concurrent if it manages multiple tasks that start; run; and complete in overlapping time periods.
What represents a flow of control in a concurrent Java program?
A Thread.
Which thread is always present when a Java application starts?
The main thread.
What is the purpose of extending the Thread class?
To create a specialized thread type by overriding the run() method with specific task logic.
When using the Runnable interface; how is execution started?
By passing the Runnable object to a Thread constructor and calling thread.start().
What problem does mutual exclusion solve?
It prevents multiple threads from accessing shared resources simultaneously; avoiding data corruption.
What is a critical section?
A part of the code that accesses a shared resource and must not be executed by more than one thread at a time.
In the statement synchronized(lock); what is lock?
An object whose “intrinsic lock” (monitor) is used to control access to the synchronized block.
What happens when a method is declared synchronized?
The thread must acquire the lock for the object (or the class; if static) before executing the method.
What does the wait method do?
It causes the current thread to wait until another thread invokes notify() or notifyAll() on the same monitor.
Which methods can release a waiting thread?
notify() and notifyAll().
Why is wait usually called in a loop?
To guard against “spurious wakeups” and ensure the condition being waited for is actually met.
What happens if a thread calls wait outside a synchronized block?
It throws an IllegalMonitorStateException.
Which statement about notify is correct?
It wakes up a single random thread waiting on the object’s monitor.
What is the role of ExecutorService?
To manage a pool of threads and handle asynchronous task execution without manual thread creation.
What does Executors.newSingleThreadExecutor() provide?
An executor that uses a single worker thread to execute tasks sequentially.
What does submit do in an ExecutorService?
It submits a task for execution and returns a Future representing the pending results.
What happens if you call start() twice on the same Thread instance?
It throws an IllegalThreadStateException.
What is a potential issue when forgetting mutual exclusion in shared data access?
Race conditions; where the final state depends on the unpredictable timing of thread execution.
What must be true before calling wait; notify; or notifyAll?
The thread must own the object’s monitor (be inside a synchronized block).
What does the volatile keyword guarantee?
Visibility: any write to a volatile variable is immediately visible to all other threads.
Which variables can be declared as volatile?
Instance variables; static variables; and array elements.
What is NOT provided by volatile?
Atomicity: it does not prevent race conditions during compound operations like i++.
Why might a thread read a stale value without volatile?
Because threads may cache variables in local CPU registers or memory for performance.