Multithreading in Java Flashcards
What are threads?
Threads are lightweight processes that facilitate in allowing multiple activities in a single process. A thread is a series of executed statements. Each thread has its own program counter, stack and local variables but it shares memory, files and per-process state.
In multithreaded environment, programs make maximum use of CPU so that the idle time can be kept to minimum.
What are the common thread states?
here are several thread states, A thread can be in any one of the state at a particular point of time. It can be running state. It can be ready to run state as soon as it gets CPU time. A running thread can be suspended. A suspended thread can be resumed. A thread can be blocked when waiting for a resource. At any time a thread can be terminated.
Purpose of multithreading?
The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program called a thread. Each thread has a separate path of its execution. So this way a single program can perform two or more tasks simultaneously.
How do you create threads in Java?
A thread can be created in two ways: 1)By extending Thread class 2) By implementing Runnable interface.
Methods to manage threads?
- getName(): It is used for Obtaining a thread’s name
- getPriority(): Obtain a thread’s priority
- isAlive(): Determine if a thread is still running
- join(): Wait for a thread to terminate
- run(): Entry point for the thread
- sleep(): suspend a thread for a period of time
- start(): start a thread by calling its run() method
Describe process by creating a thread thru implementing the Runnable interface.
- Create a class that implements the Runnable interface. We must need to give the definition of run() method.
- This run method is the entry point for the thread and thread will be alive till run method finishes its execution.
- Once the thread is created it will start running when start() method gets called. Basically start() method calls run() method implicitly.
Describe process by creating a thread thru extending Thread class.
- This is the second way of creating a thread. Here we need to create a new class that extends the Thread class.
- The class should override the run() method which is the entry point for the new thread as described above.
- Call start() method to start the execution of a thread.
What are thread priorities?
- Thread priorities are the integers which decide how one thread should be treated with respect to the others.
- Thread priority decides when to switch from one running thread to another, process is called context switching
- A thread can voluntarily release control and the highest priority thread that is ready to run is given the CPU.
- A thread can be preempted by a higher priority thread no matter what the lower priority thread is doing. Whenever a higher priority thread wants to run it does.
- To set the priority of the thread setPriority() method is used which is a method of the class Thread Class.
In place of defining the priority in integers, we can use MIN_PRIORITY, NORM_PRIORITY or MAX_PRIORITY.
What is synchronization?
When two or more threads need access to a shared resource there should be some way that the resource will be used only by one resource at a time. The process to achieve this is called synchronization. To implement the synchronous behavior java has synchronous method. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. All the other threads then wait until the first thread come out of the synchronized block.
Why use threads?
- To perform asynchronous or background processing
- Increases the responsiveness of GUI applications
- Take advantage of multiprocessor systems
- Simplify program logic when there are multiple independent entities
When does a thread end?
A Thread ends due to the following reasons:
- The thread ends when it comes when the run() method finishes its execution.
- When the thread throws an Exception or Error that is not being caught in the program.
- Java program completes or ends.
- Another thread calls stop() methods.
What’s deadlock?
Whenever there is multiple processes contending for exclusive access to multiple locks, there is the possibility of deadlock. A set of processes or threads is said to be deadlocked when each is waiting for an action that only one of the others can perform.
In order to avoid deadlock, one should ensure that when you acquire multiple locks, you always acquire the locks in the same order in all threads.
What are some common guidelines for synchronization?
- Keep blocks short. Synchronized blocks should be short — as short as possible while still protecting the integrity of related data operations.
- Don’t block. Don’t ever call a method that might block, such as InputStream.read(), inside a synchronized block or method.
- Don’t invoke methods on other objects while holding a lock. This may sound extreme, but it eliminates the most common source of deadlock.