Object Oriented Programming 2 - Concurrency

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Listen to an AI-generated conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What does it mean for two tasks to operate concurrently?

  • They are executing simultaneously.
  • They are both making progress at once. (correct)
  • They are utilizing multiple cores of the processor.
  • They are waiting for each other to complete.

In parallel processing, tasks are executed one after another.

False (B)

What is the term for the capability where an application contains separate threads of execution that can run concurrently, sharing application-wide resources?

multithreading

Java provides concurrency through the language and related ________.

<p>APIs</p>
Signup and view all the answers

Match each term with its correct description:

<p>Concurrency = Tasks making progress at the same time. Parallelism = Tasks executing simultaneously. Multithreading = Multiple threads executing concurrently within a single application.</p>
Signup and view all the answers

What is a primary advantage of multithreaded applications?

<p>They can distribute threads across multiple cores for parallel execution. (C)</p>
Signup and view all the answers

Programming concurrent applications is generally straightforward and free from potential errors.

<p>False (B)</p>
Signup and view all the answers

What Java keyword can be used by advanced programmers to control synchronization, along with Object methods wait, notify, and notifyAll?

<p>synchronized</p>
Signup and view all the answers

The vast majority of programmers should use existing collection classes and interfaces from the concurrency ______ that manage synchronization.

<p>APIs</p>
Signup and view all the answers

Match the following terms related to thread synchronization with their usage:

<p>synchronized keyword = Ensures only one thread executes a block of code at a time. wait method = Causes a thread to wait until another thread notifies it. notify method = Wakes up a single waiting thread.</p>
Signup and view all the answers

What does the synchronized keyword ensure in Java?

<p>That only a single thread can execute a block of code at the same time. (D)</p>
Signup and view all the answers

A thread can only be in one state during its lifetime.

<p>False (B)</p>
Signup and view all the answers

In which thread state does a new thread begin its life cycle?

<p>new</p>
Signup and view all the answers

A thread enters the ________ state after it has been started and is considered to be executing its task.

<p>runnable</p>
Signup and view all the answers

Match the thread state with its description:

<p>New = Thread has been created but not yet started. Runnable = Thread is ready to run or is currently running. Terminated = Thread has completed its execution.</p>
Signup and view all the answers

In what state does a runnable thread transition when it waits for another thread to perform a task?

<p>Waiting (D)</p>
Signup and view all the answers

A thread in the timed waiting state does not use a processor, even if one is available.

<p>True (A)</p>
Signup and view all the answers

What is the state of a thread that is sleeping for a designated period?

<p>timed waiting</p>
Signup and view all the answers

A thread transitions to the ________ state when it attempts to perform a task that cannot be completed immediately.

<p>blocked</p>
Signup and view all the answers

Match the following thread states with their correct descriptions:

<p>Waiting State = Waiting for another thread to perform a task. Timed Waiting State = Waiting for a specified interval of time. Blocked State = Waiting for a lock or resource.</p>
Signup and view all the answers

Which state does a runnable thread enter upon successfully completing its task?

<p>Terminated (B)</p>
Signup and view all the answers

At the operating system level, Java's runnable state encompasses two separate states: ready and running.

<p>True (A)</p>
Signup and view all the answers

What is the process called where an operating system decides which thread should be dispatched (run)?

<p>thread scheduling</p>
Signup and view all the answers

A thread in the ________ state is waiting to be assigned to a processor.

<p>ready</p>
Signup and view all the answers

Match the following terms with their descriptions in the context of operating system thread management:

<p>Ready State = Waiting to be assigned to a processor. Running State = Currently executing on a processor. Thread Scheduling = Process of determining which thread to dispatch.</p>
Signup and view all the answers

What helps determine the order in which threads are scheduled in Java?

<p>Thread priority (B)</p>
Signup and view all the answers

Thread priorities guarantee the order in which threads are executed.

<p>False (B)</p>
Signup and view all the answers

