Java Flashcards
What are the benefits of using Multithreading
- Allow the program to run continuously even if one part is blocked.
- Improve performance as compared to traditional parallel programs that use multiple processes.
- Allows the program to utilize the full power of the CPU
- Improves responsiveness of complex applications
- Generally faster than non mutlithreaded programs
- Exceptions in one thread will not affect other threads because each thread is independent
- Less resource intensive than multi process programs
What is Multithreading
Multithreading is a CPU feature tha allows two ore more instruction threads to execute independently while sharing the same process resources.
What is a thread in multithreading?
A thread is a self contained sequence of instructions that can execute in parallel with other threads that are part of the same root process.
What are some possible issues when using multithreading?
Race Conditions - This happens when the output of one thread is dependent on another thread. This can result in unexpected output from the dependent threads which may cause strange issues in the rest of the program.
Deadlock - A deadlock can occur when two or more threads are dependent on each other request resources from the other dependent threads in a circular manner. This will freeze both threads because neither is able to continue without first receiving the requested resource from the other thread.
What is the difference between Parallel and Concurrent Multiprocessing
Parallel Multiprocessing - Each thread of a process is being handled at the same time
Concurrent Multiprocessing - Each thread is being handled independently but multiple threads are used in order to increase efficiency
How does multithreading work in general?
In order to increase efficiency, a single process that needs to execute a set of instructions can utilize the power of the entire CPU by dividing up the set of instructions in to threads which can be assigned to a different core of the CPU.
This allows the CPU to execute those instruction sets at the same time, drastically reducing the processing time of the instructions.
Define the process of multithreading in Java
- Create a new thread by creating an instance of the Thread class or by creating a class that implements the Runnable interface
- Make the thread runnable by calling the .start() method
- The thread scheduler will select the thread and run it
- The thread will terminate either as a result of the run method exiting normally or by an error occurring.
What is an Object Level Lock
Object Level locking prevents other threads from accessing the shared resources of a process until the locked method is finished.
What is a Class Level Lock
Prevents multiple threads from accessing a synchronized block within a process.
What are User Threads and Daemon Threads, and how are they different?
User Thread - User threads have a specific lifestyle that the JVM will fully execute before terminating
Daemon Thread- Refered to as “Service Providers” that help support user threads.
The main difference is that Daemon threads can be terminated at any time during their lifecycle while User threads must be fully executed before they can be terminated by the JVM.
Any Daemon thread being used to support a User thread will be terminated when the User thread is terminated.
How is a User thread defined? How is a Daemon thread defined?
Any thread that is created is automatically considered a user thread.
In order to set a thread as a Daemon thread, you must:
- Create an instance of the thread class
- use the setDaemon() method, passing it the ‘true’ value (setDaemon(true))
- call the thread start() method.
Describe the wait() method
The wait() method will cause a thread to sleep until it notified by another thread that it may resume execution through the notify(), or notifyAll() method.
Describe the sleep() method.
The sleep() method causes the given thread to stop execution for a set amount of time.
Describe the notify() method.
the notify() method is used to wake a specific waiting thread
Describe the notifyAll() method
notifyAll() will wake all currently waiting threads at once.