From 99b7e389a3a421dea37be394da9e598d91ae0696 Mon Sep 17 00:00:00 2001 From: FaisalAbdulkodir Date: Wed, 20 Apr 2022 14:51:58 +0700 Subject: [PATCH] Quiz Faisal --- QUIS SISTER 1/Faisal Abdullah/Barrier.py | 29 ++++++++ QUIS SISTER 1/Faisal Abdullah/Condition.py | 73 +++++++++++++++++++ QUIS SISTER 1/Faisal Abdullah/Event.py | 54 ++++++++++++++ .../Faisal Abdullah/MyTheradClass_lock2.py | 30 ++++++++ .../Faisal Abdullah/MyThreadClass.py | 43 +++++++++++ .../Faisal Abdullah/MyThreadClass_lock.py | 28 +++++++ QUIS SISTER 1/Faisal Abdullah/Queue.py | 55 ++++++++++++++ QUIS SISTER 1/Faisal Abdullah/Rlock.py | 59 +++++++++++++++ QUIS SISTER 1/Faisal Abdullah/Semaphore.py | 26 +++++++ .../Faisal Abdullah/Thread_definition.py | 18 +++++ .../Faisal Abdullah/Thread_determine.py | 31 ++++++++ .../Thread_name_and_processes.py | 40 ++++++++++ 12 files changed, 486 insertions(+) create mode 100644 QUIS SISTER 1/Faisal Abdullah/Barrier.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/Condition.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/Event.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/MyTheradClass_lock2.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/MyThreadClass.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/MyThreadClass_lock.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/Queue.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/Rlock.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/Semaphore.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/Thread_definition.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/Thread_determine.py create mode 100644 QUIS SISTER 1/Faisal Abdullah/Thread_name_and_processes.py diff --git a/QUIS SISTER 1/Faisal Abdullah/Barrier.py b/QUIS SISTER 1/Faisal Abdullah/Barrier.py new file mode 100644 index 0000000..065cf3c --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Barrier.py @@ -0,0 +1,29 @@ +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +num_mhs = 3 +b = Barrier(num_mhs) +mhs = ['Ahmad', 'Alwi', 'Abdul'] + +# barrier = barrier digunakan untuk memblock semua thread,kemudian melepas semua untuk execute secara bersamaan + +def runner(): + name = mhs.pop() + sleep(randrange(2, 10)) + print('%s Berangkat kuliah pada: %s ' % (name, ctime())) + b.wait() + print('%s Pulang kuliah pada: %s ' % (name, ctime())) + +def main(): + threads = [] + print('Gas kuliah!!!!') + for i in range(num_mhs): + threads.append(Thread(target=runner)) + threads[-1].start() + for thread in threads: + thread.join() + print('Udah pada balik!') + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Faisal Abdullah/Condition.py b/QUIS SISTER 1/Faisal Abdullah/Condition.py new file mode 100644 index 0000000..0c8a3bf --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Condition.py @@ -0,0 +1,73 @@ +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 Kosumen(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def pakai(self): + + with condition: + + if len(items) == 0: + logging.info('Tidak ada barang yang dapat dibeli') + condition.wait() + + items.pop() + logging.info('Kamu membeli 1 barang') + + condition.notify() + + + def run(self): + for i in range(20): + time.sleep(2) + self.pakai() + + +class Pabrik(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def jual(self): + + with condition: + + if len(items) == 10: + logging.info('Di etalase terdapat {} item. Tidak cukup tempat. Produksi berhenti sementara'.format(len(items))) + condition.wait() + + items.append(1) + logging.info('Terdapat {} barang saat ini'.format(len(items))) + + condition.notify() + + + def run(self): + for i in range(20): + time.sleep(0.5) + self.jual() + + +def main(): + kosumen = Pabrik(name='Pabrik') + pabrik = Kosumen(name='Kosumen') + + pabrik.start() + kosumen.start() + + pabrik.join() + kosumen.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Faisal Abdullah/Event.py b/QUIS SISTER 1/Faisal Abdullah/Event.py new file mode 100644 index 0000000..542efca --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Event.py @@ -0,0 +1,54 @@ +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 Japati(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.name = "Japati" + + + def run(self): + while True: + time.sleep(2) + event.wait() + item = items.pop() + logging.info('Menerima surat dari No {} dikirim dengan menggunakan {} ke tujuan'.format(item, self.name)) + + +class Pengirim(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.name = "Pengirim Surat" + + + def run(self): + for i in range(5): + time.sleep(2) + item = random.randint(0, 100) + items.append(item) + logging.info('Pengirim surat di alamat {} di buat oleh {}'.format(item, self.name)) + event.set() + event.clear() + + +if __name__ == "__main__": + t1 = Pengirim() + t2 = Japati() + + + t1.start() + t2.start() + + + t1.join() + t2.join() \ No newline at end of file diff --git a/QUIS SISTER 1/Faisal Abdullah/MyTheradClass_lock2.py b/QUIS SISTER 1/Faisal Abdullah/MyTheradClass_lock2.py new file mode 100644 index 0000000..cf7d81e --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/MyTheradClass_lock2.py @@ -0,0 +1,30 @@ +import threading +import time + +konci2 = threading.Lock() +#lock = digunakan untuk block sebuah thread untuk melakukan execute sebelum thread lain beres +#lock2 = posisi dari method release() yang diubah akan mempengaruhi output + +def orang_pertama(konci2): + konci2.acquire() + print('orang - 1 sedang menggunakan kamar mandi') + time.sleep(1) + print('orang - 1 sudah selesai') + # konci2.release() + +def orang_kedua(konci2): + # konci2.acquire() + print('orang - 2 sedang menggunakan kamar mandi') + konci2.release() + time.sleep(1) + print('orang - 2 sudah selesai') + + +t1 = threading.Thread(target=orang_pertama, args=(konci2, )) +t2 = threading.Thread(target=orang_kedua, args=(konci2, )) + +t1.start() +t2.start() + +t1.join() +t2.join() diff --git a/QUIS SISTER 1/Faisal Abdullah/MyThreadClass.py b/QUIS SISTER 1/Faisal Abdullah/MyThreadClass.py new file mode 100644 index 0000000..1335c6b --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/MyThreadClass.py @@ -0,0 +1,43 @@ +from threading import Thread +import threading +import time +from random import randint + + + + + +class ThreadClass(Thread): + + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + + def run(self): + print("{} execute ketika: {}".format(threading.current_thread().getName(), time.ctime())) + time.sleep(self.duration) + print("{} lanjut ketika: {}".format(threading.current_thread().getName(), time.ctime())) + +def main(): + start_time = time.time() + + t1 = ThreadClass('T-1', randint(1,11)) + t2 = ThreadClass('T-2', randint(1,11)) + t3 = ThreadClass('T-3', randint(1,11)) + + + t1.start() + t2.start() + t3.start() + + t1.join() + t2.join() + t3.join() + + print('beres') + + print("diexcecute selama : %s detik " % (time.time() - start_time)) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Faisal Abdullah/MyThreadClass_lock.py b/QUIS SISTER 1/Faisal Abdullah/MyThreadClass_lock.py new file mode 100644 index 0000000..6c6509e --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/MyThreadClass_lock.py @@ -0,0 +1,28 @@ +import threading +import time + +konci = threading.Lock() +#lock = digunakan untuk block sebuah thread untuk melakukan execute sebelum thread lain beres + +def orang_pertama(konci): + konci.acquire() + print('orang - 1 sedang menggunakan kamar mandi') + time.sleep(1) + print('orang - 1 sudah selesai') + konci.release() + +def orang_kedua(konci): + konci.acquire() + print('orang - 2 sedang menggunakan kamar mandi') + time.sleep(1) + print('orang - 2 sudah selesai') + konci.release() + +t1 = threading.Thread(target=orang_pertama, args=(konci, )) +t2 = threading.Thread(target=orang_kedua, args=(konci, )) + +t1.start() +t2.start() + +t1.join() +t2.join() diff --git a/QUIS SISTER 1/Faisal Abdullah/Queue.py b/QUIS SISTER 1/Faisal Abdullah/Queue.py new file mode 100644 index 0000000..e3194f4 --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Queue.py @@ -0,0 +1,55 @@ +""""Thread synchronisation with queue""" + +from threading import Thread +from queue import Queue +import time +import random + + +class Japati(Thread): + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + self.name = 'Japati' + + + + def run(self): + for i in range(5): + item = random.randint(0, 256) + self.queue.put(item) + print('Menerima surat dari No {} dikirim dengan menggunakan {} ke tujuan'.format(item, self.name)) + time.sleep(1) + + +class Pengirim(Thread): + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + self.name = 'Pengirim' + + + def run(self): + while True: + item = self.queue.get() + print('Pengirim surat di alamat {} di buat oleh {}'.format(item, self.name)) + self.queue.task_done() + + +if __name__ == '__main__': + queue = Queue() + + t1 = Japati(queue) + t2 = Pengirim(queue) + t3 = Pengirim(queue) + t4 = Pengirim(queue) + + t1.start() + t2.start() + t3.start() + t4.start() + + t1.join() + t2.join() + t3.join() + t4.join() \ No newline at end of file diff --git a/QUIS SISTER 1/Faisal Abdullah/Rlock.py b/QUIS SISTER 1/Faisal Abdullah/Rlock.py new file mode 100644 index 0000000..ddee57e --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Rlock.py @@ -0,0 +1,59 @@ +import threading +import time +import random + +class Kelas: + 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 aula(box, items): + print("mahasiswa sebanyak {} orang harus keluar kelas dan berkumpul di aula \n".format(items)) + while items: + box.add() + time.sleep(1) + items -= 1 + print("Satu mahasiswa sampai di aula -->{} mahasiswa yang harus ke aula \n".format(items)) + + + +def kelas(box, items): + print("Jumlah mahasiswa {} yang tidak berangkat ke aula \n".format(items)) + while items: + box.remove() + time.sleep(1) + items -= 1 + print("mahasiswa izin tidak kelapangan -->{} mahasiswa yang tidak ke aula \n".format(items)) + + +def main(): + items = 10 + box = Kelas() + + t1 = threading.Thread(target=aula, \ + args=(box, random.randint(10,20))) + t2 = threading.Thread(target=kelas, \ + args=(box, random.randint(1,10))) + + t1.start() + t2.start() + + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Faisal Abdullah/Semaphore.py b/QUIS SISTER 1/Faisal Abdullah/Semaphore.py new file mode 100644 index 0000000..a3308d2 --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Semaphore.py @@ -0,0 +1,26 @@ +import threading +import time + +#sempahore = digunakan untuk menentukan berapa jumlah thread yang akan berjalan secara bersamaan +# disini terdapat 20, jadi akan ada 5 output yang waktu nya +semaphore = threading.Semaphore(5) + +def test(): + semaphore.acquire() + time.sleep(1) + print("{} : {}".format(threading.current_thread().getName(), time.ctime())) + semaphore.release() + + +if __name__ == "__main__": + + thread_list= list() + for i in range(20): + t=threading.Thread(target=test,args=()) + thread_list.append(t) + t.start() + + for t in thread_list: + t.join() + + print("Selesai") diff --git a/QUIS SISTER 1/Faisal Abdullah/Thread_definition.py b/QUIS SISTER 1/Faisal Abdullah/Thread_definition.py new file mode 100644 index 0000000..321abb8 --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Thread_definition.py @@ -0,0 +1,18 @@ +import threading + + +def function(thread_number): + return print('function ini di jalankan oleh thread ke {}'.format(thread_number)) + + +def main(): + threads = [] + for i in range(1,11): + t = threading.Thread(target=function, args=(i,)) + threads.append(t) + t.start() + t.join() + +if __name__ == "__main__": + main() + diff --git a/QUIS SISTER 1/Faisal Abdullah/Thread_determine.py b/QUIS SISTER 1/Faisal Abdullah/Thread_determine.py new file mode 100644 index 0000000..14b59e9 --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Thread_determine.py @@ -0,0 +1,31 @@ +import threading +import time + +def sarapan(): + time.sleep(3) + print('makan dulu') + +def ngopi(): + time.sleep(4) + print('ngopi dulu') + +def belajar(): + time.sleep(5) + print('belajar dulu') + + +t1 = threading.Thread(target=sarapan, args=()) +t2 = threading.Thread(target=ngopi, args=()) +t3 = threading.Thread(target=belajar, args=()) + +t1.start() +t2.start() +t3.start() +t1.join() +t2.join() +t3.join() + +# sarapan() +# ngopi() +# belajar() + diff --git a/QUIS SISTER 1/Faisal Abdullah/Thread_name_and_processes.py b/QUIS SISTER 1/Faisal Abdullah/Thread_name_and_processes.py new file mode 100644 index 0000000..7704f6e --- /dev/null +++ b/QUIS SISTER 1/Faisal Abdullah/Thread_name_and_processes.py @@ -0,0 +1,40 @@ +from threading import Thread +import time +import os + +class AbdulThread (Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + def run(self): + print("Proses yang sedang berjalan dengan ID {}".format(self.name)) #, " is {} \n".format(os.getpid())) + +def main(): + from random import randint + # pembuatan thread + t1 = AbdulThread(" 1 ") + t2 = AbdulThread(" 2 ") + t3 = AbdulThread(" 3 ") + + # memulai proses threading + t1.start() + t2.start() + t3.start() + + + # Thread joining + t1.join() + t2.join() + t3.join() + + # Selesai + print("Selesai") + + +if __name__ == "__main__": + main() + + + +