CS100A Lecture 21 12 November... Prelim 3 Programming assignment 9: Tuesday evening, 17 November, 7:30--9PM

advertisement
CS100A Lecture 21 12 November 1998
•Prelim 3
Tuesday evening, 17 November, 7:30--9PM
Uris Auditorium
•Programming assignment 9:
– a “control” program
– threads of execution
CS100A, Lecture 21, 12
November 1998
1
Prelim 3 Coverage
1. You should KNOW
linear search (lecture 14, slide 4)
binary search (lecture 14, slides 5--9)
selection sort (lecture 15-17, slides 2--3)
insertion sort (lecture 15-17, slide 4)
partition (lecture 15-17, slide 5-6
exponentiation (lecture 15-17, slide 17)
For example. if we say “What is selection sort?”, you state
the problem, give the loop invariant, and develop the loop.
2. One-and two-dimensional arrays in Java.
3. Loops (and invariants), especially those that use arrays
4. Matlab: --know the basics and be able to write simple
Matlab program segments.
5. Everything that was on prelims 1 and 2: See
(a) Slide 2 for lecture 13 (15 October)
(b) Slide 12 of lecture 8 (24 September)
CS100A, Lecture 21, 12
November 1998
2
Real-time application
Often software that is running on a host computer,
communicating with several microprocessors, each of
which senses some physical property or controls a
device.
Communicate through a fixed memory location in the
host, called a register.
Program Status word (psw) used to synchronize. E.g.
Host might do the following:
Set bit C of psw to 1
Wait for C to be set to 0 (by a microprocessor)
Read the register
We simplify communication using a method readValue.
CS100A, Lecture 21, 12
November 1998
3
Your Assignment: Simulating Cruise Control
Clock: Ticking away
The accelerator: Will be switched on and off by the
program
The desired speed: something you change by inputting a
value.
The road grade: something that you can change by
inputting a value.
The current speed: maintained by your program.
The main program: The host: Behind the scenes,
controlling the other components.
CS100A, Lecture 21, 12
November 1998
4
Threads of Execution
Computer executes one sequence of instructions at a
time, so don’t we need 6 computers, one for each of the
components?
We simulate the system using one computer using
threads of execution.
The computer alternates among these threads, giving
each some execution time, but so fast that it looks to us
like they are executing in parallel.
Clock
Desired speed
real speed accelerator
CS100A, Lecture 21, 12
November 1998
5
Thread of execution
public class SystemClock extend Frame
implements runnable {
haven’t
discussed
// Class must have this method. It is called by system
// start execution of the thread
public void run( ) {
Sequence of instructions making up the thread
}
}
public class MainClass {
static SystemClock c;
static Thread cThread;
static public void main ( ) {
c= new SystemClock( );
cThread= new Thread(c);
// Call method start of cThread; in turn, that method
// will call method run of c to start the thread running
cThread.start( );
...
}
CS100A, Lecture 21, 12
6
}
November 1998
Synchronization
Two threads may need to synchronize --pause during
execution until they are at appropriate places.
Example: The accelerator thread may want to wait for
the next clock tick (handled by thread SystemClock)
before proceeding.
•c.wait: wait for c’s thread to execute a notifyAll call
•Within c: execution of notifyAll( ) tells the system
that all threads who have executed c.wait can now
continue.
•Property synchronized on a statement or method
within a class indicates that no other method or field of
this class can be executed while this statement or
method is being executed.
CS100A, Lecture 21, 12
November 1998
7
Synchronization
SystemClock c;
…
// Wait for next tick of clock c
static public void sleep ( ) {
synchronized(c) {
c.wait ( );
}
--------------------------------------public class SystemClock ( ) {
public void run ( )
…
// Each iteration of the this loop “sleeps” for 5 seconds
// notifies all waiting threads that they can continue,
// and adds 1 to the clock counter
while (true) {
Thread.currentThread ( ).sleep(5000);
synchronized(this)
{notifyAll( );}
counter= counter+1;
}
}
}
CS100A, Lecture 21, 12
8
November 1998
Download