Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread... Lecture 18, Nov 03.

Similar presentations


Presentation on theme: "The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread... Lecture 18, Nov 03."— Presentation transcript:

1 The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread... Lecture 18, Nov 03

2 Administrivia P3 reminders: Send me/Nick your group members and team name Schedule P3M1 meeting with us Nov 11 coming up like a freight train… So far, only 1 group has started scheduling process I’m out of town on Mon, Nov 9

3 Now and then Last time: Quiz 2 P2 Rollout P3 handed out; discussion Today: Beginning of Threads Beginning of Graphics2d

4 Threading intro

5 Three models of execution Single process, running alone Multiple access? No worries Can be inconvenient/inefficient -- single user, blocking, etc. Multiple procs, comm via sockets/pipes/etc. More efficient: no blocking, >1 user, etc. Data sharing: hard -- all via messages/files Little synchronization problem

6 Three modes of execution Threads Called “light weight processes” Efficient: >1 user, no blocking, efficient use of resources Data sharing: shared memory Multiple threads read/write same memory space Synchronization: very tricky

7 Multi-process model Process (UNIX sense): Separate program, running in own memory space, independent of all other programs/processes on computer Each process has own copy of: Program instructions/code (“text” segment) Program counter (PC) Registers Method local variables (stack) Object local variables (heap)

8 Multi-process model

9

10

11 Multi-process commun. Process separation enforced by kernel & hardware Communication via kernel mechanisms File I/O Sockets Pipes FIFOs etc. New process created via: fork() (UNIX system call) Process.start() or Runtime.exec() (Java)

12 Inter-process comm. pipe.write() pipe.read()

13 Multi-thread model Threads run within a single process Each thread has its own Program counter (PC) Registers Method-local variables (stack) All threads in a single process share: Program instructions/code (text) Object local variables/“main memory” (heap) No kernel/hardware separation! All separation is done by you!

14 The joy/hazards of threads All threads share same memory Communication is trivial All threads share same memory Data corruption is trivial... Two threads can write to same data at same time, or can read/write same data at same time Inconsistent data state Violates pre/post conditions & expectations There be monsters...

15 The Multi-Thread Model

16 Thread communication

17 Context switch: Design Principle o’ the Day

18 Design principle: Interfaces Make your interfaces sophisticated, so that your algorithms can be simple.

19 Design principle: Interfaces More modularity: break code into small chunks Algorithms are hard to debug Make them as readable/comprehensible as possible Note: sophisticated, not complicated Make your interfaces sophisticated, so that your algorithms can be simple.

20 Intro to AWT Graphics


Download ppt "The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread... Lecture 18, Nov 03."

Similar presentations


Ads by Google