Threads Flashcards
What is multi-threading?
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
In what ways can you create a thread?
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.
List the methods in the Thread class and Runnable interface
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().
Explain the lifecycle of a thread
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.
What is deadlock?
A deadlock is a condition in which two or more threads are blocked (hung) forever because they are waiting for each other.
What is the synchronized keyword?
A block that is described with a synchronized keyword ensures that only a single thread executes at a particular time.