===== CS 235 - Week 9 Lecture - 2021-10-18 ===== ===== TODAY WE WILL ===== * announcements * start intro to Java threads * prep for next class * Reading: * "Core Java", 11th edition, Chapter 12 - Sections 12.1, 12.2, 12.3 * Chapter 10 - Section 10.3 - related to Graphics class and the ImageIcon class * should be starting Homework 6 ===== * intro to THREADS! * process: (rough definition) a program in execution, has its own memory, its own address space thread: (rough definition) a "lightweight" process - a flow of control in a program (which can have any number of such flows of control), but these flows of control all share a SINGLE address space; * since Java supports threads, it is said to be multithreaded * Java has a Thread class -- interestingly, though, the preferred way nowadays to set up a thread is by implementing a Runnable interface * interface! what does the Runnable interface require? a method run BUT! Runnable is also a so-called *functional* interface, so it can be set up using a lambda expression...! * WARNING: Java has had threads since its beginning, BUT it has also "learned from experience" and some deceptively-useful- sounding Thread methods are now deprecated and should NOT be used; (CHECK the age of that Thread example you look up before you decide to use it as a basis for code NOW...!!!) * esp.: Thread.stop(), Thread.suspend(), and Thread.resume() (see p. 779 for why these are deprecated) * There are two ways of setting Threads -- we'll stick with the version recommended in the course text, implementing the Runnable interface and creating and starting up a thread from that; * the other: defining a subclass of the Thread class -- not recommended, because it is considered better to decouple the *task* that is to be run in parallel from the *mechanism* for running it; * from "Core Java", 11th ed, pp. 734-735: * an approach for running a task in a separate thread: 1. Place the code for the task into the run method of a class that implements the Runnable interface. ...Runnable interface just requires a run method: void run(); BUT! because Runnable is a functional interface, you are also NOW also allowed to make an instance using a lambda expression: Runnable myRunnable = () -> { task_code }; 2. Construct a Thread object from a Runnable instance: Thread myThread = new Thread(myRunnable); * you want to name your thread? There's a Thread constructor for that, too: thread myNamedThread = new Thread(myRunnable, "desired name"); 3. Start the new thread object by calling its start method: myThread.start(); (start method starts up a new thread and calls the thread's run method, the one it "got" from its Runnable argument...) * fun opinion: you really don't typically want to call the Thread's run method directly, because that will call and execute that run method's tasks in the current, calling thread...! (see course text, p. 736) * Fun fact: threads can be in one of SIX states: (p. 739) * New * Runnable * Blocked * Waiting * Timed waiting * Terminated to determine the current state of a thread, you can call its getState method * when does a thread terminate? ...when its run method returns; * by executing a return statement * after executing the last statement in its method body * if an exception occurs that is not caught in the method * IF a sub-method of a run method's actions is calling sleep, DON'T squash the potential InterruptedException! ...have the sub-method THROW it instead: void mySubTask throws InterruptedException { ... // no try-catch required now, because ^^ throws the exception // if it occurs sleep(delay); ... }