27.07.2013 Views

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 19 Multithreading 763<br />

Starting threads...<br />

WAITING TO CONSUME<br />

Produced 1 into cell 0 write 1 read 0 [1, -1, -1, -1, -1]<br />

Consumed 1 from cell 0 write 1 read 1 [-1, -1, -1, -1, -1]<br />

BUFFER EMPTY<br />

Produced 2 into cell 1 write 2 read 1 [-1, 2, -1, -1, -1]<br />

Produced 3 into cell 2 write 3 read 1 [-1, 2, 3, -1, -1]<br />

Produced 4 into cell 3 write 4 read 1 [-1, 2, 3, 4, -1]<br />

Consumed 2 from cell 1 write 4 read 2 [-1, -1, 3, 4, -1]<br />

Produced 5 into cell 4 write 0 read 2 [-1, -1, 3, 4, 5]<br />

Produced 6 into cell 0 write 1 read 2 [6, -1, 3, 4, 5]<br />

Produced 7 into cell 1 write 2 read 2 [6, 7, 3, 4, 5]<br />

BUFFER FULL<br />

WAITING TO PRODUCE 8<br />

Consumed 3 from cell 2 write 2 read 3 [6, 7, -1, 4, 5]<br />

Produced 8 into cell 2 write 3 read 3 [6, 7, 8, 4, 5]<br />

BUFFER FULL<br />

Consumed 4 from cell 3 write 3 read 4 [6, 7, 8, -1, 5]<br />

Produced 9 into cell 3 write 4 read 4 [6, 7, 8, 9, 5]<br />

BUFFER FULL<br />

WAITING TO PRODUCE 10<br />

Consumed 5 from cell 4 write 4 read 0 [6, 7, 8, 9, -1]<br />

Produced 10 into cell 4 write 0 read 0 [6, 7, 8, 9, 10]<br />

BUFFER FULL<br />

Producer finished producing values<br />

Terminating Producer<br />

Consumed 6 from cell 0 write 0 read 1 [-1, 7, 8, 9, 10]<br />

Consumed 7 from cell 1 write 0 read 2 [-1, -1, 8, 9, 10]<br />

Consumed 8 from cell 2 write 0 read 3 [-1, -1, -1, 9, 10]<br />

Consumed 9 from cell 3 write 0 read 4 [-1, -1, -1, -1, 10]<br />

Consumed 10 from cell 4 write 0 read 0 [-1, -1, -1, -1, -1]<br />

BUFFER EMPTY<br />

Consumer retrieved values totaling: 55<br />

Terminating Consumer<br />

All threads have terminated.<br />

Fig. Fig. 19.9 19.9 Threads modifying a synchronized circular buffer (part 2 of 2).<br />

Class SynchronizedCells (Fig. 19.10) contains six attributes—sharedCells<br />

is a five-element list of integers that represents the circular buffer, writeable indicates<br />

whether a producer can write into the circular buffer, readable indicates whether a consumer<br />

can read from the circular buffer, readLocation indicates the current position<br />

from which the next value can be read by a consumer, writeLocation indicates the<br />

next location in which a value can be placed by a producer and threadCondition is<br />

the condition variable that protects access to the buffer.<br />

1 # Fig. 19.10: SynchronizedCells.py<br />

2 # Synchronized circular buffer of integer values<br />

Fig. Fig. 19.10 19.10 Synchronized circular buffer of integers (part 1 of 3).<br />

© Copyright 1992–2002 by <strong>Deitel</strong> & Associates, Inc. All Rights Reserved. 8/21/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!