What interface should you use rather than explicitly creating and using Thread objects to implement concurrency?

<p>Executor</p>
Signup and view all the answers

Operating systems often employ ________, which enables threads of equal priority to share a processor.

<p>timeslicing</p>
Signup and view all the answers

Match each term in thread scheduling with its description:

<p>Thread Priority = Helps the OS determine scheduling order. Timeslicing = Equal-priority threads share processor time. Thread Scheduler = Determines which thread runs next.</p>
Signup and view all the answers

What is the term for when a higher-priority thread preempts a currently running thread?

<p>Preemptive scheduling (B)</p>
Signup and view all the answers

Indefinite postponement is also known as thread aging.

<p>False (B)</p>
Signup and view all the answers

What is the name of the operating system technique used to prevent starvation?

<p>aging</p>
Signup and view all the answers

A situation where two or more threads are blocked forever, waiting for each other, is known as ________.

<p>deadlock</p>
Signup and view all the answers

Match the following terms related to thread management with their descriptions:

<p>Preemptive Scheduling = Higher-priority thread preempts a running thread. Starvation = Indefinite postponement of a thread. Deadlock = Threads blocked waiting for each other.</p>
Signup and view all the answers

A lengthy activity in a single-threaded application can lead to poor responsiveness because:

<p>It must complete before other activities can begin. (B)</p>
Signup and view all the answers

Thread scheduling is consistent across different Java implementations.

<p>False (B)</p>
Signup and view all the answers

What single method must be extended in a Thread class object to implement multithreading?

<p>run()</p>
Signup and view all the answers

To create threads by implementing an interface you must implement the _________ interface.

<p>Runnable</p>
Signup and view all the answers

Match the multithreading implementation technique with its description:

<p>Extending Thread Class = Create a class that extends the Thread class and overrides the run() method. Implementing Runnable Interface = Create a class that implements the Runnable interface and provides an implementation for the run() method. Executor Framework = Utilize ExecutorService to manage threads, by creating a thread pool.</p>
Signup and view all the answers

Regarding the execution of multiple threads, when is the program assured to terminate?

<p>When the last thread completes its execution. (A)</p>
Signup and view all the answers

When a higher-priority thread enters the ready state, the operating system generally preempts the currently running thread; this operation is known as 'proactive scheduling'.

<p>False (B)</p>
Signup and view all the answers

Flashcards

Concurrency

Operating two or more tasks, making progress simultaneously.

Parallelism

Tasks operating simultaneously, executing at the exact same time.

Multithreading

Capability of a program to execute multiple threads concurrently, sharing resources.

Main Thread

The single thread where Java programs starts.

Signup and view all the flashcards

Single-Threaded Applications

Applications limited to a single thread.

Signup and view all the flashcards

Multithreaded applications

Applications divided into multiple concurrent threads.

Signup and view all the flashcards

Thread State

A thread's condition at a specific moment during its life cycle.

Signup and view all the flashcards

New State

A thread has been created but not yet started.

Signup and view all the flashcards

Runnable State

A state where a thread is ready to run.

Signup and view all the flashcards

Waiting State

Thread waits for another thread's task completion or notification.

Signup and view all the flashcards

Timed Waiting State

Thread waits for a specified amount of time.

Signup and view all the flashcards

Blocked State

Thread cannot proceed due to resource constraints; waits temporarily.

Signup and view all the flashcards

Terminated State

Thread completed its task or terminated.

Signup and view all the flashcards

Dispatching a Thread

Operating system allocates processor time to ready threads.

Signup and view all the flashcards

Quantum/Timeslice

Amount of time a thread gets to execute before being preempted.

Signup and view all the flashcards

Thread Scheduling

Process by which the operating system determines which thread runs.

Signup and view all the flashcards

Thread Priority

Value that determines the order in which threads are scheduled.

Signup and view all the flashcards

Timeslicing

Scheduler implementation letting equal-priority threads share a processor.

