Wk2L1 - Intro to Threads & Processes 2 Flashcards

1
Q

What is the Runnable Interface in Java?

A

The Runnable interface in Java contains only one abstract method, run(), which must be implemented to define the thread’s behavior. Unlike extending the Thread class, implementing Runnable allows multiple interfaces to be used.

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

What is the difference between extending the Thread class and implementing Runnable?

A

Extending Thread: Allows overloading of methods other than run(), but a class can only inherit from one class.

Implementing Runnable: Only requires the implementation of run(). Memory may be shared between threads depending on the objects passed.

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

What does the Thread.join() method do?

A

The join() method makes the main thread wait until the spawned threads finish their execution. It is useful when you need to ensure all threads have completed before moving on to the next task.

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

When would you use the join() method in a multithreaded program?

A

You would use join() when a task requires other threads to finish before continuing execution, ensuring that the program does not move forward until all threads are done.

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

What are the benefits of using the Runnable Interface?

A

Implementing Runnable provides more flexibility by allowing the class to implement multiple interfaces, and you can control whether memory is shared between threads by passing the same or different objects to them.

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

What are association and inheritance in Object-Oriented Programming (OOP)?

A

Association: Represents a “has a” relationship between objects.

Inheritance: Represents an “is a” relationship, where a subclass inherits methods and properties from a superclass.

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

What is an abstract class in Java?

A

An abstract class contains abstract methods with no implementation and is too generalized to be used for creating objects directly. It serves as a blueprint for subclasses.

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

How does memory sharing work with threads in Java?

A

Memory can be shared between threads when the same object is passed to each thread. However, if different objects are passed, the threads will not share memory.

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

What is the difference between Inheritance and Interface in Java?

A

Inheritance:
- Represents an “is a” relationship.
- A subclass inherits properties and methods from a single superclass.
- A class can only inherit from one superclass.
- Allows overriding methods from the parent class.
- Example: A Dog class extends an Animal class because a dog “is an” animal.

Interface:
- Represents a “can do” relationship.
- A class can implement multiple interfaces.
- Contains only abstract methods that must be implemented.
- Provides a blueprint for behavior that can be shared across classes without using inheritance.
- Example: A Dog class implements a Runnable interface because a dog “can run.”

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