forked from BukuInformatika/python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultithreading.tex
More file actions
520 lines (506 loc) · 21.9 KB
/
Copy pathMultithreading.tex
File metadata and controls
520 lines (506 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
Menjalankan beberapa\textit{ thread} mirip dengan menjalankan beberapa program yang berbeda secara bersamaan, namun dengan manfaat berikut : \par
\begin{itemize}
\item Beberapa \textit{thread} dalam proses berbagi ruang data yang sama dengan benang induk dan karena dapat saling berbagi informasi atau berkomunikasi satu sama lain dengan lebih muda daripada jika prosesnya terpisah \par
\item \textit{thread} terkadang disebut proses ringan dan tidak membutuhkan banyak memori atas, mereka lebih murah daripada proses.\end{itemize}
\par
Sebuah \textit{thread} memiliki permulaan, urutan eksekusi dan sebuah kesimpulan. Ini memiliki pointer perintah yang melacak dari mana dalam konteksnya saat ini berjalan. \par
\begin{itemize}
\item Hal ini dapat dilakukan sebelum pre-\textit{empted} (\textit{inturrepted})\end{itemize}
\par
\noindent
\begin{itemize}
\item Untuk sementara dapat ditunda sementara \textit{thread} lainnya yang sedang berjalan ini disebut unggul. \end{itemize}
\par
\noindent
\textbf{1.}\textbf{1 }\textbf{Memulai }\textbf{\textit{Thread}}\textbf{ Baru} \par
\noindent
\hspace*{0.5in} Untuk melakukan \textit{thread} lain, perlu memanggil metode berikut yang tersedia dimodul \textit{thread} : \par
\noindent
\begin{center}{\fontsize{9pt}{9pt}\selectfont Thread.start $ \_ $new $ \_ $thread (function, args [, kwargs] )}\end{center} \par
Pemanggilan metode ini memungkinkan cara cepat dan tepat untuk membuat \textit{thread} baru di linux dan window. \par
Pemanggilan metode segera kembali dan anak \textit{thread} dimulai dan fungsi pemanggilan dengan daftar \textit{args} telah berlalu. Saat fungsi kembali ujung \textit{thread} akan berakhir. \par
Disini, \textit{args }adalah tupel argumen. Gunakan tupel kosong untuk memanggil fungsi tanpa melewati argumen. \textit{Kwargs} adalah kamus opsional argumen kata kunci. \par
\noindent
\par
\noindent
%%%%%%%%%%%% Start New Page here %%%%%%%%%%%%%%
\newpage
\vspace{12pt}
\vspace{12pt}
\noindent
Contoh : \par
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $!/usr/bin/python} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont Import thread} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Import time} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Define a function for the thread} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Def print $ \_ $time (threadNamw, delay):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont \hspace*{0.5in} Count = 0} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont \hspace*{0.5in} While count <5:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont \hspace*{0.5in} Time.sleep(delay)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont \hspace*{0.5in} Count +=1} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont \hspace*{0.5in} Print $ " $ $ \% $s : $ \% $s $ " $ $ \% $ (threadName, time.ctime(time.time()))} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Create two thread as follows} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont try:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont thread.start $ \_ $new $ \_ $thread(print $ \_ $time, ( $ " $Thread-1 $ " $, 2, ))} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont thread.start $ \_ $new $ \_ $thread(print $ \_ $time, ( $ " $Thread-2 $ " $, 4,))} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont except:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~ print $ " $Error: unable to start thread $ " $} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont while 1:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont pass} \par
{\fontsize{10pt}{10pt}\selectfont
%%%%%%%%%%%% Start New Page here %%%%%%%%%%%%%%
\newpage
Bila kode diatas dieksekusi, maka menghasilkan hasil sebagai berikut :} \par
\vspace{10pt}
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Jan 22 15:42:17 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Jan 22 15:42:19 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Jan 22 15:42:19 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Jan 22 15:42:21 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Jan 22 15:42:23 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Jan 22 15:42:23 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-1~: Thu Jan 22 15:42:23 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Jan 22 15:42:25 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Jan 22 15:42:27 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Jan 22 15:42:31 2009}\end{center} \par
\noindent
\begin{center}{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Jan 22 15:42:35 2009}\end{center} \par
\vspace{12pt}
Meskipun sangat efektif untuk benang tingkat rendah, namun modul \textit{thread} sangat terbatas dibandingkan dengan modul yang baru. \par
\vspace{12pt}
\textbf{1.2 }\textbf{Modul}\textbf{ Threading } \par
Modul threading yang lebih baru disertakan dengan Python 2.4 memberikan jauh lebih kuat, dukungan tingkat tinggi untuk \textit{thread}\textit{ }dari modul\textit{ }\textit{thread}\textit{ }dibahas pada bagian sebelumnya. \par
The \textit{thread}\textit{ing }modul mengekpos semua metode dari \textit{thread}\textit{ }dan menyediakan beberapa metode tambahan : \par
\begin{itemize}
\item \textbf{t}\textbf{hreading.activeCount() } \par
Mengembalikan jumlah objek \textit{thread} yang aktif \par
\item \textbf{t}\textbf{hreading.currentThread() } \par
Mengembalikan jumlah objek \textit{thread} dalam kontrol benang pemanggil \par
\item \textbf{t}\textbf{hreading.enumerate() } \par
Mengembalikan daftar semua benda \textit{thread}\textit{ }yang sedang aktif \par
\vspace{12pt}
Selain metode, modul \textit{thread}\textit{ing }memiliki \textit{thread}\textit{ }kelas yang mengimplementasikan \textit{thread}\textit{ing. }Metode yang disediakan oleh \textit{thread}\textit{ }kelas adalah sebagai berikut : \par
\item \textbf{run()} \par
Metode adalah titik masuk untuk \textit{thread} \par
\item \textbf{start()} \par
Metode dimulai\textbf{ }\textit{thread}\textit{ }dengan memanggil metode run \par
\item \textbf{join(}\textbf{[time]}\textbf{)} \par
Menunggu benang untuk mengakhiri \par
\item \textbf{isAlive()} \par
Metode memeriksa apakah\textbf{ }\textit{thread}\textit{ }masih mengeksekusi\textbf{ } \par
\item \textbf{getName()} \par
Metode mengambalikan nama\textbf{ }\textit{thread} \par
\item \textbf{setName()} \par
Metode menetapkan nama\textbf{ }\textit{thread} \par
\vspace{12pt}
\textbf{1.3 }\textbf{Membuat }\textbf{\textit{Thread }}\textbf{Menggunakan }\textbf{\textit{Threading}}\textbf{ Modul} \par
Untuk melaksanakan \textit{thread}\textit{ }baru menggunakan\textit{ threading} harus melakukan hal berikut : \par
\item Mendefinisikan subclass dari \textit{thread} kelas \par
\item Menimpa $ \_ $init $ \_ $ (self [args]) metode untuk menambahkan argumen tambahan \par
\item Menimpa run(self[args]) metode untuk menerapkan apa \textit{thread} harus dilakukan ketika mulai \end{itemize}
\par
\noindent
%%%%%%%%%%%% Start New Page here %%%%%%%%%%%%%%
\newpage
\vspace{12pt}
Setelah membuat baru \textit{thread} subclass, dapat membuah seuah instance dari itu dan kemudian memulai \textit{thread} baru dengan menerapkan \textit{start(),} yang ada gilirinnya panggilan \textit{run()} metode. \par
\vspace{12pt}
Contoh :
\par
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $!/usr/bin/python} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont import threading} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont import time} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont exitFlag = 0} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont class myThread (threading.Thread):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont \hspace*{0.5in} def $ \_ $init $ \_ $(self, threadID, name, counter) :} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~~ threading.Thread. $ \_ $init $ \_ $(self)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont \hspace*{0.5in} self.threadID = threadID} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont \hspace*{0.5in} self.name = name} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont self.counter = counter} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont def run (self) :} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont \hspace*{0.5in} print $ " $Starting $ " $ + self.name} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont \hspace*{0.5in} print $ \_ $time(self.name, self.counter, 5)} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont \hspace*{0.5in} print $ " $Exiting $ " $+ self.name} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont def print $ \_ $time(threadName, delay, counter):} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont while counter:} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont \hspace*{0.5in} if exitFlag:} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont \hspace*{0.5in} \hspace*{0.5in} threadName.exit()} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont \hspace*{0.5in} time.sleep(delay)} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont \hspace*{0.5in} print $ " $ $ \% $s: $ \% $s $ " $ $ \% $ (threadName, time.ctime(time.time()))} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont counter -= 1} \par
\vspace{9pt}
\noindent
{\fontsize{9pt}{9pt}\selectfont $ \# $ Create new threads} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont thread1 = myThread(1, $ " $Thread-1 $ " $, 1)} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont thread2 = myThread(2, $ " $Thread-2 $ " $, 2)} \par
\vspace{9pt}
\noindent
{\fontsize{9pt}{9pt}\selectfont $ \# $ Start new threads} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont thread1.start()} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont thread2.start()} \par
\noindent
{\fontsize{9pt}{9pt}\selectfont print $ " $Exiting Main Thread $ " $} \par
\vspace{12pt}
Ketika kode diatas dijalankan, menghasilkan hasil sebagai berikut:
\par
\noindent
{\fontsize{10pt}{10pt}\selectfont Starting Thread-1} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Starting Thread-2} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Exiting Main Thread} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Mar 21 09:10:03 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Mar 21 09:10:04 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Mar 21 09:10:04 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Mar 21 09:10:05 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Mar 21 09:10:06 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1 : Thu Mar 21 09:10:07 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Exiting Thread-1} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Mar 21 09:10:08 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Mar 21 09:10:10 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2 : Thu Mar 21 09:10:12 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Exiting Thread=2} \par
\vspace{10pt}
\textbf{ 1.4 Sinkronisasi }\textbf{\textit{Thread}} \par
\textit{T}\textit{hread}\textit{ing }modul disediakan dengan Python termasuk sederhana untuk menerapkan mekanisme bahwa memungkinkan untuk menyinkronkan \textit{thread}\textit{ }penguncian. Sebuah kunci baru dibuat dengan memanggil \textit{lock() }metode yang mengembalikan kunci baru. \par
The \textit{acquire}\textit{ }\textit{(blocking)}\textit{ }metode objek kunci baru digunakan untuk memaksa \textit{thread}\textit{ }untuk menjalankan serempak. Opsional \textit{blocking} parameter memungkikan untuk mengontrol apakah\textit{ thread} menunggu untuk mendapatkan kunci. \par
Jika \textit{blocking} diatur ke 0, \textit{thread} segera kembali dengan nilai 0 jika kunci tidak dapat diperoleh dan dengan 1 jika kunci dikuisisi. Jika pemblokiran diatur ke 1, blok dan menunggu kunci yang akan dirilis. \par
The \textit{release()} metode objek kunci baru digunakan untuk melepaskan kunci ketika tidak lagi diperlukan. \par
\noindent
Contoh: \par
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $!/usr/bin/python} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont import threading} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont import time} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont class myThread (threading.Thread):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~ def $ \_ $init $ \_ $(self, threadID, name, counter):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ threading.Thread. $ \_ $init $ \_ $(self)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ self.threadID = threadID} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ self.name = name} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ self.counter = counter} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~ def run(self)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ print $ " $Starting $ " $+ self.name} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ $ \# $ Get lock to synchronize threads} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ ThreadLock.acquire()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ print $ \_ $time(self.name, self.counter, 3)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ $ \# $ Free lock to realease next thread} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ ThreadLock.release()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~ } \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~ Def print $ \_ $time(threadName, delay, counter):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~ while counter:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ time.sleep(delay)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ print $ " $ $ \% $s: $ \% $s $ " $ $ \% $ (threadName, time.ctime(time.time()))} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ counter -= 1} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~ threadLock = threading.Lock()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~ threads = []} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Create new threads} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont thread1 = myThread(1, $ " $Thread-1,1 )} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont thread2 = myThread(2, $ " $Thread-2,2 )} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Start new Threads} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont thread1.start()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont thread2.start()} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Add threads to thread list} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont threads.append(thread1)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont thread2.append(thread2)} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Wait for all threads to complete} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Fort t in threads:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ t.join()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont print $ " $Exiting Main thread $ " $} \par
\vspace{10pt}
\noindent
Bila kode diatas dieksekusi, maka menghasilkan sebagai berikut : \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont Starting Thread-1} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Starting Thread-2} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1: Thu Mar 21 09:11:28 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1: Thu Mar 21 09:11:29 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1: Thu Mar 21 09:11:30 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2: Thu Mar 21 09:11:32 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2: Thu Mar 21 09:11:34 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2: Thu Mar 21 09:11:36 2013} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Exiting Main Thread} \par
\vspace{12pt}
\textbf{1.5 Multithreaded Antrian Prioritas} \par
The queue modul memungkinkan untuk membuat objek antrian baru yang dapat menampung jumlah tertentu item. Ada metode berikut untuk mengontrol antrian : \par
\begin{itemize}
\item \textbf{get()} \par
Menghapus dan mengembalikan item dari antrian
\par
\item \textbf{put()} \par
Menambahkan item ke antrian
\par
\item \textbf{qsize()} \par
Mengembalikan jumlah item yang saat ini dalam antrian
\par
\item \textbf{empty()} \par
Mengembalikan benar jika antrian kosong jika tidak, salah
\par
\item \textbf{full()}\end{itemize}
\par
Mengembalikan benar jika antrian penuh jika tidak, salah
\par
\vspace{12pt}
\vspace{12pt}
\vspace{12pt}
\noindent
Contoh: \par
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $!/usr/bin/python} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont import Queue} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont import threading} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont import time} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont exitFlag = 0} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont class myThread (threading.Thread):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~def $ \_ $init $ \_ $(self, threadID, name, q):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ threading.Thread. $ \_ $init $ \_ $(self)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ self.name = name} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ self.q = q} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont def run(self):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~ print $ " $Starting $ " $+ self.name} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~ process $ \_ $data(self.name, self.q)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~ print $ " $Exiting $ " $+ self.name} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont def process $ \_ $data(threadName, q):} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ while not exitFlag:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ queuLock.acquire()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ if not workQueu.empty():} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~~~ data = q.get()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~~~ queueLock.release()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~~~ print $ " $ $ \% $s processing $ \% $s $ " $ $ \% $ (threadName, data)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~ else:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~~~ queueLock.release()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~~~~~ time.sleep(1)} \par
\vspace{12pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont threadList = [ $ " $Thread-1 $ " $, $ " $Thread-2 $ " $, $ " $Thread-3 $ " $]} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont nameList = [ $ " $One $ " $, $ " $Two $ " $, $ " $Three $ " $, $ " $Four $ " $, $ " $Five $ " $]} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont queueLock = threading.Lock()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont workLock = Queue.Queue(10)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont threads = []} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont threadID = 1} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Create new threads} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont For tName in threadList:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ thread = myThread(threadID, tName, workQueue)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ thread.start()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ thread.append(thread)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ threadID +=1} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Fill the queue} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont queueLock.acquire()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont for word in nameList:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ workQueue.put(word)} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont queueLock.release()} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Wait for queue to empty} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont while not workQueue.empty():} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont pass} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Notify threads it?s time to exit} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont exitFlag = 1} \par
\vspace{10pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont $ \# $ Wait for all threads to complete} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont For t in threads:} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont ~~~ t.join()} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont print $ " $Exiting Main Thread $ " $} \par
\vspace{10pt}
\noindent
Bila kode diatas dieksekusi, maka menghasilkan hasil sebagai berikut: \par
\vspace{12pt}
\noindent
{\fontsize{10pt}{10pt}\selectfont Starting Thread-1} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Starting Thread-2} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Starting Thread-3} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1 processing One} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2 processing Two} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-3 processing Three} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-1 processing Four} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Thread-2 processing Five} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Exiting Thread-3} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Exiting Thread-1} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Exiting Thread-2} \par
\noindent
{\fontsize{10pt}{10pt}\selectfont Exiting Main Thread} \par
\vspace{12pt}