From 116020152b28ee6febb2a5d6eef170029b4ae4de Mon Sep 17 00:00:00 2001 From: burhanudinzuhri Date: Wed, 20 Apr 2022 15:13:24 +0700 Subject: [PATCH] Quiz Sister 1194008 Burhanudin Zuhri --- .../1194008_Burhanudin Zuhri/Barrier.py | 26 +++++++ .../1194008_Burhanudin Zuhri/Condition.py | 70 +++++++++++++++++++ .../1194008_Burhanudin Zuhri/Event.py | 47 +++++++++++++ .../1194008_Burhanudin Zuhri/MyThreadClass.py | 63 +++++++++++++++++ .../MyThreadClass_lock.py | 56 +++++++++++++++ .../MyThreadClass_lock_2.py | 26 +++++++ .../1194008_Burhanudin Zuhri/Rlock.py | 60 ++++++++++++++++ .../1194008_Burhanudin Zuhri/Semaphore.py | 39 +++++++++++ .../Thread_definition.py | 12 ++++ .../Thread_determine.py | 26 +++++++ .../Thread_name_and_processes.py | 36 ++++++++++ .../Threading_with_queue.py | 52 ++++++++++++++ 12 files changed, 513 insertions(+) create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Barrier.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Condition.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Event.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock_2.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Rlock.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Semaphore.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_definition.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_determine.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_name_and_processes.py create mode 100644 QUIS SISTER 1/1194008_Burhanudin Zuhri/Threading_with_queue.py diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Barrier.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Barrier.py new file mode 100644 index 0000000..1ee4c6c --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Barrier.py @@ -0,0 +1,26 @@ +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +jml_turis = 3 +destinasi = Barrier(jml_turis) +nama_turis = ['Andi', 'Budi', 'Caca'] + +def wisata(): + name = nama_turis.pop() + sleep(randrange(2, 5)) + print('%s Berangkat sekolah pada hari: %s ' % (name, ctime())) + destinasi.wait() + +def main(): + threads = [] + print('Perjalanan Dimulai') + for i in range(jml_turis): + threads.append(Thread(target=wisata)) + threads[-1].start() + for thread in threads: + thread.join() + print('Semuanya sampai di sekolah tepat waktu') + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Condition.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Condition.py new file mode 100644 index 0000000..6374678 --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/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() + + +class Pembeli(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def consume(self): + + with condition: + + if len(items) == 0: + logging.info('Tidak ada sepeda yang dapat dibeli') + condition.wait() + + items.pop() + logging.info('Anda membeli 1 buah sepeda') + + condition.notify() + + def run(self): + for i in range(20): + time.sleep(2) + self.consume() + + +class Penjual(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def produce(self): + + with condition: + + if len(items) == 10: + logging.info('Pada toko terdapat {} buah sepeda. Produksi berhenti untuk sementara'.format(len(items))) + condition.wait() + + items.append(1) + logging.info('Terdapat {} buah sepeda saat ini'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(20): + time.sleep(0.5) + self.produce() + + +def main(): + penjual = Penjual(name='Penjual') + pembeli = Pembeli(name='Pembeli') + + penjual.start() + pembeli.start() + + penjual.join() + pembeli.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Event.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Event.py new file mode 100644 index 0000000..8f79a9b --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Event.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 Supplier(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('Supplier memberitahu bahwa: item {} telah diambil oleh {}'\ + .format(item, self.name)) + +class Vendor(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('Vendor memberitahu bahwa: item {} telah ditambahkan oleh {}'\ + .format(item, self.name)) + event.set() + event.clear() + +if __name__ == "__main__": + t1 = Vendor() + t2 = Supplier() + + t1.start() + t2.start() + + t1.join() + t2.join() diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass.py new file mode 100644 index 0000000..a8da48a --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass.py @@ -0,0 +1,63 @@ +import time +import os +from random import randint +from threading import Thread + +class Mahasiswa (Thread): + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + def run(self): + print ("---> " + self.name + \ + " Sedang mengerjakan ujian dengan nomor ujian "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " pengerjaan ujian telah berakhir\n") + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = Mahasiswa("Mahasiswa#1 ", randint(1,10)) + thread2 = Mahasiswa("Mahasiswa#2 ", randint(1,10)) + thread3 = Mahasiswa("Mahasiswa#3 ", randint(1,10)) + thread4 = Mahasiswa("Mahasiswa#4 ", randint(1,10)) + thread5 = Mahasiswa("Mahasiswa#5 ", randint(1,10)) + thread6 = Mahasiswa("Mahasiswa#6 ", randint(1,10)) + thread7 = Mahasiswa("Mahasiswa#7 ", randint(1,10)) + thread8 = Mahasiswa("Mahasiswa#8 ", randint(1,10)) + thread9 = Mahasiswa("Mahasiswa#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("Selesai") + + #Execution Time + print("--- %s detik ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock.py new file mode 100644 index 0000000..e512cf7 --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock.py @@ -0,0 +1,56 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# Lock Definition +threadLock = threading.Lock() + +class Absen(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 + \ + " dengan absen "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " selesai absen\n") + #Release the Lock + threadLock.release() + + +def main(): + start_time = time.time() + # Thread Creation + thread1 = Absen("Siswa", randint(1,10)) + thread2 = Absen("Guru", randint(1,10)) + + + + # Thread Running + thread1.start() + thread2.start() + + + + # Thread joining + thread1.join() + thread2.join() + + + + # End + print("Jagalah ketertiban") + + #Execution Time + print("Waktu yang dibutuhkan untuk mengabsen siswa dan guru yaitu %s detik" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + \ No newline at end of file diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock_2.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock_2.py new file mode 100644 index 0000000..274ae9c --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock_2.py @@ -0,0 +1,26 @@ +import threading +import time + +kendaraan = threading.Lock() + +def Mobil(kendaraan): + kendaraan.acquire() + print('Mobil sedang melewati terowongan') + time.sleep(1) + print('Mobil sudah melewati terowongan') + +def Motor(kendaraan): + print('Motor sedang melewati terowongan') + kendaraan.release() + time.sleep(1) + print('Motor sudah melewati terowongan') + + +t1 = threading.Thread(target=Mobil, args=(kendaraan, )) +t2 = threading.Thread(target=Motor, args=(kendaraan, )) + +t1.start() +t2.start() + +t1.join() +t2.join() \ No newline at end of file diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Rlock.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Rlock.py new file mode 100644 index 0000000..96ebea2 --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Rlock.py @@ -0,0 +1,60 @@ +import threading +import time +import random + + +class Ricecooker: + 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(kotak_nasi, items): + print("Banyaknya nasi {} kg yang ditambahkan \n".format(items)) + while items: + kotak_nasi.add() + time.sleep(1) + items -= 1 + print("Sisa nasi saat ini -->{} kg \n".format(items)) + + + +def remover(kotak_nasi, items): + print("Banyaknya nasi {} kg yang dimakan \n".format(items)) + while items: + kotak_nasi.remove() + time.sleep(1) + items -= 1 + print("Nasi yang dimakan -->{} kg \n".format(items)) + + +def main(): + items = 10 + kotak_nasi = Ricecooker() + + t1 = threading.Thread(target=adder, \ + args=(kotak_nasi, random.randint(10,20))) + t2 = threading.Thread(target=remover, \ + args=(kotak_nasi, random.randint(1,10))) + + t1.start() + t2.start() + + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Semaphore.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Semaphore.py new file mode 100644 index 0000000..d053b7f --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Semaphore.py @@ -0,0 +1,39 @@ +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 Penulis(): + logging.info('Permintaan dibuat') + semaphore.acquire() + logging.info('Penulis menulis surat nomor {}'.format(item)) + + +def Pengirim(): + global item + time.sleep(2) + item = random.randint(0, 1000) + logging.info('Pengirim merespon permintaan nomor {}'.format(item)) + semaphore.release() + + +def main(): + for i in range(10): + t1 = threading.Thread(target=Penulis) + t2 = threading.Thread(target=Pengirim) + + t1.start() + t2.start() + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_definition.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_definition.py new file mode 100644 index 0000000..446f8ef --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_definition.py @@ -0,0 +1,12 @@ +import time +from threading import Thread + +def campur(i): + print ("Proses mencampurkan pupuk %d" % i) + time.sleep(2) + print (" Mencampurkan pupuk selesai dalam %d" % i) + +for i in range(10): + t = Thread(target=campur, args=(i,)) + t.start() + \ No newline at end of file diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_determine.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_determine.py new file mode 100644 index 0000000..4a00b37 --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_determine.py @@ -0,0 +1,26 @@ +import threading +import time + +def Bersepeda(): + time.sleep(3) + print('Bersepeda di pagi hari') + +def Memancing(): + time.sleep(4) + print('Memancing di sore hari') + +def Belajar(): + time.sleep(5) + print('Belajar di malam hari') + + +t1 = threading.Thread(target=Bersepeda, args=()) +t2 = threading.Thread(target=Memancing, args=()) +t3 = threading.Thread(target=Belajar, args=()) + +t1.start() +t2.start() +t3.start() +t1.join() +t2.join() +t3.join() diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_name_and_processes.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_name_and_processes.py new file mode 100644 index 0000000..1bc236f --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Thread_name_and_processes.py @@ -0,0 +1,36 @@ +from threading import Thread +import time +import os + +class Komputer(Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + def run(self): + print("Komputer yang masih bekerja yaitu komputer {}".format(self.name)) + +def main(): + from random import randint + # pembuatan thread + proses1 = Komputer("bernomor 1 ") + proses2 = Komputer("bernomor 2 ") + proses3 = Komputer("bernomor 3 ") + + # memulai proses threading + proses1.start() + proses2.start() + proses3.start() + + + # Thread joining + proses1.join() + proses2.join() + proses3.join() + + # End + print("Selesai") + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194008_Burhanudin Zuhri/Threading_with_queue.py b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Threading_with_queue.py new file mode 100644 index 0000000..00d8023 --- /dev/null +++ b/QUIS SISTER 1/1194008_Burhanudin Zuhri/Threading_with_queue.py @@ -0,0 +1,52 @@ +from threading import Thread +from queue import Queue +import time +import random + + +class Pelayan(Thread): + + def _init_(self, queue): + Thread._init_(self) + self.queue = queue + + def run(self): + for i in range(5): + item = random.randint(0, 20) + self.queue.put(item) + print('Pelayan akan memberitahu bahwa : antrian nomor %d akan mendapat giliran selanjutnya %s\n'\ + % (item, self.name)) + time.sleep(1) + + +class Pelanggan(Thread): + + def _init_(self, queue): + Thread._init_(self) + self.queue = queue + + def run(self): + while True: + item = self.queue.get() + print('%d Muncul antrian dari %s'\ + % (item, self.name)) + self.queue.task_done() + +if __name__ == '_main_': + queue = Queue() + + t1 = Pelayan(queue) + t2 = Pelanggan(queue) + t3 = Pelanggan(queue) + t4 = Pelanggan(queue) + + t1.start() + t2.start() + t3.start() + t4.start() + + t1.join() + t2.join() + t3.join() + t4.join() + \ No newline at end of file