Outline Please check the web page about your recitation group s - - PowerPoint PPT Presentation
Outline Please check the web page about your recitation group s - - PowerPoint PPT Presentation
Outline Please check the web page about your recitation group s meeting place. First group meeting: this Thursday (13:15) Terminology Exceptions Control flow -- the order of execution throw and catch
– 3 :: 2 – 0024 Spring 2010
Outline
Please check the web page about your recitation groups meeting place. First group meeting: this Thursday (13:15) Terminology Exceptions Control flow -- the order of execution
- “throw” and “catch”
– 3 :: 3 – 0024 Spring 2010
Terminology
Program -> sequence of operations
Translation is done by the compiler We may later see exactly what operations are executed
We are for now interested in atomic operations
– 3 :: 4 – 0024 Spring 2010
Java model
– 3 :: 5 – 0024 Spring 2010
Java rules
Simple “print"
– 3 :: 6 – 0024 Spring 2010
Java rules
– 3 :: 7 – 0024 Spring 2010
Java model
– 3 :: 8 – 0024 Spring 2010
Simple Thread class
class TwoThreads { public static void main(String args[]) { SimpleThread st1 = new SimpleThread(); SimpleThread st2 = new SimpleThread(); st1.start(); st2.start(); } /* main */ }
– 3 :: 9 – 0024 Spring 2010
Simple Thread class, part 2
class SimpleThread extends Thread { public void run() { System.out.println(“Started.”); } /* run */ }
– 3 :: 10 – 0024 Spring 2010
Variant
class TwoThreads_2 { public static void main(String args[]) { Thread t1 = new Thread(new MyRunnable()); Thread t2 = new Thread(new MyRunnable()); t1.start(); t2.start(); } /* main */ }
– 3 :: 11 – 0024 Spring 2010
Variant, part 2
class MyRunnable implements Runnable { public void run() { System.out.println(“Started.”); } /* run */ }
– 3 :: 12 – 0024 Spring 2010
Atomic operations
An activity is called atomic if it completes without the possibility of interruption int j, k; j = k + 1; Is this (Java) statement executed atomically? int j, k; j = j + 1; Is this (Java) statement executed atomically?
– 3 :: 13 – 0024 Spring 2010
Atomic operations
int j, k; j = j++; Is this (Java) statement executed atomically? Who decides what operations are done atomically?
- Ideally, the language designers have specified
exactly what operations must be executed atomically.
– 3 :: 14 – 0024 Spring 2010
Atomic operations
For now, we accept that reading or writing an int value is done atomically. And nothing else.
Not completely true. All 32-bit quantities must be written
atomically.
Some details are missing.
– 3 :: 15 – 0024 Spring 2010
Primitive data types in Java
Java provides a rich set of primitive types Weve seen int. Variables, stored in 32 bits. int j, k; There are “long int” and “double” -- both require 64 bits
- f storage.
– 3 :: 16 – 0024 Spring 2010
Machine instruction not atomic
(Normal) machine instruction are not atomic
Several operations E.g., addl 4(%ebp), %eax // add register and memory
Compute effective address (4+ebp) Get value from memory Get value from %eax Add Update %eax
Interruption possible after each step Possibility of interruption implies that the machine
instruction does not execute atomically.
– 3 :: 17 – 0024 Spring 2010
Why looking at details?
Well stay at as high a level as possible but sometimes we must look “under the hood”
Most of the time source statements (or even methods) are a
good abstraction level
When we want to understand parallel execution then we
must understand which operations are executed atomically.
Well later see how we can ensure the atomic execution of
sequences of operations (even methods)
– 3 :: 18 – 0024 Spring 2010
Terminology, continued
Thread
Model of execution of a sequence of operations “Program counter” (PC) + state
Program counter - tells us what operation to do next
State = local variables (sometimes in registers)
Address (data) space
Sequence of memory locations
Abstract memory cells No information about access time
All memory locations have the same protection properties
Access rights, kind of address space (read-only, execute, ….)
Threads work with one (or more) address spaces
Each thread has a private address space for its private data Some of the address spaces may be shared among threads
– 3 :: 19 – 0024 Spring 2010
Terminology, continued
Process consists of one (or more) threads and one (or more address spaces) Job: collection of processes (Essential) Operating System (OS) services
Thread management
Creation, execution, interruption, termination, …
Address space management
Creation, mapping to real storage, setting/revoking access
rights, space reclamation, …
Process management Inter-process communication (IPC)
Threads communicate via shared address space(s)
– 3 :: 20 – 0024 Spring 2010
Threads and processes
A simple model (but good for many scenarios)
1 thread (“init thread” -- started by OS) 1 address space for private data (i.e. stack) (for init thread) 1 address space for thread (private) instructions 1 address space for shared libraries 1 address space for object instances
A slightly more sophisticated setup
1 thread (“init thread”) -- started by OS 1 .. N-1 threads started by init thread N address spaces for thread-private data N address spaces for thread (private) instructions 1 address space for shared libraries 1 address space for object instances
OS may not use (full) address space but just use different segments
– 3 :: 21 – 0024 Spring 2010
Thread
Thread
Model of execution of a sequence of operations “Program counter” (PC) + state
Program counter - tells us what operation to do next
State = local variables
class T1 { public static void main(String args[]) { int k; k = 0; k = k + 1; } } (at least) 4 Operations: update k, read k, add one, update k
– 3 :: 22 – 0024 Spring 2010
Detour: static methods
class S1 { public int static foo () { return 1; } } class S2 { public void static main(String args[ ]) { System.out.println(S1.foo()); } } Static methods can be invoked with class name.
You dont need an object instance
– 3 :: 23 – 0024 Spring 2010
Why bother?
Java knows constructors -- special method to produce an instance of a given class.
Great if you need one instance. What if you need two instances. Or two instances from
different classes.
Constructor(s) wont do the job.
Static methods help in such a situation.
Can impose rules on the usage/construction of object
instances.
Also useful at the start of a program -- when there are no
- bjects yet.
– 3 :: 24 – 0024 Spring 2010
Example revisited
class S1 { public int foo () { /* not static */ return 1; } } class S2 { public void static main(String args[ ]) { int j; S1 sref = new S1(); j = sref.foo() System.out.println(j); } }
– 3 :: 25 – 0024 Spring 2010
Comments
Default constructor -- S1(); Reference variable to instance of S1 -- sref “new” operator with constructor X() yields a reference to an instance of class X. “j” and “sref” are local variables (local to method main)
– 3 :: 26 – 0024 Spring 2010
Operations in example
1 (or more) to get reference to instance of S1
Plus whatever it takes to produce an instance of S1
1 (or more) to access sref 1 (or more) to invoke foo() 1 (or more) to get the result from foo() 1 to update j 1 (or more) to access System 1 (or more) to access field out 1 (or more) to invoke println() 1 (or more) to return
– 3 :: 27 – 0024 Spring 2010
More comments
Our goal is not to count operations. Want to understand that many operations are not atomic and that a thread can be interrupted often.
– 3 :: 28 – 0024 Spring 2010
Terminology, continued
Address (data) space
Sequence of memory locations
Abstract memory cells Can store values
» Dont worry for now about details
No information about access time
All memory locations have the same protection properties
Access rights, kind of address space (read-only, execute, ….) Not important for us right now -- just for the record
public void static main(String args[ ]) { int j; S1 sref … } Space for j and sref
– 3 :: 29 – 0024 Spring 2010
Data space
S1 sref = new S1(); System will find space to store instance of S1. sref can be stored, passed as an argument (to a method), or copied. No other manipulation is allowed.
– 3 :: 30 – 0024 Spring 2010
Terminology, continued
Threads work with one (or more) address spaces
Each thread has a private address space for its private data Some of the address spaces may be shared among threads
Private data: stored in the stack
Well look in the 3rd semester at stacks.
Threads that can read a reference can also access the
- bject the reference refers to.
– 3 :: 31 – 0024 Spring 2010
Terminology, continued
Process consists of one (or more) threads and one (or more address spaces) Job: collection of processes (Essential) Operating System (OS) services
Thread management
Creation, execution, interruption, termination, …
Address space management
Creation, mapping to real storage, setting/revoking access
rights, space reclamation, …
Process management Inter-process communication (IPC)
Threads communicate via shared address space(s)
– 3 :: 32 – 0024 Spring 2010
Threads and processes
A simple model (but good for many scenarios)
1 thread (“init thread” -- started by OS) 1 address space for private data (i.e. stack) (for init thread) 1 address space for thread (private) instructions 1 address space for shared libraries 1 address space for object instances
A slightly more sophisticated setup
1 thread (“init thread”) -- started by OS 1 .. N-1 threads started by init thread N address spaces for thread-private data N address spaces for thread (private) instructions 1 address space for shared libraries 1 address space for object instances
OS may not use (full) address space but just use different segments
– 3 :: 33 – 0024 Spring 2010
Model of execution
Thread instructions: a sequence of statements
Or operations that the statements have been translated into
After finishing one statement the next statement is the sequential successor statement. Statement 0; … Statement j; Statement j+1; …. Transition Statement_j --> Statement_j+1: control transfer
– 3 :: 34 – 0024 Spring 2010
Control flow changes
Control flow: sequence of control transfers Simple control transfers: S_j and S_(j+1) adjacent Conditional control transfers: if (N == M) { System.out.println(“Equal”); } else { System.out.println(“Not Equal”); }
– 3 :: 35 – 0024 Spring 2010
Other control flow changes
Method invocation Method return Switch statement Break/jump statement Loops
Hide the control transfer
All these transfers have in common that they are happen in a “predicatable” fashion.
If you inspect the program you can see what is going on.
– 3 :: 36 – 0024 Spring 2010
Exceptional control flow changes
What if your program must react to an “unpredictable” situations? Read a file. Encounter end of file. Run a program. Exceed your time slot. Add two numbers. Encounter an overflow.
– 3 :: 37 – 0024 Spring 2010
Exceptions
Exception is shorthand for the phrase "exceptional event” A method encounters an exception. It creates an “exception object” and hands it off to the runtime system for handling.
Exception objects are objects -- instances of some
appropriate class
Handing off the exception object requires special statement.
– 3 :: 38 – 0024 Spring 2010
Throwing an exception
Handing off the exception object is called throwing an exception Java contains the throw clause Exception e = new Exception(); throw (e);
– 3 :: 39 – 0024 Spring 2010
Consider this setup
A call chain: Main has invoked a method. This method invoked another method. The method invoked last encounters an error and throws an exception.
– 3 :: 40 – 0024 Spring 2010
Looking for someone to handle an exception
– 3 :: 41 – 0024 Spring 2010
Catching an exception
To handle an exception it must be “caught”. Java has a catch clause that will catch exceptions of a certain type
Type = class
catch (<ExceptionType> <name>) .. For example catch (Exception e_caught) …
If you catch type X you also catch all subtypes of X.
– 3 :: 42 – 0024 Spring 2010
Java rules
Code that can throw exceptions must be enclosed in a try statement: try { <statements> } catch (ExceptionType1 name) { <handler for exceptions of type1> } catch (ExceptionType2 name) { <handler type2> }
– 3 :: 43 – 0024 Spring 2010
Kinds of exceptions
Java knows 3 types of exceptions
Checked exceptions -- exceptions a programs wants to
recover from
Open a non-existing file (user input error)
Errors -- exception beyond the programs control
Hard disk error (block cannot be read)
Runtime exceptions -- violation of logic of program
Null pointer access Could be handled but better to eliminate the bug