Signup and view all the flashcards

Thread Scheduler

The systems's component that selects the next thread to run.

Signup and view all the flashcards

Round-Robin Fashion

Implementation which lets threads execute for quantum in rotation.

Signup and view all the flashcards

Preemptive Scheduling

OS stops a running thread when a higher-priority thread becomes ready.

Signup and view all the flashcards

Indefinite Postponement

Execution is indefinitely delayed by higher-priority threads.

Signup and view all the flashcards

Starvation

Indefinite Postponement also referred as.

Signup and view all the flashcards

Aging

Technique used by OS to prevent repeated indefinite postponement.

Signup and view all the flashcards

Deadlock

Condition when waiting threads can't proceed due to dependency.

Signup and view all the flashcards

Concurrency Utilities

Java provides utilities to manage complexity and reduce prone errors.

Signup and view all the flashcards

Platform Dependent

Thread scheduling may vary across different Java implementations.

Signup and view all the flashcards

Methods to create threads

Create a class that extends the Thread class or implement Runnable interface.

Signup and view all the flashcards

Runnable Object

Object representing a task that can execute concurrently.

Signup and view all the flashcards

Thread.sleep()

Method to place a calling thread in timed waiting.

Signup and view all the flashcards

Main Thread

Thread that executes the main method.

Signup and view all the flashcards

Main Method

Code in run method executes in the threads created when?

Signup and view all the flashcards

Program Termination

Continues running, because threads are still active.

Signup and view all the flashcards

Executor Interface

Recommend manager that can be used to control Runnable objects.

Signup and view all the flashcards

Thread Pool

A group of threads to execute Runnables.

Signup and view all the flashcards

Executor's execute()

Method that accepts a Runnable as an argument.

Signup and view all the flashcards

Executor's Behavior

Creates a new thread or waits for availability.

Signup and view all the flashcards

ExecutorService Interface

Interface extending Executor, manages the life cycle.

Signup and view all the flashcards

Executors.newCachedThreadPool()

Method that Returns an ExecutorService that creates new threads.

Signup and view all the flashcards

ExecutorService.shutdown()

Method stopping acceptance of new tasks, continues executing submitted tasks.

Signup and view all the flashcards

Study Notes

  • Object Oriented Programming 2 in Java covers concurrency and multithreading.

Concurrency and Parallelism

  • Concurrency involves two tasks making progress at once.
  • Parallelism involves tasks being executed simultaneously.
  • Java supports concurrency through language features and APIs.

Multithreading

  • Applications can contain separate threads of execution.
  • Each thread has its own method-call stack and program counter.
  • Threads can execute concurrently while sharing application-wide resources like memory and file handles.
  • Multithreading is the ability to execute multiple threads concurrently.

Performance Tip

  • Single-threaded applications can suffer from poor responsiveness due to lengthy activities blocking others.
  • Multithreaded applications can distribute threads across multiple cores, allowing parallel execution, and improving efficiency.
  • Multithreading can also increase performance on single-processor systems by allowing another thread to use the processor when one thread is waiting.

Challenges with Concurrency

  • Programming concurrent applications is difficult and error-prone.
  • Most programmers should use existing collection classes and interfaces from concurrency APIs for synchronization.
  • Advanced programmers can use the synchronized keyword, Object methods (wait, notify, notifyAll), Locks, and Conditions for synchronization control.
  • The synchronized keyword ensures only one thread executes a block of code at a time.
  • Threads entering a synchronized block see the effects of previous modifications guarded by the same lock.

Thread States and Lifecycle

  • At any time, a thread can be in one of several states.

New and Runnable States

  • A new thread starts its lifecycle in the new state.
  • It remains in the new state until started, which moves it to the runnable state, considered ready to execute its task.

Waiting State

  • A runnable thread can transition to the waiting state while waiting for another thread to perform a task.
  • Transitions from waiting to runnable occur only when another thread notifies it to continue.

