ch14

Cards (19)

  • Multithreading
    • In Java, programs can have multiple threads
    • A thread is a separate computation process
    • Threads are often thought of as computations that run in parallel
    • Although they usually do not really execute in parallel
    • Instead, the computer switches resources between threads so that each one does a little bit of computing in turn
    • Modern operating systems allow more than one program to run at the same time
    • An operating system uses threads to do this
  • Life Cycle of a Thread
    • New
    • Runnable
    • Waiting
    • Timed waiting
    • Blocked
    • Terminated (Dead)
  • New
    A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
  • Runnable
    After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task (or is ready to run in the process queue).
  • Waiting
    Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
  • Timed waiting
    A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
  • Blocked
    A blocked thread is about to enter a synchronized block, but there is another thread currently running inside a synchronized block on the same object. The first thread must then wait for the second thread to exit its block. A synchronized block in Java can only be executed by a single thread at a time.
  • Terminated (Dead)

    A runnable thread enters the terminated state when it completes its task or otherwise terminates.
  • Advantages of Java Multithreading
    • It doesn't block the user because threads are independent and you can perform multiple operations at the same time
    • You can perform many operations together, so it saves time
    • Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread
  • Java Thread class
    • Java provides Thread class to achieve thread programming
    • A thread is an object of the class Thread
    • Thread class extends Object class and implements Runnable interface
    • Usually, a derived class of Thread is used to program a thread
    • Thread class provides constructors and methods to create and perform operations on a thread
    • The methods run and start are inherited from Thread
    • The derived class overrides the method run to program the thread
    • The method start initiates the thread processing and invokes the run method
  • Java Thread Methods
    • start()
    • run()
    • sleep()
    • isAlive()
    • join()
    • getPriority()
    • setPriority()
    • getName()
    • setName()
    • getId()
    • resume()
    • stop()
    • destroy()
    • toString()
    • interrupt()
    • checkAccess()
  • Thread.sleep
    • Thread.sleep is a static method in the class Thread that pauses the thread that includes the invocation
    • It pauses for the number of milliseconds given as an argument
    • It may throw a checked exception, InterruptedException, which must be caught or declared
  • The getGraphics Method
    • The method getGraphics is an accessor method that returns the associated Graphics object of its calling object
    • Every JComponent has an associated Graphics object
    • The following program contains a simple GUI that draws circles one after the other when the "Start" button is clicked
    • There is a 1/10 of a second pause between drawing each circle
    • If the close-window button is clicked, nothing happens until the program is finished drawing all its circles
    • The actionPerformed method does not end until after the method fill ends
    • And, until the method actionPerformed ends, the GUI cannot go on to do the next thing, which is probably to respond to the close-window button
    • To fix the problem, have the actionPerformed method create a new (independent) thread to draw the circles
    • Once created, the new thread will be an independent process that proceeds on its own
    • Now, the work of the actionPerformed method is ended, and the main thread (containing actionPerformed) is ready to respond to something else
    • If the close-window button is clicked while the new thread draws the circles, then the program will end
    • The following program uses a main thread and a second thread to fix the nonresponsive GUI
    • It creates an inner class Packer that is a derived class of Thread
    • The method run is defined in the same way as the previous method fill
    • Instead of invoking fill, the actionPerformed method now creates an instance of Packer, a new independent thread named packerThread
    • The packerThread object then invokes its start method
    • The start method initiates processing and invokes run
    • Another way to create a thread is to have a class implement the Runnable interface
    • The Runnable interface has one method heading: public void run()
    • A class that implements Runnable must still be run from an instance of Thread
    • This is usually done by passing the Runnable object as an argument to the thread constructor
  • The GUI produced is identical to the GUI produced by previous example except that in this version the close-window button works even while the circles are being drawn, so you can end the GUI early if you get bored