More on Threads

Multi-thread applications make use of the fact that many tasks contain separate and distinct subtasks.

One thread can be used for each subtask.

We are dealing with concurrent processes

suppose parallel

For example, consider that reasoning methods may be categorized into types such as

Consider

Can we do anything concurrently?

Are there precedence constraints?

Precedence on the data...

If there are few such constraints, one could write a largely parallel algorithm. Often there are such constraints.

At a quite abstract level:

In any communication (man to man, machine to machine, man to machine), we have protocols. In the past, machines have been sequential single processors. When they have the ability to do more than one thing at once, what new things can they do? Do we still need to tell them what to do? How much can they do on their own?

In artificial intelligence we have the idea of intelligent agents and a blackboard. (their monitor) (consider implementation tools of Java Spaces and heredown for image)

Blackboard models usually consist of three components

  1. the knowledge sources (intelligent agents) (independent)

  2. the blackboard data structure

  3. control

The problem solving state data is kept in a global data base, the blackboard. Knowledge sources produce changes to the blackboard which lead incrementally to a solution to the problem. Communication and interaction among the knowledge sources take place soley through the blackboard.

For control, the knowledge sources respond opportunistically to changes in the blackboard.

What does this mean? Changes to the blackboard trigger knowledge source activity.

Knowledge sources can be schematized as condition-action pairs.

The condition and action components of a KS are realized as arbitrary methods. To minimize reevaluating the condition programs continuously, each condition program declares to the system the primitive kinds of situations in which it is interested. The condition method is triggered only when there occurs changes that create such situations (and is then given references to all of them).

This changes a polling action into an interrupt-driven one and is more efficient.

When executed, the condition method can search among the set of existing hypothetical interpretations for arbitrarily complex configurations of interest to its KS

See Hearsay-II pg 219

various ideas -

More computer science concrete: Dealing with resources

I am taking some of this out of Operating System Concepts, Peterson and Silberschatz

A sequential program can be in one of four states:

Figure for state diagram of a process (page 320)

We, of course want to prevent or avoid deadlock. If one has precedence constraints, then whether you process in parallel or sequentially, you still need to deal with this.

The Critical Section Problem

A critical section is a section of code that must be allowed to complete atomically with no interruption that affects its successful completion or the data it is working on. Such things as context switching a thread or updating a record in a database are critical sections. Other things may go on at the same time, and the thread that is executing in the critical section may even lose its processor, but no other thread may do anything that affects the critical section. Should another thread want to execute that same critical section, it is forced to wait until the first thread finishes.

Critical sections are typically made as short as possible and often carefully optimized because they can significantly affect the concurrency of the program. They are often used to prevent data corruption of shared resources.

Synchronization mechanisms:
For us in java, a critical section is the same as a synchronized method or block.

Two main functions with respect to shared resources:

Monitor - If one wants concurrent processes then you need to keep the resources and the monitor separate.

Monitor also has the shared object (or data members). If one can have multiple threads accessing data simultaneously, all shared data must be locked and provide synchronized methods to provide access to the object. This synchronization is vital to maintaining the integrity of any shared objects.

Consider - why isn't synchronized stuff in resources? (Usually if one takes time to think about these things they make sense.)

Example Resources :

Appropriate monitor operations must be involved before and after resource access

Monitors typically WAIT, SIGNAL, QUEUE

Mutual Exclusion

if a process is executing in its critical section then no other process can be executing in its critical section

The Dining Philosophers Problem: Dijkstra (page 347)

The problem: deadlock. Suppose all pick up the right chopstick at the same time. These hardworking philosophers could starve to death

Some possible solutions for you to implement

Why did I even bring this up? communication, Shared memory and synchronization. SUN and Java now have Jini, Java Spaces and Java Message Services to work with these ideas as well.

Controlling thread order - the Scheduler
(page 367 and 372 Java in 21 days)

Priorities:

setPriority - from 0 to 10

Synchronization in Java is about 5 times more expensive to call. Use sparingly.