Timed Waiting State

  • A runnable thread can enter the timed waiting state for a specified interval.
  • Transitions back to runnable occur when the time interval expires or when the waited-for event occurs.
  • A thread in the timed waiting state does not use a processor, even if one is available.
  • A sleeping thread is in timed waiting for a sleep interval.

Blocked State

  • A runnable thread transitions to the blocked state when it attempts to perform a task that cannot be completed immediately, requiring it to wait temporarily.

Terminated State

  • A runnable thread enters the terminated state when it successfully completes its task or terminates otherwise, potentially due to an error.

Operating-System View of the Runnable State

  • Java's runnable state includes two separate states an operating system level: ready and running.
  • A thread first transitioning to runnable from the new state is in the ready state.
  • A ready thread enters the running state when the operating system assigns it to a processor, referred to as dispatching the thread.
  • Threads are typically given a quantum or timeslice to perform their task.
  • Thread scheduling is the process determining which thread to dispatch, handled by the operating system and JVM.

Thread Priorities and Thread Scheduling

  • Every Java thread has a thread priority that helps determine the order in which threads are scheduled.
  • Higher-priority threads are generally more important, and should be allocated processor time before lower-priority ones.
  • Thread priorities cannot guarantee the order in which threads execute.
  • It is suggested to use the "Executor" interface to implement concurrency
  • Most operating systems support timeslicing, enabling threads of equal priority to share a processor.
  • An operating system's thread scheduler determines which thread runs next.
  • One thread-scheduler implementation keeps the highest-priority thread running at all times or uses a round-robin fashion if there are multiple highest-priority threads.

Indefinite Postponement and Deadlock

  • When a higher-priority thread enters the ready state, the OS preempts the running thread through preemptive scheduling.
  • A steady influx of higher-priority threads could postpone lower-priority threads indefinitely, referred to as starvation.
  • Operating systems employ a technique called aging to prevent starvation.
  • Deadlock occurs when a waiting thread cannot proceed because it's waiting for another thread to proceed, while that other thread is also waiting for it.

Java and Concurrency

  • Java simplifies concurrency with higher-level utilities, reducing complexity and errors.
  • Thread priorities can be used behind the scenes, but most Java multithreading does not need setting or adjusting of thread priorities.
  • Thread scheduling behavior depends on OS

Ways of Creating and Executing Threads

  • Extend the Thread class.
  • Implement the Runnable interface.
  • Creating and Executing Threads with the Executor Framework.

Extending Thread Class

  • Threads are implemented as objects that contain the run() method.

Implementing the Runnable Interface

  • A Runnable object represents a "task" that executes concurrently with other tasks.

Creating and Executing Threads with the Executor Framework

  • Using the Executor interface is the best method for managing Runnable objects.
  • The Runnable interface declares the run method, which defines the task performed by the Runnable object.
  • When a thread executing a Runnable is created and started, the thread calls the Runnable object's run method, which executes in the new thread.
  • PrintTask class implements Runnable, so multiple PrintTasks can execute concurrently.
  • The thread's sleep method (a static method) puts a thread into a timed waiting state for a specified amount of time.
  • The sleep method can throw InterruptedException if the sleeping thread's interrupt method is called.
  • Code in main is executed in the main thread, created by the JVM
  • Code in the run method of PrintTask executes in the threads created in main.
  • The program keeps running, even after the main method` terminates because the program will not terminate until its last thread completes execution.
  • The executor interface can reuse threads and improve number of threads.
  • The Executor assigns every "Runnable" execute function to a thread.
  • If there are no available threads the executor creates a new thread or waits for a thread.
  • The "ExecutorService" interface extends "Executor" and declares methods for managing the life cycle of an Executor.
  • The "ExecutorsfunctionnewCachedThreadPool` returns a service.
  • ExecutorService has a method called shutdown which will notify when the ExecutorService stops accepting new tasks, but it will keeps executes executing tasks

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser