4.6 Week 6 - OOP -- Java Object Terminology 4 Operator Precedence and Associativity4.4 Week 4 -- Unicode, Floating Point, ...4.5 Week 5 - Threads

4.5 Week 5 - Threads

4.5.1 Reading and Web Sites

  1. Finish reading van der Linden Chapter 6, More OOP -- Extending Classes
  2. Start reading van der Linden Chapter 10, Threads and Chapter 11, Advanced Thread Topics

4.5.2 Notes

  1. The difference between Threads and Processes
    Threads Process
    Memory Space Shared Seperate except with OS calls.
    Page Table Shared Seperate
    Registers Stored on stack Stored on stack
    Preempt-able Maybe Yes.
    Stacks One per thread One per thread
    Processes can have multiple threads.
    Task switch "Fast" "Slow"
  2. Two ways to define a Thread
    1. extends Thread p. 254
    2. implements Runnable
    JavaTMonly allows one inheritance. (C++ allows multiple inheritance.)
  3. To start a Thread, call start(). The method start() will then call your run().
  4. A given Thread can only be successfully started once, but you will not get an exception if you call start() multiple times.
  5. Critical sections
  6. synchronized
  7. Native Threads
    Provided by the operating system. Generally provides better Thread support and faster Thread switching. Also provide less predictable preemption.
  8. Green Threads
    Provided with the Java runtime library. This is actually part of the Java code that you get with the JDK.

4.5.3 Lab Assignments - Threads and Critical sections

  1. Write a main process that starts up six different threads.
  2. Use the extends Thread for three and the implements Runnable for the other three.
  3. Each thread will print 240 copies of its letter, "a" through "f", one at a time. The processes must yield() control after printing each letter (or character and newline).
  4. The class will need a static integer that will keep track of the total number of letters printed. When a process "notices" that it has printed a letter at the "end" of the line (letter_count % 80 == 0) it need to print out a new line character.
  5. Make sure that your access to letter_count is synchronized.
  6. Suggestion:
    1. Do extends Thread first.
    2. Write a separate program for implements Runnable
    3. Merge the two programs.
  7. Suggestion: Try your program without the synchronized code in place.
  8. Extra credit: At the end of each line, in a fixed length format print out the number of each letter of the six (6) letters on the line.
  9. Note: It took me less than 60 non-blank, non-comment lines in three files.
Turn in a copy of your program and two example runs. Are they the same? Why or why not?
Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site

4.6 Week 6 - OOP -- Java Object Terminology 4 Operator Precedence and Associativity4.4 Week 4 -- Unicode, Floating Point, ...4.5 Week 5 - Threads