Java Flashcards

1
Q

What are the benefits of using Multithreading

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Multithreading

A

Multithreading is a CPU feature tha allows two ore more instruction threads to execute independently while sharing the same process resources.

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

What is a thread in multithreading?

A

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.

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

What are some possible issues when using multithreading?

A

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.

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

What is the difference between Parallel and Concurrent Multiprocessing

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does multithreading work in general?

A

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.

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

Define the process of multithreading in Java

A
  1. Create a new thread by creating an instance of the Thread class or by creating a class that implements the Runnable interface
  2. Make the thread runnable by calling the .start() method
  3. The thread scheduler will select the thread and run it
  4. The thread will terminate either as a result of the run method exiting normally or by an error occurring.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an Object Level Lock

A

Object Level locking prevents other threads from accessing the shared resources of a process until the locked method is finished.

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

What is a Class Level Lock

A

Prevents multiple threads from accessing a synchronized block within a process.

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

What are User Threads and Daemon Threads, and how are they different?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is a User thread defined? How is a Daemon thread defined?

A

Any thread that is created is automatically considered a user thread.

In order to set a thread as a Daemon thread, you must:

  1. Create an instance of the thread class
  2. use the setDaemon() method, passing it the ‘true’ value (setDaemon(true))
  3. call the thread start() method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe the wait() method

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe the sleep() method.

A

The sleep() method causes the given thread to stop execution for a set amount of time.

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

Describe the notify() method.

A

the notify() method is used to wake a specific waiting thread

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

Describe the notifyAll() method

A

notifyAll() will wake all currently waiting threads at once.

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

What is the difference between the start() and run() method of a thread

A
the start() method creates a thread that contains code the can be ran
the run() method calls for the code to actually be executed.

The run method does not create a thread but can be used to tell the JVM to execute a thread that has already been created.

17
Q

Explain the concept of a thread pool

A

A thread pool is a group of predefined threads that are initialized the the start of a process.
These threads can then be used by the process and placed back in to the thread pool when the execution is complete.

18
Q

Describe the Join() method

A

The join() method is used to tell one thread to wait to be executed until another thread is complete.

19
Q

Define garbage collection

A

Garbage collection is the process of finding and deleting objects in memory that are no longer required by the process. This is done in order to free up memory and reduce the overall load of a process.

20
Q

Explain Volatile variable in java

A

A volatile variable is a variable that is defined by the volatile keyword. This makes the variable thread safe by allowing each individual thread to read the value of the variable from memory instead of the CPU chache so that each thread that references the variable will have an accurate up to date value.

21
Q

What are design patterns

A

Design patterns are well proven solutions to common problems in programming.

22
Q

Name the three main types of design patterns in java

A
  • Creational
  • Structural
  • Behavioral
23
Q

Define the term Creational Design Pattern

A

Creational design patterns are design patterns used when objects are created within a program.

24
Q

Define the term Structural Design Pattern

A

Structural design patterns are implemented in the design and composition of classes.

25
Q

Define the term Behavioral Design Pattern

A

Behavioral design patterns are concerned with object interaction and responsibility.

26
Q

Name and briefly describe all creational design patterns

A
  • Factory Method: Create an abstract main class that is extended by subclasses and only create subclass objects
  • Abstract Factory Method: Extend Main class in order to create non specified families of related objects
  • Singleton Pattern: Create one single object that can be referenced anywhere
  • Prototype pattern: Copy objects that are expensive to create from scratch and change their values
  • Builder Pattern: Construct complex objects from smaller simple objects
  • Object Pool Pattern: Reuse expensive objects instead of creating new objects