- Forward


Independent Threads
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Background
Back SMYC Forward
  • Multi-Tasking:
    • More than one application can be run "at the same time"
  • Multi-Threading:
    • One application can run more than one method/function "at the same time"
Some Informal Definitions
Back SMYC Forward
  • Object State:
    • The information stored in an object's attributes (both static and non-static)
  • Shared State:
    • An attribute that could be accessed by multiple threads
  • Mutable State:
    • An attribute that can change over its lifetime
Two Distinct Situations
Back SMYC Forward
  • Shared Mutable State:
    • Requires (often complicated) coordination across threads (and is not considered here)
  • Independent State:
    • Much easier (and the topic of this lecture)
Motivation
Back SMYC Forward
  • A Text Indexing Application:
    • The program creates an index that contains all of the words in the text and all of the lines in which each word appears
  • The Need for Multiple Threads:
    • Processing can take a long time so the user sometimes has to wait to enter the titles
Motivation (cont.)
Back SMYC Forward

The Indexer

javaexamples/threads_independent/v1/Indexer.java
 
Motivation (cont.)
Back SMYC Forward

The User Interface

javaexamples/threads_independent/v1/Lindex.java
 
Motivation (cont.)
Back SMYC Forward
  • What We Need:
    • A way for the call to createIndex() to return quickly
  • An Important Property of the Indexer:
    • The attributes are immutable
  • An Implication:
    • The code required to create an index can run in its own thread of execution (and multiple indexes can be created "at the same time")
One Way to Use Threads in Java
Back SMYC Forward
  • The Relevant Classes/Interfaces:
    • The Thread Class
  • The Design:
    • threads-child
One Way to Use Threads in Java (cont.)
Back SMYC Forward
  • A Conceptual Problem with this Approach:
    • People think the child class is a thread of execution
  • A Practical Problem with this Approach:
    • Java doesn't support multiple inheritance
Another Way to Use Threads in Java
Back SMYC Forward
  • The Idea:
    • Use the Command/Strategy Pattern
  • The Relevant Classes/Interfaces:
    • The Thread Class
    • The Runnable Interface
  • The Design:
    • threads-runnable
A "Threaded" Indexer
Back SMYC Forward
javaexamples/threads_independent/v2/Indexer.java
 
Tracing a Multi-threaded Application
Back SMYC Forward

public class SlasherDriver
{
    public static void main(String[] args)
    {
	Slasher     plus, slash;

1       slash = new Slasher();
6       slash.setCount(3);
8       slash.start();
10      plus = new Slasher("+");
14      plus.setCount(2);
16      plus.start();
    }
}
  
public class Slasher implements Runnable
{
    private int         count;
    private String      symbol;
    private Thread      controlThread;

    public Slasher()
    {
2       this("/");
    }
    
    public Slasher(String symbol)
    {
3 11    this.symbol = symbol;
4 12    count = 0;
5 13    controlThread = new Thread(this);
    }

    public void run()
    {
A C E G for (int i=0; i<count; i++)  a c e
        {
B D F          System.out.print(symbol);  b d
        }
    }

    public void setCount(int count)
    {
7 15    this.count = count;
    }

    public void start()
    {
9 17    controlThread.start();
    }
}
  

Safe Construction
Back SMYC Forward
  • Remember:
    • An object is in a consistent state only after its contrsuctor returns
  • An Implication:
    • The this reference must not be allowed to "escape" during construction
  • In the Context of Threads:
    • Don't call a Thread object's start() method in a constructor (because the Thread object's constructor is passed this)
Daemon Threads
Back SMYC Forward
  • The Idea:
    • A thread that is normally used for background/helper activities
    • A thread that can be safely stopped at any time
  • Java Details:
    • The daemon status of a Thread can be changed using its setDaemon() method
Priorities and Scheduling in Java
Back SMYC Forward
  • An Observation:
    • On a single processor machine only one thread can execute at a time
  • How Java Schedules Threads:
    • Chooses the runnable thread with the highest priority
There's Always More to Learn
Back -