Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads Chapter 25. A PC actually does one task at a time but threads are activities that seem to all run at once Time slicing very tiny amounts of time.

Similar presentations


Presentation on theme: "Threads Chapter 25. A PC actually does one task at a time but threads are activities that seem to all run at once Time slicing very tiny amounts of time."— Presentation transcript:

1 Threads Chapter 25

2 A PC actually does one task at a time but threads are activities that seem to all run at once Time slicing very tiny amounts of time are used for each activity (thread) then the next one gets a share of the PC’s CPU time. The threads run concurrently

3 Multitasking Things seem to all run at once Share a task Each thread doing one part of a whole task Independent threads: Program: “Bouncer” with old class “Ball” Uses a thread on example “Bounce2” Two threads running concurrently Dying and Killing: “Bounce3” example stop it with [button] at out command

4 The State of a thread(s) new (newly created) running (executing instructions) runnable (currently another thread is running but ours is ready to go) blocked (may be sleeping) dead (exited normally)

5 Scheduling thread priorities and yield(ing) … yield( ); allows another thread to run (our thread is not a hog of resources) The PC’s schedule actually decides what runs and how CPU time is shared (preemptable vs. nonpreemptiable) Mutual exclusion and Synchronization Only one thread allowed into a method that is “synchronized” may cause a bit of a bottleneck so keep “synchronized methods small

6 Program “TwoCounters” vs. “SharedNumber” Public void synchronized increment ( ) { Thread interaction – want and notify both threads want same resources (CRT) at same time Program “Poets” example of both accessing CRT writing lines mixed… A solution “Mowing” requires a request to use screen See program “ScreenController” – each gets five sconds “Clock” digital screen used, displays hours, minutes wakes after 60 second ticks

7 See program “Minute” and “TickTock” Class “Café” - “You want fries with that”? Buttons for placing order and timing cooking – each part of the total task “food cooked and to customer” Class “Queue” Possible problems:  Interruptions (happen in real world too)  Deadlock See “philosophers” I heard it with 5 golden forks – same problem – they need two forks to eat and have to cooperate (five forks and 5 philosophers) Mean philosophers could starve the 5 th out if they teamed up

8 Deadlock: “two threads waiting for something that the other thread is holding” Common causes: Ignore each other (bounce balls) Share data (..counting threads) Collaborate (café) Compete for resources (poets)


Download ppt "Threads Chapter 25. A PC actually does one task at a time but threads are activities that seem to all run at once Time slicing very tiny amounts of time."

Similar presentations


Ads by Google