diff --git a/Chapter03/praktikum/studikasus_communicating_with_pipe.py b/Chapter03/praktikum/studikasus_communicating_with_pipe.py new file mode 100644 index 0000000..4d33698 --- /dev/null +++ b/Chapter03/praktikum/studikasus_communicating_with_pipe.py @@ -0,0 +1,26 @@ +import multiprocessing + +def konduktor(conn, events): + for event in events: + conn.send(event) + print(f"Dirijen mengirim tanda: {event}") + +def pemain_alat_musik(conn): + while True: + event = conn.recv() + if event == "eod": + print("Pemain alat musik menerima: permainan alat musik berhenti") + return + print(f"Pemain alat musik menerima: {event}") + + + +if __name__ == "__main__": + events = ["Violin mulai bermain", "Cello mulai bermain", "Double bass mulai bermain", "Harpa mulai bermain", "end"] + conn1, conn2 = multiprocessing.Pipe() + process_1 = multiprocessing.Process(target=konduktor, args=(conn1, events)) + process_2 = multiprocessing.Process(target=pemain_alat_musik, args=(conn2,)) + process_1.start() + process_2.start() + process_1.join() + process_2.join() \ No newline at end of file diff --git a/Chapter03/praktikum/studikasus_communicating_with_queue.py b/Chapter03/praktikum/studikasus_communicating_with_queue.py new file mode 100644 index 0000000..b9c80dc --- /dev/null +++ b/Chapter03/praktikum/studikasus_communicating_with_queue.py @@ -0,0 +1,47 @@ +import multiprocessing +import random +import time + + +class pemilik(multiprocessing.Process): + def __init__(self, queue): + multiprocessing.Process.__init__(self) + self.queue = queue + + def run(self) : + for i in range(4): + kostrakan = random.randrange(0, 20) + self.queue.put(kostrakan) + print ("Proses Pemilik : Pemilik dengan ID %d Menambahkan Bangunan ke dalam list kos dan kontrakan"\ + % (kostrakan)) + time.sleep(1) + print ("Jumlah kos dan kontrakan adalah %s"\ + % self.queue.qsize()) + +class pencari(multiprocessing.Process): + def __init__(self, queue): + multiprocessing.Process.__init__(self) + self.queue = queue + + def run(self): + while True: + if (self.queue.empty()): + print("Tidak Ada Bangunan yang tersedia saat ini :(") + break + else : + time.sleep(2) + item = self.queue.get() + print ('Proses Pencari : Bangunan %d Tersedia \ + dimiliki oleh %s \n'\ + % (item, self.name)) + time.sleep(1) + + +if __name__ == '__main__': + queue = multiprocessing.Queue() + process_Pemilik = pemilik(queue) + process_pencari = pencari(queue) + process_Pemilik.start() + process_pencari.start() + process_Pemilik.join() + process_pencari.join() \ No newline at end of file diff --git a/Chapter03/praktikum/studikasus_killing_processes.py b/Chapter03/praktikum/studikasus_killing_processes.py new file mode 100644 index 0000000..67fe655 --- /dev/null +++ b/Chapter03/praktikum/studikasus_killing_processes.py @@ -0,0 +1,20 @@ +import multiprocessing +import time + +def pungsi(): + print ('Start') + time.sleep(0.1) + print ('Finish') + +if __name__ == '__main__': + p = multiprocessing.Process(target=pungsi) + print ('BEFORE:', p, p.is_alive()) + + p.start() + print ('DURING:', p, p.is_alive()) + + p.terminate() + print ('TERMINATED:', p, p.is_alive()) + + p.join() + print ('JOINED:', p, p.is_alive()) \ No newline at end of file diff --git a/Chapter03/praktikum/studikasus_naming_processes.py b/Chapter03/praktikum/studikasus_naming_processes.py new file mode 100644 index 0000000..9fce602 --- /dev/null +++ b/Chapter03/praktikum/studikasus_naming_processes.py @@ -0,0 +1,38 @@ +import multiprocessing +import time +from time import ctime + +def myFunc(): + name = multiprocessing.current_process().name + print ("Starting process name = %s \n pada tanggal %s\n" % (name, ctime())) + time.sleep(3) + print ("Exiting process name = %s \n pada tanggal %s\n" % (name, ctime())) + +if __name__ == '__main__': + process_with_name1 = multiprocessing.Process\ + (name='Penamaan produk',\ + target=myFunc) + process_with_name2 = multiprocessing.Process\ + (name='Penomoran produk',\ + target=myFunc) + + #process_with_name.daemon = True + + process_with_default_name1 = multiprocessing.Process\ + (target=myFunc) + process_with_default_name2 = multiprocessing.Process\ + (target=myFunc) + + process_with_name1.start() + process_with_name2.start() + process_with_default_name1.start() + process_with_default_name2.start() + + process_with_name1.join() + process_with_name2.join() + process_with_default_name1.join() + process_with_default_name2.join() + + print('Selesai proses') + + diff --git a/Chapter03/praktikum/studikasus_process_in_subclass.py b/Chapter03/praktikum/studikasus_process_in_subclass.py new file mode 100644 index 0000000..d65fb20 --- /dev/null +++ b/Chapter03/praktikum/studikasus_process_in_subclass.py @@ -0,0 +1,15 @@ +import multiprocessing +from time import ctime + +class MyProcess(multiprocessing.Process): + + def run(self): + print ('%s produk selesai di inputkan pada tanggal %s' %(self.name, ctime())) + return + +if __name__ == '__main__': + for i in range(10): + process = MyProcess() + process.start() + process.join() + diff --git a/Chapter03/praktikum/studikasus_process_pool.py b/Chapter03/praktikum/studikasus_process_pool.py new file mode 100644 index 0000000..fdab2db --- /dev/null +++ b/Chapter03/praktikum/studikasus_process_pool.py @@ -0,0 +1,36 @@ +#Using a Process Pool – Chapter 3: Process Based Parallelism +import multiprocessing +from time import ctime, sleep +from random import random + +def function_square(data): + result = data*data + return result + +produk1 = 'Ram' +produk2 = 'Matherboard' +produk3 = 'Komputer' +if __name__ == '__main__': + inputs1 = list(range(9,10)) + inputs2 = list(range(19,20)) + inputs3 = list(range(29,30)) + + pool = multiprocessing.Pool(processes=4) + pool_outputs1 = pool.map(function_square, inputs1) + pool_outputs2 = pool.map(function_square, inputs2) + pool_outputs3 = pool.map(function_square, inputs3) + + mhs1 = produk1 + mhs2 = produk2 + mhs3 = produk3 + + value = random() * 10 + sleep(value) + + print (f'Pembagian Nomor Produk: \n Nama Produk: %s \n Nomor Produk: %s \n Waktu render: {value} \n Tanggal: %s \n' % (mhs1, pool_outputs1, ctime())) + print (f'Pembagian Nomor Produk: \n Nama Produk: %s \n Nomor Produk: %s \n Waktu render: {value} \n Tanggal: %s \n' % (mhs2, pool_outputs2, ctime())) + print (f'Pembagian Nomor Produk: \n Nama Produk: %s \n Nomor Produk: %s \n Waktu render: {value} \n Tanggal: %s \n' % (mhs3, pool_outputs3, ctime())) + + pool.close() + pool.join() + print('Selesai') \ No newline at end of file diff --git a/Chapter03/praktikum/studikasus_processes_barrier.py b/Chapter03/praktikum/studikasus_processes_barrier.py new file mode 100644 index 0000000..6fe45fd --- /dev/null +++ b/Chapter03/praktikum/studikasus_processes_barrier.py @@ -0,0 +1,36 @@ +import multiprocessing +from multiprocessing import Barrier, Lock, Process +from time import time +from datetime import datetime + + +def test_with_barrier(synchronizer, serializer): + name = multiprocessing.current_process().name + synchronizer.wait() + now = time() + with serializer: + print("Produk %s ----> %s" \ + %(name,datetime.fromtimestamp(now))) + +def test_without_barrier(): + name = multiprocessing.current_process().name + now = time() + print("Produk %s ----> %s" \ + %(name ,datetime.fromtimestamp(now))) + +if __name__ == '__main__': + synchronizer = Barrier(2) + serializer = Lock() + Process(name='Meja - Berhasil terjual'\ + ,target=test_with_barrier,\ + args=(synchronizer,serializer)).start() + Process(name='Kursi - Berhasil terjual'\ + ,target=test_with_barrier,\ + args=(synchronizer,serializer)).start() + Process(name='Laptop - Berhasil terjual'\ + ,target=test_without_barrier).start() + Process(name='Ram - Berhasil terjual'\ + ,target=test_without_barrier).start() + + + diff --git a/Chapter03/praktikum/studikasus_run_background_processes.py b/Chapter03/praktikum/studikasus_run_background_processes.py new file mode 100644 index 0000000..b468760 --- /dev/null +++ b/Chapter03/praktikum/studikasus_run_background_processes.py @@ -0,0 +1,34 @@ +import multiprocessing +import time +from time import ctime + +def foo(): + name = multiprocessing.current_process().name + print ("Produk %s pada tanggal %s \n" %(name, ctime())) + if name == 'retur': + for i in range(0,5): + print('SKU produk %d \n' %i) + time.sleep(1) + else: + for i in range(5,10): + print('SKU produk %d \n' %i) + time.sleep(1) + print ("Exiting %s \n" %name) + + +if __name__ == '__main__': + background_process = multiprocessing.Process\ + (name='retur berhasil',\ + target=foo) + background_process.daemon = True + + NO_background_process = multiprocessing.Process\ + (name='retur berhasil',\ + target=foo) + + NO_background_process.daemon = False + + background_process.start() + NO_background_process.start() + + diff --git a/Chapter03/praktikum/studikasus_run_background_processes_no_daemons.py b/Chapter03/praktikum/studikasus_run_background_processes_no_daemons.py new file mode 100644 index 0000000..021ace0 --- /dev/null +++ b/Chapter03/praktikum/studikasus_run_background_processes_no_daemons.py @@ -0,0 +1,34 @@ +import multiprocessing +import time +from time import ctime + +def foo(): + name = multiprocessing.current_process().name + print ("Produk %s pada tanggal %s \n" %(name, ctime())) + if name == 'retur': + for i in range(10,15): + print('SKU produk %d \n' %i) + time.sleep(1) + else: + for i in range(15,20): + print('SKU produk %d \n' %i) + time.sleep(1) + print ("Exiting %s \n" %name) + + +if __name__ == '__main__': + background_process = multiprocessing.Process\ + (name='retur berhasil',\ + target=foo) + background_process.daemon = False + + NO_background_process = multiprocessing.Process\ + (name='retur behasil',\ + target=foo) + + NO_background_process.daemon = False + + background_process.start() + NO_background_process.start() + + diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusBarrier.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusBarrier.py new file mode 100644 index 0000000..b75d8e1 --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusBarrier.py @@ -0,0 +1,22 @@ +# example of using a barrier +from time import ctime, sleep +from random import random +from threading import Thread, Barrier + +name = ['Farhan', 'Kevin', 'Putri', 'Helmi', 'Salsabila'] +def main(barrier, number): + value = random() * 10 + mhs = name.pop() + sleep(value) + print(f'No urut {number} %s sudah sampai di kampus, pada hari %s dengan waktu: {value} \n' % (mhs, ctime())) + # menunggu semuanya slesai + barrier.wait() + +# buat barrier +case = Barrier(5 + 1) +for i in range(5): + gasin = Thread(target=main, args=(case, i)) + gasin.start() +print('Mulai..') +case.wait() +print('Selesai') \ No newline at end of file diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusCondition.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusCondition.py new file mode 100644 index 0000000..01cb0fc --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusCondition.py @@ -0,0 +1,70 @@ +import logging +import threading +import time + +LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s' +logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) + +items = [] +condition = threading.Condition() + + +class Mahasiswa(threading.Thread): + def _init_(self, *args, **kwargs): + super()._init_(*args, **kwargs) + + def urut(self): + + with condition: + + if len(items) == 5: + logging.info('Absensi ke') + condition.wait() + + items.pop() + logging.info('Absensi ke {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(7): + time.sleep(2) + self.urut() + + +class Dosen(threading.Thread): + def _init_(self, *args, **kwargs): + super()._init_(*args, **kwargs) + + def Jumlah(self): + + with condition: + + if len(items) == 13: + logging.info('Jumlah {}. Stopped'.format(len(items))) + condition.wait() + + items.append(1) + logging.info('Jumlah waktu {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(20): + time.sleep(0.5) + self.Jumlah() + + +def main(): + urut = Mahasiswa(name='Mahasiswa') + Quiz = Dosen(name='Dosen') + + urut.start() + Quiz.start() + + urut.join() + Quiz.join() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusEvent.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusEvent.py new file mode 100644 index 0000000..d8c3014 --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusEvent.py @@ -0,0 +1,47 @@ +import logging +import threading +import time +import random + +LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s' +logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) + +items = [] +event = threading.Event() + + +class Peserta(threading.Thread): + def _init_(self, *args, **kwargs): + super()._init_(*args, **kwargs) + + def run(self): + while True: + time.sleep(2) + event.wait() + item = items.pop() + logging.info('Peserta dengan nomor urut: {} Menuju ke ruangan {}'\ + .format(item, self.name)) + +class Juri(threading.Thread): + def _init_(self, *args, **kwargs): + super()._init_(*args, **kwargs) + + def run(self): + for i in range(5): + time.sleep(2) + item = random.randint(0, 100) + items.append(item) + logging.info('Juri telah approv peserta nomor urut {}'\ + .format(item, self.name)) + event.set() + event.clear() + +if __name__ == "__main__": + t1 = Peserta() + t2 = Juri() + + t1.start() + t2.start() + + t1.join() + t2.join() \ No newline at end of file diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass.py new file mode 100644 index 0000000..b6daa94 --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass.py @@ -0,0 +1,59 @@ +import time +import os +from random import randint +from threading import Thread + +# Threading melakukan konkurensi untuk mengeksekusi sebuah operasi +# Threading memisahkan sebgian kode dan mengeksekusi di proses yg dibuatnya sendiri +# tentukan limit prosesnya agar tidak menghabiskan ruang cpu dan ram +# tidak menggunakan fungsi lock + +class MyThreadClass (Thread): + # self merpresentasikan setiap objek + # getpid untuk mendapatkan ID proses dari proses saat ini + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + def run(self): + print ("---> " + self.name + \ + " running, ID "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print("Waktu ", self.duration, "seconds") + print ("---> " + self.name + " selesai\n") + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = MyThreadClass("Proses Thread 1 ", randint(1,10)) + thread2 = MyThreadClass("Proses Thread 2 ", randint(1,10)) + thread3 = MyThreadClass("Proses Thread 3 ", randint(1,10)) + thread4 = MyThreadClass("Proses Thread 4 ", randint(1,10)) + thread5 = MyThreadClass("Proses Thread 5 ", randint(1,10)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + + # Thread joining (proses pengembalian elemen dari proses yg sudah selesai) + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + + # End + print("End") + + #Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass_lock.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass_lock.py new file mode 100644 index 0000000..2caf872 --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass_lock.py @@ -0,0 +1,69 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# Threading melakukan konkurensi untuk mengeksekusi sebuah operasi +# Threading memisahkan sebgian kode dan mengeksekusi di proses yg dibuatnya sendiri +# tentukan limit prosesnya agar tidak menghabiskan ruang cpu dan ram +# Menggunakan fungsi lock yang akan membungkus beberapa perintah dalam method untuk di jalnkankan dulu baru apabila kelar akan menjalankan printah selanjutnya setelah syntax lock release + +# Lock Definition +threadLock = threading.Lock() + +class MyThreadClass (Thread): + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + def run(self): + #Acquire the Lock + #getpid untuk mendapatkan ID proses dari proses saat ini + threadLock.acquire() + print ("---> " + self.name + \ + "running, ID "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print("Waktu ", self.duration, "seconds") + print ("---> " + self.name + " selesai\n") + #Release the Lock + threadLock.release() + + +def main(): + start_time = time.time() + # Thread Creation + thread1 = MyThreadClass("Proses Thread 1 ", randint(1,10)) + thread2 = MyThreadClass("Proses Thread 2 ", randint(1,10)) + thread3 = MyThreadClass("Proses Thread 3 ", randint(1,10)) + thread4 = MyThreadClass("Proses Thread 4 ", randint(1,10)) + thread5 = MyThreadClass("Proses Thread 5 ", randint(1,10)) + + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + + # Thread joining (proses pengembalian elemen dari proses yg sudah selesai) + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + + # End + print("End") + + #Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass_lock_2.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass_lock_2.py new file mode 100644 index 0000000..95f0044 --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusMyThreadClass_lock_2.py @@ -0,0 +1,66 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# Threading melakukan konkurensi untuk mengeksekusi sebuah operasi +# Threading memisahkan sebgian kode dan mengeksekusi di proses yg dibuatnya sendiri +# tentukan limit prosesnya agar tidak menghabiskan ruang cpu dan ram +# Menggunakan fungsi lock yang akan membungkus beberapa perintah dalam method untuk di jalnkankan dulu baru apabila kelar akan menjalankan printah selanjutnya setelah syntax lock release + +# Lock Definition +threadLock = threading.Lock() + +class MyThreadClass (Thread): + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + def run(self): + #Acquire the Lock + #getpid untuk mendapatkan ID proses dari proses saat ini + threadLock.acquire() + print ("---> " + self.name + \ + "running, ID "\ + + str(os.getpid()) + "\n") + #Release the Lock + threadLock.release() + time.sleep(self.duration) + print(self.duration) + print ("---> " + self.name + " selesai\n") + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = MyThreadClass("Proses Thread 1 ", randint(1,10)) + thread2 = MyThreadClass("Proses Thread 2 ", randint(1,10)) + thread3 = MyThreadClass("Proses Thread 3 ", randint(1,10)) + thread4 = MyThreadClass("Proses Thread 4 ", randint(1,10)) + thread5 = MyThreadClass("Proses Thread 5 ", randint(1,10)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + + # Thread joining (proses pengembalian elemen dari proses yg sudah selesai) + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + + # End + print("End") + + #Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusRlock.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusRlock.py new file mode 100644 index 0000000..30cbe58 --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusRlock.py @@ -0,0 +1,60 @@ +import threading +import time +import random + + +class Barang: + def __init__(self): + self.lock = threading.RLock() + self.total_items = 0 + + def execute(self, value): + with self.lock: + self.total_items += value + + def add(self): + with self.lock: + self.execute(1) + + def remove(self): + with self.lock: + self.execute(-1) + +def adder(Barang, items): + print("Banyaknya produk {} yang ditambahkan \n".format(items)) + while items: + Barang.add() + time.sleep(1) + items -= 1 + print("stok produk saat ini --> {} buah \n".format(items)) + + + +def remover(Barang, items): + print("banyaknya produk {} yang di jual \n".format(items)) + while items: + Barang.remove() + time.sleep(1) + items -= 1 + print("produk yang dijual --> {} buah \n".format(items)) + + +def main(): + items = 12 + box = Barang() + + t1 = threading.Thread(target=adder, \ + args=(box, random.randint(1,10))) + t2 = threading.Thread(target=remover, \ + args=(box, random.randint(1,10))) + + t1.start() + t2.start() + + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusSemaphore.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusSemaphore.py new file mode 100644 index 0000000..224a2c9 --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusSemaphore.py @@ -0,0 +1,41 @@ +import logging +import threading +import time +import random + +LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s' +logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) + + +semaphore = threading.Semaphore(0) +item = 0 + + +def ujian(): + logging.info('Ujian Nasional Sedang Berlangsung') + semaphore.acquire() + logging.info('Nomor {} harap register terlebih dahulu'.format(item)) + + +def nomorujian(): + global item + time.sleep(3) + item = random.randint(1, 10) + logging.info('Noomor {} telah selesai mengerjakan Ujian Nasional'.format(item)) + semaphore.release() + + +def main(): + for i in range(1): + t1 = threading.Thread(target=ujian) + t2 = threading.Thread(target=nomorujian) + + t1.start() + t2.start() + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusThreadDefinition.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusThreadDefinition.py new file mode 100644 index 0000000..4280ca7 --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusThreadDefinition.py @@ -0,0 +1,17 @@ +import threading +from time import ctime + + +def __mi__(thread_number): + return print('Ini adalah Thread Definition N°{} \n Waktu selesai {}'.format(thread_number, ctime())) + +def main(): + threads = [] + for i in range(10): + t = threading.Thread(target=__mi__, args=(i,)) + threads.append(t) + t.start() + t.join() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusThreading_with_queue.py b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusThreading_with_queue.py new file mode 100644 index 0000000..0f49f5e --- /dev/null +++ b/QUIS SISTER 1/1194018_HelmiSalsabila/StudiKasusThreading_with_queue.py @@ -0,0 +1,53 @@ +""""Thread synchronisation with queue""" + +from threading import Thread +from queue import Queue +import time +import random + + +class Panitia(Thread): + + def init(self, queue): + Thread.init(self) + self.queue = queue + + def run(self): + for i in range(5): + item = random.randint(0, 256) + self.queue.put(item) + print('Penerima kurban dengan nomor %d ditambahkan pada antrian oleh %s\n'\ + % (item, self.name)) + time.sleep(1) + + +class Peserta(Thread): + + def init(self, queue): + Thread.init(self) + self.queue = queue + + def run(self): + while True: + item = self.queue.get() + print('Penerima nomor %d Penerima Kurban dimunculkan oleh %s'\ + % (item, self.name)) + self.queue.task_done() + +if __name__ == '_main_': + queue = Queue() + + t1 = Panitia(queue) + t2 = Peserta(queue) + t3 = Peserta(queue) + t4 = Peserta(queue) + + t1.start() + t2.start() + t3.start() + t4.start() + + t1.join() + t2.join() + t3.join() + t4.join() \ No newline at end of file