Computer Programming: Multitasking and Threads
5 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What describes multitasking in computer programming?

  • Running a single process at a time to completion.
  • Executing multiple processes simultaneously. (correct)
  • Queuing processes to run one after another.
  • Only running background processes.
  • What happens when a thread sleeps?

  • It continues to consume CPU resources.
  • It can be interrupted if an exception occurs. (correct)
  • It stops executing completely until awakened.
  • It can no longer be interrupted by the main program.
  • In the context of threading, what does the 'start' method do?

  • It initializes a new thread to execute the task. (correct)
  • It creates a new instance of the Runnable interface.
  • It runs the task specified in the same thread.
  • It terminates an existing thread.
  • What effect does a time limit have on a thread's processing?

    <p>It restricts the thread to a specific wait duration.</p> Signup and view all the answers

    What can happen to a thread that is interrupted while sleeping?

    <p>The thread can handle the interruption and execute its task.</p> Signup and view all the answers

    Study Notes

    Threads

    • Threads are a fundamental concept in Java
    • Multitasking allows multiple programs to run concurrently; in reality, the operating system switches between programs very quickly
    • Multithreading allows one program to perform multiple tasks seemingly simultaneously
    • A process is a “program” that can be multitasked
    • Multithreaded programs (or concurrent programs) perform multiple tasks within the same process
    • Each task in a multithreaded program is a thread
    • Processes have their own CPU, memory, registers etc but threads share the same memory space
    • Multithreading is useful in tasks like web browsing (handling multiple requests/files) or in garbage collection, where Java runs a background thread to manage memory

    Objectives

    • Multitasking vs Multithreading
    • Creating threads
    • Thread states
    • Interrupting threads
    • Thread priorities and scheduling
    • Threads sharing data
    • Threads synchronization
    • Deadlocks

    Multitasking

    • Multitasking is common in modern operating systems.
    • The OS switches between programs rapidly giving the impression of simultaneous execution.
    • Only one program runs at a time.

    Multithreading

    • Each program (process) in multitasking can be divided into multiple threads.
    • Threads execute simultaneously within the same process.
    • Threads share data within the same program, unlike processes which do not share data.

    Threads vs Processes

    • Processes run in their own contexts, with their own CPU, memory, and registers.
    • Threads within a process share the same memory and data.

    Advantages of Multithreading

    • Web browsers can handle communications with multiple hosts, editing emails and displaying images, and downloading files all at once.
    • Java uses multithreading in tasks like garbage collection to manage memory efficiently.

    Concurrent Programming

    • A concurrent program contains multiple threads.
    • A parallel program uses multiple processors to execute multiple tasks.
    • In concurrent programs, only one thread executes at a time, even though multiple appear to be running simultaneously.
    • The thread scheduler determines which thread runs next, and how long it runs.

    Example of two threads

    • Programs can move money between bank accounts, using a class that has the account balance data.
    • The transfer method moves money between two accounts.
    • Example code to handle threads

    Example of Two Threads - Steps

    • Place the code for a task into the run() method of a class that implements the Runnable interface.
    • Create a Thread object from the Runnable object.
    • Start the thread using the start() method.

    Example of Two Threads (continued)

    • A separate thread is created for the transfer of funds.
    • Code is added to the run method to transfer money between accounts at regular intervals.
    • Code includes a try...catch block to catch interrupted exception. Use of a variable Thread1

    Thread Lifetime

    • Threads can be in one of six states: new, runnable, blocked, waiting, timed waiting, or terminated.
    • States are explained accordingly in the sections that follow.
    • The getState() method gets the current thread state.

    Thread States: New

    • When a thread is created using the new operator, it's in the new state.
    • The program hasn't started executing the code within the thread's methods.
    • Calling the start() method moves the thread into the runnable state.

    Thread States: Runnable

    • A thread is ready to execute in the runnable state.
    • The thread might not be currently running on the CPU/processor.
    • The OS schedules the thread.

    Thread States: Blocked and Waiting

    • If a thread tries to acquire an object (lock) held by another thread, it becomes blocked.
    • It becomes unblocked when other threads release the lock.
    • The thread waits for another thread to notify the scheduler of a condition, which enters waiting state.
    • Multiple methods have a timeout parameter.

    Thread States: Terminated

    • A thread ends naturally when its run() method finishes.
    • An uncaught exception also terminates a thread.
    • There are transitions between these states:
      • Blocked or waiting threads can become runnable.
      • A reactivated thread compares priority with currently running threads.
        • If higher priority, it preempts a current thread and runs.

    Interupting Threads

    • The stop() method is now deprecated.
    • Use the interrupt() method to request termination.
    • Thread state and the interrupted status can determine when a thread is interrupted.
    • Thread.currentThread().isInterrupted() can determine if a thread has been interrupted
    • Throwing and catching InterruptedException can handle a thread interrupt during blocking operations

    Daemon Threads

    • A daemon thread runs solely for the benefit of other threads. This is similar but doesn't prevent JVM from terminating, such as the garbage collector.
    • Daemon threads don't prevent the JVM from exiting. At least one non-daemon thread must exist for the program to continue.
    • Set thread to be daemon by t.setDaemon(true) before t.start()
    • Check if a thread is a daemon using isDaemon().

    Thread Priorities

    • Every thread has a priority.
    • By default, a thread inherits the priority of the thread that created it.
    • Set Priority using setPriority().
    • Priorities are mapped to OS priorities, but in some JVMs not all priorities are supported.
    • OS determines priority levels, hence priority is dependent on the platform being used.

    Waiting for Thread Completion - join()

    • The join() method waits for a thread to complete.
    • If the thread is already terminated, or hasn't begun, join() returns immediately.
    • The join() method can be interrupted so catch the exception.
    • join() with a specified timeout can determine whether a specific thread finished within a certain time limit or throws an exception

    The Race Condition

    • Multiple threads share VM memory.
    • Problems can arise if threads access and modify shared data simultaneously.
    • Problems like an incorrect total balance can arise during concurrent updates.
    • Concurrent updates to shared data may yield incorrect results.
    • Using the synchronized keyword makes sure only one thread is accessing modifiable data at a given time/

    The Race Condition (further)

    • The order of operations might not yield a valid result.
    • Using examples like transferring money simultaneously could result in losses or incorrect totals
    • There are techniques that protect a code block/program from concurrent updates

    Lock Objects

    • Use ReentrantLock to protect critical sections of code.
    • lock() acquires the lock, and unlock() releases it.
    • finally block encloses the unlock operation to prevent deadlock if an exception occurs within the critical section.
    • Only one thread can access a locked object's critical section at a time.

    Condition Objects

    • Use condition objects to manage threads that have acquired a lock but cannot do work immediately.
    • If not enough money exists, the thread calling await() is put into the wait set.
    • signalAll() in the synchronized block releases all threads that are waiting on this object, or condition.
    • signal() releases a single thread waiting on the condition object.

    Deadlock

    • Deadlock occurs when threads are blocked indefinitely due to the program design, waiting for each other to unlock objects or resources
    • Examples include multiple accounts, with different threads trying to access and potentially modify them simultaneously

    Blocking Queue

    • Java BlockingQueue is a queue that ensures thread safety during concurrent access.
    • Methods like put() and take() are blocking, pausing execution when a queue is full or empty.
    • Other methods like offer() and poll() return a fail status instead of blocking if the queue is full or empty.
    • These methods can be time-limited.
    • Often used to implement producer-consumer patterns.

    Producer-Consumer Problem

    • The producer-consumer problem is a classic multithreading scenario.
    • Two threads, a producer and a consumer, share a common buffer.
    • The producer adds items to the buffer, and the consumer removes them.
    • Use a BlockingQueue to handle the concurrent accesses

    Synchronized Keyword

    • Use synchronized on methods or code blocks to protect shared resources.
    • This ensures that only one thread can access the protected code section at any given time/

    Synchronized Blocks

    • The critical code section that is synchronized via a block is protected by the lock of the object referenced by obj, the object given to the synchronized block.

    Deadlock Situations

    • Situations such as in Example 3 (ThreadLock-slide 81), which tries to transfer a larger number of operations, can result in a deadlock if threads access and modify shared data concurrently, not following the needed procedures.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Threads PDF

    Description

    This quiz explores key concepts of multitasking in computer programming, focusing on threads and their behaviors. Discover what happens when threads sleep, the significance of the 'start' method, and the impact of time limits on thread processing. Test your knowledge on thread interruptions and their effects.

    More Like This

    Multi-threading in Java Networking
    16 questions
    Python Threading and Parallel Processing
    10 questions
    Taps and Dies for Threading
    6 questions
    Threading Tools Basics
    44 questions

    Threading Tools Basics

    HardWorkingFoxglove avatar
    HardWorkingFoxglove
    Use Quizgecko on...
    Browser
    Browser