From 62a53c8b03fa8c0ce39d5f5ec4800a23ff9a481f Mon Sep 17 00:00:00 2001 From: Eriskiannisa Date: Wed, 20 Apr 2022 16:15:11 +0700 Subject: [PATCH] 1194013-Eriskiannisa-Quiz-Chapter02 --- .../Eriskiannisa Febrianty/Barrier.py | 29 +++++++++ .../Eriskiannisa Febrianty/Condition.py | 40 ++++++++++++ QUIS SISTER 1/Eriskiannisa Febrianty/Event.py | 34 +++++++++++ .../Eriskiannisa Febrianty/MyThreadClass.py | 30 +++++++++ .../MyThreadClass_lock.py | 39 ++++++++++++ .../MyThreadClass_lock_2.py | 44 +++++++++++++ QUIS SISTER 1/Eriskiannisa Febrianty/Rlock.py | 61 +++++++++++++++++++ .../Eriskiannisa Febrianty/Semaphore.py | 43 +++++++++++++ .../Thread_definition.py | 13 ++++ .../Thread_determine.py | 29 +++++++++ .../Thread_name_and_processes.py | 32 ++++++++++ .../Threading_with_queue.py | 53 ++++++++++++++++ 12 files changed, 447 insertions(+) create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Barrier.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Condition.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Event.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock_2.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Rlock.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Semaphore.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Thread_definition.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Thread_determine.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Thread_name_and_processes.py create mode 100644 QUIS SISTER 1/Eriskiannisa Febrianty/Threading_with_queue.py diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/Barrier.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Barrier.py new file mode 100644 index 0000000..3785ec9 --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Barrier.py @@ -0,0 +1,29 @@ +# Tugas Studi Kasus +import threading +import time + +def start_checkout(): + print("starting checkout the payment...") + time.sleep(2) + +def payment(b): + start_checkout() + b.wait() + print("Successfully payment") + +def confirmation(b): + print("waiting for payment getting ready...") + b.wait() + print("sending email confirmation...") + +if __name__=='__main__': + + b = threading.Barrier(2, timeout=5) + payment = threading.Thread(target=payment, args=(b,)) + payment.start() + confirmation = threading.Thread(target=confirmation, args=(b,)) + confirmation.start() + + payment.join() + confirmation.join() + print("Done") \ No newline at end of file diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/Condition.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Condition.py new file mode 100644 index 0000000..d331f1e --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Condition.py @@ -0,0 +1,40 @@ +# Tugas Studi Kasus +import logging +import threading +import random + +logging.basicConfig(level=logging.DEBUG, + format='(%(threadName)-9s) %(message)s',) + +condition = threading.Condition() + +class Mengantre(): + + def pengunjung(self): + logging.debug('pengunjung mulai mengantre...') + + with condition: + logging.debug('pengunjung menunggu giliran...') + condition.wait() + logging.debug('sukses mendapat giliran') + + def loket(self): + logging.debug('loket mulai dibuka...') + + with condition: + logging.debug('loket memanggil nomor antrean...') + na=0 + na=random.randint(1,13) + print('cek nomor antrean...') + print('memanggil nomor antrean ke {}'.format(na)) + condition.notify() + +antre = Mengantre() +pengunjung = threading.Thread(name='Pengunjung', target=antre.pengunjung) +loket = threading.Thread(name='loket', target=antre.loket) + +pengunjung.start() +loket.start() + +pengunjung.join() +loket.join() \ No newline at end of file diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/Event.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Event.py new file mode 100644 index 0000000..eb54dec --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Event.py @@ -0,0 +1,34 @@ +# Tugas Studi Kasus +import threading +import time +import logging + +logging.basicConfig(level=logging.DEBUG, + format='(%(threadName)-9s) %(message)s',) + +def wait_for_event(e): + logging.debug('Menunggu film dimulai...') + event_is_set = e.wait() + logging.debug('Film dimulai: %s', event_is_set) + +def wait_for_event_timeout(e, t): + while not e.isSet(): + logging.debug('Menunggu Film Selesai') + event_is_set = e.wait(t) + logging.debug('Film dimulai: %s', event_is_set) + if event_is_set: + logging.debug('Memproses Film') + else: + logging.debug('Iklan') + +if __name__ == '__main__': + e = threading.Event() + t1 = threading.Thread(name='blocking', target=wait_for_event,args=(e,)) + t2 = threading.Thread(name='non-blocking', target=wait_for_event_timeout, args=(e, 2)) + t1.start() + t2.start() + + logging.debug('Menunggu sebelum film dimulai') + time.sleep(3) + e.set() + logging.debug('Film dimulai') \ No newline at end of file diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass.py b/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass.py new file mode 100644 index 0000000..5fc27b0 --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass.py @@ -0,0 +1,30 @@ +# Tugas Studi Kasus +import time +import os +from random import randint +from threading import Thread + +class MyThread(Thread): + def __init__(self, i, duration): + Thread.__init__(self) + self.x = i + self.duration = duration + + def run(self): + print("Value stored is: ", self.x) + time.sleep(self.duration) + print("Exiting thread with value: ", self.x) + + +thread1 = MyThread(1, randint(1,10)) +thread2 = MyThread(2, randint(1,10)) +thread3 = MyThread(3, randint(1,10)) + +thread1.start() +thread2.start() +thread3.start() + +thread1.join() +thread2.join() +thread3.join() + diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock.py b/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock.py new file mode 100644 index 0000000..782add1 --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock.py @@ -0,0 +1,39 @@ +# Tugas Studi Kasus + +import threading +import time +from threading import Thread +from random import randint + +# Lock Definition +threadLock = threading.Lock() + +class MyThread(Thread): + def __init__(self, i, duration): + Thread.__init__(self) + self.x = i + self.duration = duration + + def run(self): + # Acquire the Lock + threadLock.acquire() + + print("Value stored is: ", self.x) + time.sleep(self.duration) + print("Exiting thread with value: ", self.x) + + # Release the Lock + threadLock.release() + + +thread1 = MyThread(1, randint(1,10)) +thread2 = MyThread(2, randint(1,10)) +thread3 = MyThread(3, randint(1,10)) + +thread1.start() +thread2.start() +thread3.start() + +thread1.join() +thread2.join() +thread3.join() diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock_2.py b/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock_2.py new file mode 100644 index 0000000..f37045c --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock_2.py @@ -0,0 +1,44 @@ +# Tugas Studi Kasus + +import threading +import time +import os +from threading import Thread +from random import randint + +# Lock Definition +threadLock = threading.Lock() + +class MyThread(Thread): + # overriding constructor + def __init__(self, i, duration): + # calling parent class constructor + Thread.__init__(self) + self.x = i + self.duration = duration + + # define your own run method + def run(self): + # Acquire the Lock + threadLock.acquire() + + print("Value stored is: ", self.x) + + # Release the Lock + threadLock.release() + time.sleep(self.duration) + print("Exiting thread with value: ", self.x) + + +thread1 = MyThread(1, randint(1,10)) +thread2 = MyThread(2, randint(1,10)) +thread3 = MyThread(3, randint(1,10)) + +thread1.start() +thread2.start() +thread3.start() + +thread1.join() +thread2.join() +thread3.join() + diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/Rlock.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Rlock.py new file mode 100644 index 0000000..24444d0 --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Rlock.py @@ -0,0 +1,61 @@ +# Tugas Studi Kasus +import threading +import time +import random + + +class Box: + 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(box, items): + print("{} Barang yang ada di box \n".format(items)) + while items: + box.add() + time.sleep(1) + items -= 1 + print("Tambah barang ke box sebanyak -->{} \n".format(items)) + + + +def remover(box, items): + print("{} Barang di box yang terjual \n".format(items)) + while items: + box.remove() + time.sleep(1) + items -= 1 + print("Barang yang terjual sebanyak -->{} \n".format(items)) + + +def main(): + items = 10 + box = Box() + + t1 = threading.Thread(target=adder, \ + args=(box, random.randint(10,20))) + 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/Eriskiannisa Febrianty/Semaphore.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Semaphore.py new file mode 100644 index 0000000..364003b --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Semaphore.py @@ -0,0 +1,43 @@ +# Tugas Studi Kasus + +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 pemenangundian(): + logging.info('Menunggu Kereta datang...') + semaphore.acquire() + logging.info('Kereta {} Sudah Berangkat'.format(item)) + + +def nomorundian(): + global item + time.sleep(3) + item = random.randint(1, 10) + logging.info('Kereta {} Sudah Datang '.format(item)) + semaphore.release() + + +def main(): + for i in range(1): + t1 = threading.Thread(target=pemenangundian) + t2 = threading.Thread(target=nomorundian) + + t1.start() + t2.start() + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_definition.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_definition.py new file mode 100644 index 0000000..8d16585 --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_definition.py @@ -0,0 +1,13 @@ +# Tugas Studi Kasus +import time +from threading import Thread + +def Proses(i): + print("Thread %i memproses dalam 5 detik." % i) + time.sleep(5) + print("Thread %i selesai di proses." % i) + +for i in range(10): + th = Thread(target=Proses, args=(i, )) + th.start() + th.join() \ No newline at end of file diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_determine.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_determine.py new file mode 100644 index 0000000..3e681b2 --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_determine.py @@ -0,0 +1,29 @@ +#Tugas Studi Kasus +import threading +import time + +def thread_pertama(i): + time.sleep(5) + print('Nilai dari '+ str(threading.current_thread().getName())+" adalah: ", i) + +def thread_kedua(i): + time.sleep(6) + print('Nilai dari '+ str(threading.current_thread().getName())+" adalah: ", i) + +def thread_ketiga(i): + time.sleep(7) + print('Nilai dari '+ str(threading.current_thread().getName())+" adalah: ", i) + + +thread1 = threading.Thread(target=thread_pertama, args=(10,)) # nama default +thread2 = threading.Thread(name='Thread Kedua', target=thread_kedua, args=(20,)) +thread3 = threading.Thread(name='Thread Ketiga', target=thread_ketiga, args=(30,)) + +# Running the threads +thread1.start() +thread2.start() +thread3.start() + +thread1.join() +thread2.join() +thread3.join() diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_name_and_processes.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_name_and_processes.py new file mode 100644 index 0000000..af0ec0b --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Thread_name_and_processes.py @@ -0,0 +1,32 @@ +# Tugas studi Kasus +from threading import Thread + +class MyThreadClass (Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + def run(self): + print("Kereta Sedang Menuju ke {}".format(self.name)) + +def main(): + from random import randint + # Thread Creation + thread1 = MyThreadClass("Stasiun 1 ") + thread2 = MyThreadClass("Stasiun 2 ") + + # Thread Running + thread1.start() + thread2.start() + + + # Thread joining + thread1.join() + thread2.join() + + # End + print("Kereta Sampai di tujuan") + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Eriskiannisa Febrianty/Threading_with_queue.py b/QUIS SISTER 1/Eriskiannisa Febrianty/Threading_with_queue.py new file mode 100644 index 0000000..72fd97b --- /dev/null +++ b/QUIS SISTER 1/Eriskiannisa Febrianty/Threading_with_queue.py @@ -0,0 +1,53 @@ +# Tugas Studi Kasus +from threading import Thread +from queue import Queue +import time +import random + + +class Pembeli(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('Pembeli: item N°%d ditambahkan ke keranjang oleh %s\n'\ + % (item, self.name)) + time.sleep(1) + + +class Penjual(Thread): + + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + + def run(self): + while True: + item = self.queue.get() + print('Penjual : Notifikasi Item %d Pesanan dari %s'\ + % (item, self.name)) + self.queue.task_done() + +if __name__ == '__main__': + queue = Queue() + + + t1 = Pembeli(queue) + t2 = Penjual(queue) + t3 = Penjual(queue) + t4 = Penjual(queue) + + t1.start() + t2.start() + t3.start() + t4.start() + + t1.join() + t2.join() + t3.join() + t4.join()