Object-Oriented Programming SWE211
24 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 must be done to start executing a thread defined by extending the Thread class?

  • Override the main() method.
  • Instantiate the Thread class.
  • Call the run() method directly.
  • Call the start() method. (correct)
  • In the context of threads, what does the run() method represent?

  • A method for stopping the thread.
  • An alternative to the constructor of a thread.
  • A method that initializes the thread.
  • The code that executes when the thread starts. (correct)
  • Which statement is true about the term 'terminate' in relation to threads?

  • A thread terminates once the run() method returns. (correct)
  • A thread terminates when the start() method is called.
  • A thread terminates when it reaches a sleep state.
  • A thread terminates only after the run() method explicitly calls an exit function.
  • What does the following code do? 'MyThread thr1 = new MyThread(); thr1.start();'

    <p>Creates an instance of MyThread and starts its execution.</p> Signup and view all the answers

    What is the purpose of overriding the run() method in a thread class?

    <p>To specify the code that should be executed when the thread starts.</p> Signup and view all the answers

    Which line of code correctly creates and starts a thread in one statement?

    <p>new MyThread().start();</p> Signup and view all the answers

    What does the start() method do when invoked on a thread instance?

    <p>It prepares the thread to execute its run() method.</p> Signup and view all the answers

    In the example provided, what message is printed when the thread runs?

    <p>This thread is running...</p> Signup and view all the answers

    What is the primary role of threads within a program?

    <p>To execute multiple activities simultaneously</p> Signup and view all the answers

    Which of the following accurately describes the main thread in a Java program?

    <p>It is the only thread in a single-threaded program</p> Signup and view all the answers

    What happens when threads are executing concurrently?

    <p>They may share data and results</p> Signup and view all the answers

    Which of the following statements best defines a multithreaded program?

    <p>A program made up of separate tasks running simultaneously</p> Signup and view all the answers

    How do threads interact in a concurrent system?

    <p>Through shared memory space</p> Signup and view all the answers

    In terms of execution, threads can be described as:

    <p>Dynamic processes performing specific tasks</p> Signup and view all the answers

    What is a key benefit of using multiple threads in a program?

    <p>It allows for multitasking within the application</p> Signup and view all the answers

    What does it mean for threads to 'switch' in a programming context?

    <p>To alternate the execution of threads on a CPU</p> Signup and view all the answers

    What is the default thread in any Java program called?

    <p>Main Thread</p> Signup and view all the answers

    How can a thread be created in Java?

    <p>By inheriting from the Thread class or implementing the Runnable interface</p> Signup and view all the answers

    What does the method Thread.currentThread() return?

    <p>The currently executing thread</p> Signup and view all the answers

    What is the priority of the Main Thread by default when it is created?

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

    Which of the following statements about Java threads is correct?

    <p>Threads can run on multiple CPUs simultaneously</p> Signup and view all the answers

    When defining a thread by implementing the Runnable interface, what is a restriction you avoid?

    <p>You cannot extend other classes</p> Signup and view all the answers

    What happens to the execution of threads when they share a single CPU?

    <p>They run in a round-robin fashion with time allocation</p> Signup and view all the answers

    What is a key benefit of implementing the Runnable interface over extending the Thread class?

    <p>You can separate the work of running from the thread management</p> Signup and view all the answers

    Study Notes

    Object-Oriented Programming SWE211

    Multithreading and Sharing Objects

    • Multithreading involves multiple tasks executing concurrently within a program
    • Each thread has a beginning and an end
    • Threads are not static code, but dynamic processes of executing code

    Introduction

    • Threads are separate activities within a program
    • Threads can monitor keyboard input, track mouse movement, and update the screen concurrently
    • Each task (like keyboard monitoring or mouse tracking) can be a separate thread
    • Threads execute code dynamically, not just statically

    Single Threaded Program

    • Most programs written before understanding threading only have one thread (started by main method)
    • A single thread executes in a linear, sequential manner from start to finish

    Multithreaded Program

    • A multithreaded program allows multiple threads to run concurrently
    • Threads can be visualized as independent boxes with a main thread at the top running multiple threads concurrently

    Concurrent System - Threads Concept

    • A collection of activities/tasks occurring simultaneously
    • Each task can be turned into a thread
    • Multiple threads can run genuinely concurrently on multiple processors
    • Threads share the CPU with other threads through allocating time slices
    • The operating system distributes time to the threads

    Main Thread

    • The default thread in every Java program
    • The Java Virtual Machine uses the main thread to execute program statements
    • Example shown finds the specified main thread

    Creating Threads

    • Threads are treated as objects in Java
    • Thread classes are located in the java.lang library
    • Defining methods to create, destroy, and edit threads is possible

    Defining Thread Classes

    • Can be done by inheriting from the Thread class

    • Preferred if you need to inherit only from the Thread class

    • Java does not support multiple inheritance

    • Can be done by implementing the Runnable interface

    • Preferred when you want to inherit from other classes besides the Thread class

    Runnable methods

    • The run() method must be overridden to define the thread's task

    Inherit From the Thread Class

    • Define a class to be extended from the Thread class and make run method specific
    • Create an instance of the class
    • Use thr1.start() to start the thread

    An Example program

    • Demonstrates a basic thread creation
    • Demonstrates how the thread is started

    Implementing the Runnable Interface

    • Define a runnable class by implementing the Runnable interface and overriding the run method
    • Create an instance of the class and a Thread for it
    • Start the thread by calling its start() method

    An Example program (Runnable implementation)

    • Demonstrates a runnable class creation and start thread

    Thread States

    • A thread can be in one of five states: New, Ready, Running, Blocked, or Finished.

    Movement between states

    • The Java system controls the switch in between states.
    • The start() method moves a thread into the runnable state.
    • Threads can be interrupted, yielding CPU time or blocked because of input / output processing
    • In blocked state, the thread stays there until some event occurs to allow it to finish its execution

    wait(), notify(), and notifyAll

    • These are object methods used for thread communication.
    • Must be used within synchronized blocks

    Thread Methods

    • The Thread class in java has methods for various functionalities

    Other Methods

    • A series of methods in Thread class for accessing current thread, sleep, and interrupts

    Thread Priority

    • Each thread has a default priority (Thread.NORM_PRIORITY)
    • You can change the priority using setPriority()
    • Some constants for priority values (MIN_PRIORITY, MAX_PRIORITY, NORM_PRIORITY)

    Thread Priority program

    • An example demonstrating how priority works to show differences in thread output results. Thread output shows differences in order of thread execution when using thread priorities.

    Shared Objects

    • Problems arise when threads access and modify shared data simultaneously
    • Inconsistent states can occur if multiple threads try to update the same shared data simultaneously

    Synchronization & Locks

    • To address shared object problems, synchronize access to shared data with the synchronized keyword.
    • It implements a lock to control access.

    Synchronization Example

    • Modifying the example code above using synchronized blocks corrects the output problem by forcing serialization of access.

    If one thread tries to read the data and other thread tries to update the same data

    • Incorrect states may arise as a result of the threads working on the same data simultaneously.
    • Synchronizing could prevent this.

    Shared account object between 3 threads (Example)

    • Three threads (MyThread, YourThread, HerThread) access a shared account object and perform operations

    Monitor (shared object access)

    • A mechanism to manage access to shared objects in Java
    • It synchronizes operations to prevent concurrent problems

    Synchronizing Statements vs. Methods

    • Synchronizing blocks of code vs synchronized methods. Shows how statements in class can be converted for synchornization
    • The methods are similar in effect, but the latter is cleaner.

    isAlive(), interrupt(), and isInterrupted()

    • Methods for managing thread's state and interrupting threads

    Other Notes

    • Includes example program outputs in detailed explanations.

    Studying That Suits You

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

    Quiz Team

    Description

    This quiz explores concepts related to multithreading and sharing objects within the context of Object-Oriented Programming (OOP). It details the differences between single-threaded and multithreaded programs, emphasizing how threads allow concurrent execution of tasks. Test your understanding of how threads operate and interact in a programming environment.

    More Like This

    Use Quizgecko on...
    Browser
    Browser