From 109f6a2b61dfb41c9d10f93aa614e485a4effd3e Mon Sep 17 00:00:00 2001 From: WahyuKurniaSari Date: Thu, 21 Apr 2022 19:46:43 +0700 Subject: [PATCH] Quiz Wahyu Kurnia Sari 1184001 --- .../Chapter 2 wahyu kurnia sari/Barrier.py | 31 ++++++++ .../Chapter 2 wahyu kurnia sari/Condition.py | 70 +++++++++++++++++ .../Chapter 2 wahyu kurnia sari/Event.py | 41 ++++++++++ .../MyThreadClass.py | 56 ++++++++++++++ .../MyThreadClass_lock.py | 76 ++++++++++++++++++ .../MyThreadClass_lock_2.py | 77 +++++++++++++++++++ .../Chapter 2 wahyu kurnia sari/Rlock.py | 26 +++++++ .../Chapter 2 wahyu kurnia sari/Semaphore.py | 34 ++++++++ .../Thread_definition.py | 17 ++++ .../Thread_determine.py | 29 +++++++ .../Thread_name_and_processes.py | 38 +++++++++ .../Threading_with_queque.py | 68 ++++++++++++++++ 12 files changed, 563 insertions(+) create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Barrier.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Condition.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Event.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock_2.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Rlock.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Semaphore.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_definition.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_determine.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_name_and_processes.py create mode 100644 QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Threading_with_queque.py diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Barrier.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Barrier.py new file mode 100644 index 0000000..4f9c076 --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Barrier.py @@ -0,0 +1,31 @@ + + +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +jumlah_menu = 4 +hasilnya = Barrier(jumlah_menu) +menunya = ['BAKSO', 'BATAGOR', 'SIOMAY', 'MIE AYAM', 'KUE BERAS', 'SEBLAK'] + + + +def menu(): + cobain = menunya.pop() + sleep(randrange(2, 3 )) + print('%s JUGA TERMASUK DALAM MENU \n' % (cobain)) + hasilnya.wait() + + +def main(): + threads = [] + print('Yang ada disini') + for i in range(jumlah_menu): + threads.append(Thread(target=menu)) + threads[-1].start() + for thread in threads: + thread.join() + print('okeh beres') + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Condition.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Condition.py new file mode 100644 index 0000000..0efae5c --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Condition.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() + +# yg ga boleh diganti def __init__ +class Consumer(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def consume(self): + + with condition: + + if len(items) == 0: + logging.info('no items to tepung') + condition.wait() + + items.pop() + logging.info('tepung 1 ') + + condition.notify() + + def run(self): + for i in range(3): + time.sleep(2) + self.consume() + + +class Producer(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def produce(self): + + with condition: + + if len(items) == 3: + logging.info('jenis tepung {}. selesai'.format(len(items))) + condition.wait() + + items.append(1) + logging.info('total {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(3): + time.sleep(0.2) + self.produce() + + +def main(): + producer = Producer(name='tepung') + consumer = Consumer(name='kue') + + producer.start() + consumer.start() + + producer.join() + consumer.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Event.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Event.py new file mode 100644 index 0000000..f34b47f --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Event.py @@ -0,0 +1,41 @@ +import threading +import time +import logging + +logging.basicConfig(level=logging.DEBUG, + format='(%(threadName)-9s) %(message)s', ) + + +def jalan(e): + logging.debug('Kamu lapar tidak') + event_is_set = e.wait() + logging.debug('Hari ini makan apa?: %s', event_is_set) + + +def pergi(e, t): + while not e.isSet(): + logging.debug('Aku lapar sekali nih, makan sate yok') + event_is_set = e.wait(t) + logging.debug('Sate Apa: %s', event_is_set) + if event_is_set: + logging.debug('Sate Maranggi di warung Hj. Maya pasteur') + else: + logging.debug('Hayuk?') + + +if __name__ == "__main__": + e = threading.Event() + t1 = threading.Thread(name='wahyu', + target=jalan, + args=(e,)) + t1.start() + + t2 = threading.Thread(name='Kurnia', + target=pergi, + args=(e, 2)) + t2.start() + + logging.debug('Waiting before calling Event.set()') + time.sleep(3) + e.set() + logging.debug('Allhamdulillah kenyang') \ No newline at end of file diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass.py new file mode 100644 index 0000000..fd2b046 --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass.py @@ -0,0 +1,56 @@ +import time +import os +from random import randint +from threading import Thread + + +class MyThreadClass(Thread): + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + + def run(self): + print("---> " + self.name + \ + " Total Stok Smartphone tahun 2022 di BEC yaitu, " \ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print("---> " + self.name + " Update Terkini Stok habis\n") + + +def main(): + start_time = time.time() + + thread1 = MyThreadClass("Tersedia Merk Samsung ", randint(1, 10)) + thread2 = MyThreadClass("Tersedia Merk Iphone ", randint(1, 10)) + thread3 = MyThreadClass("Tersedia Merk Redmi ", randint(1, 10)) + thread4 = MyThreadClass("Tersedia Merk Oppo ", randint(1, 10)) + thread5 = MyThreadClass("Tersedia Merk Xiomi ", randint(1, 10)) + + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + + # End + print("Tunggu sampai barang tersedia kembali") + + # Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock.py new file mode 100644 index 0000000..7774734 --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock.py @@ -0,0 +1,76 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# 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 + threadLock.acquire() + print("---> " + self.name + \ + " Jumlah stock motor di indonesia berdasarkan merk" \ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print("---> " + self.name + " matic\n") + # Release the Lock + threadLock.release() + + +def main(): + start_time = time.time() + # Thread Creation + thread1 = MyThreadClass("Vario#1 ", randint(1, 10)) + thread2 = MyThreadClass("Beat#2 ", randint(1, 10)) + thread3 = MyThreadClass("PCX#3 ", randint(1, 10)) + thread4 = MyThreadClass("Piaggio#4 ", randint(1, 10)) + thread5 = MyThreadClass("Mio#5 ", randint(1, 10)) + thread6 = MyThreadClass("Nmax#6 ", randint(1, 10)) + thread7 = MyThreadClass("Lexi#7 ", randint(1, 10)) + thread8 = MyThreadClass("Aerox#8 ", randint(1, 10)) + thread9 = MyThreadClass("ADV#9 ", randint(1, 10)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + thread6.start() + thread7.start() + thread8.start() + thread9.start() + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + thread6.join() + thread7.join() + thread8.join() + thread9.join() + + # End + print("Motor Matic Siap di Distribusikan") + + # Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock_2.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock_2.py new file mode 100644 index 0000000..90801dc --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock_2.py @@ -0,0 +1,77 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# 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 + threadLock.acquire() + print("---> " + self.name + \ + " Pendistribusian motor matic sesuai di regional Jawa Barat " \ + + str(os.getpid()) + "\n") + threadLock.release() + time.sleep(self.duration) + print("---> " + self.name + " Pendistribusian di regional III\n") + # Release the Lock + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = MyThreadClass("Bandung#1 ", randint(1, 8)) + thread2 = MyThreadClass("Subang#2 ", randint(1, 8)) + thread3 = MyThreadClass("Bekasi#3 ", randint(1, 8)) + thread4 = MyThreadClass("Bogor#4 ", randint(1, 8)) + thread5 = MyThreadClass("Tangerang#5 ", randint(1, 8)) + thread6 = MyThreadClass("Tasikmalaya#6 ", randint(1, 8)) + thread7 = MyThreadClass("Sumedang#7 ", randint(1, 8)) + thread8 = MyThreadClass("Cianjur#8 ", randint(1, 8)) + thread9 = MyThreadClass("Ciamis#9 ", randint(1, 8)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + thread4.start() + thread5.start() + thread6.start() + thread7.start() + thread8.start() + thread9.start() + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + thread4.join() + thread5.join() + thread6.join() + thread7.join() + thread8.join() + thread9.join() + + # End + print("End") + + # Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Rlock.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Rlock.py new file mode 100644 index 0000000..2b5e0c1 --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Rlock.py @@ -0,0 +1,26 @@ +# program to illustrate the use of RLocks + +# importing the module +import threading + +# initializing the shared resource +baksobakar = 0 + +# creating an RLock object instead +# of Lock object +lock = threading.RLock() + +# the below thread is trying to access +# the shared resource +lock.acquire() +baksobakar = baksobakar + 20 + +# the below thread is trying to access +# the shared resource +lock.acquire() +baksobakar = baksobakar + 32 +lock.release() +lock.release() + +# displaying the value of shared resource +print(baksobakar) diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Semaphore.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Semaphore.py new file mode 100644 index 0000000..31ed351 --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Semaphore.py @@ -0,0 +1,34 @@ +# importing the modules +from threading import * +import time + +# creating thread instance where count = 3 +obj = Semaphore(1) + + +# creating instance +def display(name): + # calling acquire method + obj.acquire() + for i in range(1): + print('Jenis Sate, ', end='') + time.sleep(1) + print(name) + + # calling release method + obj.release() + + +# creating multiple thread +t1 = Thread(target=display, args=('Taichan-1',)) +t2 = Thread(target=display, args=('Maranggi-2',)) +t3 = Thread(target=display, args=('Kambing-3',)) +t4 = Thread(target=display, args=('Ayam-4',)) +t5 = Thread(target=display, args=('Lilit-5',)) + +# calling the threads +t1.start() +t2.start() +t3.start() +t4.start() +t5.start() \ No newline at end of file diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_definition.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_definition.py new file mode 100644 index 0000000..13da964 --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_definition.py @@ -0,0 +1,17 @@ +import threading + + +def my_func(thread_number): + return print('Mau makan bakso berapa porsi? {}'.format(thread_number)) + + +def main(): + threads = [] + for i in range(7): + t = threading.Thread(target=my_func, args=(i,)) + threads.append(t) + t.start() + t.join() + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_determine.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_determine.py new file mode 100644 index 0000000..abab75e --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_determine.py @@ -0,0 +1,29 @@ +import threading +import time + +def function_A(): + print (threading.currentThread().getName()+str('--> tersedia di rumah makan Hj. Maya Pasteur \n')) + time.sleep(2) + print (threading.currentThread().getName()+str( '--> tersedia di rumah makan Hj. Maya Pasteur \n')) + return + +def function_B(): + print (threading.currentThread().getName()+str('--> tersedia di rumah makan saung sunda \n')) + time.sleep(2) + print (threading.currentThread().getName()+str( '--> tersedia di rumah makan saung sunda\n')) + return + + + +if __name__ == "__main__": + + t1 = threading.Thread(name='Makanan Manis', target=function_A) + t2 = threading.Thread(name='Makanan Pedas', target=function_B) + + + t1.start() + t2.start() + + + t1.join() + t2.join() diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_name_and_processes.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_name_and_processes.py new file mode 100644 index 0000000..28a89ce --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Thread_name_and_processes.py @@ -0,0 +1,38 @@ +from threading import Thread +import time +import os + + +class MyThreadClass(Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + def run(self): + print("Bakso Bakar {}".format(self.name)) # , " is {} \n".format(os.getpid())) + + +def main(): + from random import randint + # Thread Creation + thread1 = MyThreadClass("Pedas ") + thread2 = MyThreadClass("Asin") + + # Thread Running + thread1.start() + thread2.start() + + # Thread joining + thread1.join() + thread2.join() + + # End + print("Siap Dihidangakan") + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Threading_with_queque.py b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Threading_with_queque.py new file mode 100644 index 0000000..9c0e13d --- /dev/null +++ b/QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Threading_with_queque.py @@ -0,0 +1,68 @@ +def queue(): + s = [] + return s + + +def enqueue(s, i): + s.insert(0, i) + return s + + +def dequeue(s): + return s.pop() + + +def rear(s): + return (s[0]) + + +def front(s): + return (s[len(s) - 1]) + + +def size(s): + return len(s) + + +def isEmpty(s): + return s == [] + + +def No2(): + s = queue() + k = '' + while True: + banyak = int(input('Dari angka 1-10, seberapa siap kamu? = ')) + for j in range(banyak): + orang = input('undang teman anda %i yang akan masuk dalam games ini = ' % (j + 1)) + enqueue(s, orang) + s.reverse() + print('maka teman anda masuk dalam permainan %s' % s) + s.reverse() + o = input('mencari id patner musuh anda = ') + ditemukan = 't' + itung = 0 + while ditemukan == 't': + if o == front(s): + print('Congrats! id musuh ditemukan') + ditemukan = 'ok' + elif o != front(s): + masukan = dequeue(s) + enqueue(s, masukan) + ditemukan = 't' + s.reverse() + print('Looping %i = %s' % ((itung + 1), s)) + s.reverse() + itung += 1 + if itung > len(s): + print('Maaf anda kurang beruntung, Coba lagi ya') + ditemukan = 'ya' + print('Total peserta yang di perlukan', str(itung - 5)) + k = input('apakah anda ingin melanjutkan permainannya? (y/t) ? ') + if k != 'y': + break + else: + print('Semangat, jangan menyerah' ) + + +No2()