diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Barrier.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Barrier.py new file mode 100644 index 0000000..649a5ac --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Barrier.py @@ -0,0 +1,34 @@ +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +minuman = ['thai tea', 'mixue', 'janjiw', 'boba', 'josu'] +finish_line = Barrier(len(minuman)) +result = [] + +def runner(): + name = minuman.pop() + sleep(randrange(2, 10)) + result.append(name) + print('%s Sudah siap: %s \n' % (name, ctime())) + finish_line.wait() + + +def finisher(result): + for idx, i in enumerate(result): + print(str(idx+1) + " Selamat menikmati "+ i) + + +def main(): + threads = [] + print('Memulai membikin minuman') + for i in range(len(minuman)): + threads.append(Thread(target=runner)) + threads[-1].start() + for thread in threads: + thread.join() + print('Minuman Selesai\n') + print(finisher(result)) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Condition.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Condition.py new file mode 100644 index 0000000..64c58ab --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Condition.py @@ -0,0 +1,67 @@ +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 Outbound(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def pick(self): + with condition: + if len(items) == 0: + logging.info('Baju kosong. Tidak ada Baju yang di ambil') + condition.wait() + + items.pop() + logging.info('Mengambil 1 Baju') + logging.info('Sisa Baju dalam lemari {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(11): + time.sleep(2) + self.pick() + + +class Inbound(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def store(self): + with condition: + if len(items) == 10: + logging.info('Total Baju dalam lemari {}. lemari penuh'.format(len(items))) + condition.wait() + + items.append(1) + logging.info('Memasukan Baju pada lemari {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(10): + time.sleep(0.5) + self.store() + + +def main(): + inbound = Inbound(name='Inbound') + outbound = Outbound(name='Outbound') + + inbound.start() + outbound.start() + + inbound.join() + outbound.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Event.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Event.py new file mode 100644 index 0000000..6e78eab --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Event.py @@ -0,0 +1,55 @@ +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 Service(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.name = "Waiter" + + + def run(self): + while True: + time.sleep(2) + event.wait() + item = items.pop() + logging.info('Mengambil pesanan untuk meja No. {} dan sedang di antarkan oleh {} ke meja'.format(item, self.name)) + + +class Chef(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.name = "Chef Arnold" + + + def run(self): + for i in range(5): + time.sleep(2) + item = random.randint(0, 100) + items.append(item) + logging.info('Chef membuat makanan untuk meja No. {} di buat oleh {}'.format(item, self.name)) + event.set() + event.clear() + + +if __name__ == "__main__": + t1 = Chef() + t2 = Service() + + + t1.start() + t2.start() + + + t1.join() + t2.join() diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass.py new file mode 100644 index 0000000..cb995b8 --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass.py @@ -0,0 +1,74 @@ +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 sum_pid_duration(self, pid): + total = 0 + for i in range(self.duration): + total += (i+1) * pid + + return total + + def run(self): + print("---> " + self.name + " running, MemProses ID = " + str(os.getpid()) + "\n") + time.sleep(self.duration) + print("---> Total perhitungan Proses ID * Durasi = " + str(os.getpid()) + " * " + str(self.duration) + " = " + str(self.sum_pid_duration(os.getpid()))) + print("---> " + self.name + " Selesai\n") + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = MyThreadClass("Nomor Proses ID #1 ", randint(1,10)) + thread2 = MyThreadClass("Nomor Proses ID #2 ", randint(1,10)) + thread3 = MyThreadClass("Nomor Proses ID #3 ", randint(1,10)) + thread4 = MyThreadClass("Nomor Proses ID #4 ", randint(1,10)) + thread5 = MyThreadClass("Nomor Proses ID #5 ", randint(1,10)) + thread6 = MyThreadClass("Nomor Proses ID #6 ", randint(1,10)) + thread7 = MyThreadClass("Nomor Proses ID #7 ", randint(1,10)) + thread8 = MyThreadClass("Nomor Proses ID #8 ", randint(1,10)) + thread9 = MyThreadClass("Nomor Proses ID #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("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/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass_lock.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass_lock.py new file mode 100644 index 0000000..6f84010 --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass_lock.py @@ -0,0 +1,78 @@ +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 sum_pid_duration(self, pid): + total = 0 + for i in range(self.duration): + total += (i+1) * pid + + return total + + def run(self): + #Acquire the Lock + threadLock.acquire() + print("---> " + self.name + " running, MemProses ID = " + str(os.getpid()) + "\n") + time.sleep(self.duration) + print("---> Total perhitungan Proses ID * Durasi = " + str(os.getpid()) + " * " + str(self.duration) + " = " + str(self.sum_pid_duration(os.getpid()))) + print("---> " + self.name + " Selesai\n") + #Release the Lock + threadLock.release() + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = MyThreadClass("Nomor Proses ID #1 ", randint(1,10)) + thread2 = MyThreadClass("Nomor Proses ID #2 ", randint(1,10)) + thread3 = MyThreadClass("Nomor Proses ID #3 ", randint(1,10)) + thread4 = MyThreadClass("Nomor Proses ID #4 ", randint(1,10)) + thread5 = MyThreadClass("Nomor Proses ID #5 ", randint(1,10)) + thread6 = MyThreadClass("Nomor Proses ID #6 ", randint(1,10)) + thread7 = MyThreadClass("Nomor Proses ID #7 ", randint(1,10)) + thread8 = MyThreadClass("Nomor Proses ID #8 ", randint(1,10)) + thread9 = MyThreadClass("Nomor Proses ID #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("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/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass_lock_2.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass_lock_2.py new file mode 100644 index 0000000..85394ed --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/MyThreadClass_lock_2.py @@ -0,0 +1,78 @@ +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 sum_pid_duration(self, pid): + total = 0 + for i in range(self.duration): + total += (i+1) * pid + + return total + + def run(self): + #Acquire the Lock + threadLock.acquire() + print("---> " + self.name + " running, Sedang MemProses ID = " + str(os.getpid()) + "\n") + threadLock.release() + time.sleep(self.duration) + print("---> Total perhitungan Proses ID * Durasi = " + str(os.getpid()) + " * " + str(self.duration) + " = " + str(self.sum_pid_duration(os.getpid()))) + print("---> " + self.name + " Selesai\n") + #Release the Lock + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = MyThreadClass("Nomor Proses ID #1 ", randint(1,10)) + thread2 = MyThreadClass("Nomor Proses ID #2 ", randint(1,10)) + thread3 = MyThreadClass("Nomor Proses ID #3 ", randint(1,10)) + thread4 = MyThreadClass("Nomor Proses ID #4 ", randint(1,10)) + thread5 = MyThreadClass("Nomor Proses ID #5 ", randint(1,10)) + thread6 = MyThreadClass("Nomor Proses ID #6 ", randint(1,10)) + thread7 = MyThreadClass("Nomor Proses ID #7 ", randint(1,10)) + thread8 = MyThreadClass("Nomor Proses ID #8 ", randint(1,10)) + thread9 = MyThreadClass("Nomor Proses ID #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("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/1194017_Haekal Hilmi Zain_D4 TI - 3A/Rlock.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Rlock.py new file mode 100644 index 0000000..3072285 --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Rlock.py @@ -0,0 +1,62 @@ +import threading +import time +import random + + +class Warehouse: + def __init__(self): + self.lock = threading.RLock() + self.total_items = 0 + + + def execute(self, value): + with self.lock: + self.total_items += value + + + def keep(self): + with self.lock: + self.execute(1) + + + def take(self): + with self.lock: + self.execute(-1) + + +def keeping(box, items): + print("Tersedia {} Mie instan yang akan ditambahkan dalam gudang \n".format(items)) + while items: + box.keep() + time.sleep(1) + items -= 1 + print("Menambahkan satu Mie instan dengan ID : {} Mie instan ditambahkan dalam gudang \n".format(items)) + + +def taking(box, items): + print("Terdapat {} Mie instan yang akan di ambil \n".format(items)) + while items: + box.take() + time.sleep(1) + items -= 1 + print("Mengambil satu Mie instan dengan ID : {} Mie instan di ambil dari dalam gudang \n".format(items)) + + +def main(): + box = Warehouse() + + + t1 = threading.Thread(target=keeping, args=(box, random.randint(10,20))) + t2 = threading.Thread(target=taking, args=(box, random.randint(1,10))) + + + t1.start() + t2.start() + + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Semaphore.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Semaphore.py new file mode 100644 index 0000000..5985861 --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Semaphore.py @@ -0,0 +1,48 @@ +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) +table = 0 +exist_table = [1, 4, 5, 7, 9, 10, 12, 15] + + +def reservation(): + logging.info('Melakukan reservasi meja makan') + semaphore.acquire() + + +def resepsionis(): + global table + time.sleep(1) + logging.info('Memproses meja makan, silahkan menunggu') + time.sleep(3) + table = random.randint(1, 25) + if table not in exist_table: + exist_table.append(table) + logging.info('meja No. {} kosong, proses dilanjutkan'.format(table)) + semaphore.release() + else: + logging.info('meja No. {} sudah terisi, pilih meja lainnya'.format(table)) + semaphore.release() + + +def main(): + for i in range(10): + t1 = threading.Thread(target=reservation) + t2 = threading.Thread(target=resepsionis) + + t1.start() + t2.start() + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_definition.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_definition.py new file mode 100644 index 0000000..4a83f8b --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_definition.py @@ -0,0 +1,24 @@ +import threading +from random import randint +import time + +def loop_count(thread_number): + loop_count = randint(1, 20) + print('Proses {}, Memulai proses looping sebanyak {}'.format(thread_number, loop_count)) + for i in range(loop_count): + print(i) + print("selesai") + return time.sleep(5) + + +def main(): + threads = [] + for i in range(10): + t = threading.Thread(target=loop_count, args=(i,)) + threads.append(t) + t.start() + t.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_determine.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_determine.py new file mode 100644 index 0000000..8b19df8 --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_determine.py @@ -0,0 +1,51 @@ +import threading +import time +from random import randint + + +def loop_count(): + loop_count = randint(1, 20) + print('Memulai proses looping sebanyak {}'.format(loop_count)) + for i in range(loop_count): + print(i) + print("selesai") + return time.sleep(5) + + +def looping_A(): + print (threading.currentThread().getName()+str('--> starting \n')) + time.sleep(2) + loop_count() + print (threading.currentThread().getName()+str( '--> exiting \n')) + return + + +def looping_B(): + print (threading.currentThread().getName()+str('--> starting \n')) + time.sleep(2) + loop_count() + print (threading.currentThread().getName()+str( '--> exiting \n')) + return + + +def looping_C(): + print (threading.currentThread().getName()+str('--> starting \n')) + time.sleep(2) + loop_count() + print (threading.currentThread().getName()+str( '--> exiting \n')) + return + + +if __name__ == "__main__": + + t1 = threading.Thread(name='looping #A', target=looping_A) + t2 = threading.Thread(name='looping #B', target=looping_B) + t3 = threading.Thread(name='looping #C',target=looping_C) + + t1.start() + t2.start() + t3.start() + + t1.join() + t2.join() + t3.join() diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_name_and_processes.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_name_and_processes.py new file mode 100644 index 0000000..b2a42c0 --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Thread_name_and_processes.py @@ -0,0 +1,52 @@ +from threading import Thread +import time +import os +from random import randint + +class MyThreadClass (Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + + def loop_count(self): + loop_count = randint(1, 20) + print('Memulai proses looping sebanyak {}'.format(loop_count)) + for i in range(loop_count): + print(i) + print("selesai") + return time.sleep(5) + + + def run(self): + print("ID of process running {}".format(self.name)) #, " is {} \n".format(os.getpid())) + self.loop_count() + + +def main(): + from random import randint + + # Thread Creation + thread1 = MyThreadClass("Looping #1 ") + thread2 = MyThreadClass("Looping #2 ") + + + # Thread Running + thread1.start() + thread2.start() + + + # Thread joining + thread1.join() + thread2.join() + + # End + print("End") + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Threading_with_queue.py b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Threading_with_queue.py new file mode 100644 index 0000000..97e0d38 --- /dev/null +++ b/QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Threading_with_queue.py @@ -0,0 +1,52 @@ +""""Thread synchronisation with queue""" + +from threading import Thread +from queue import Queue +import time +import random + + +class Akang(Thread): + + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + self.name = "Kang Uji" + + def run(self): + for i in range(5): + item = random.randint(0, 256) + self.queue.put(item) + print('Akang uji membuat bakso di meja No. {} di buat oleh {}'.format(item, self.name)) + time.sleep(1) + + +class Service(Thread): + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + self.name = "A Rojak" + + def run(self): + while True: + item = self.queue.get() + print('Mengambil bakso untuk meja No. {} dan sedang di antarkan oleh {} ke meja'.format(item, self.name)) + self.queue.task_done() + +if __name__ == '__main__': + queue = Queue() + + t1 = Akang(queue) + t2 = Service(queue) + t3 = Service(queue) + t4 = Service(queue) + + t1.start() + t2.start() + t3.start() + t4.start() + + t1.join() + t2.join() + t3.join() + t4.join() diff --git a/QUIS SISTER 1/Soal Quis.txt b/QUIS SISTER 1/Soal Quis.txt index e153693..cb4c85c 100644 --- a/QUIS SISTER 1/Soal Quis.txt +++ b/QUIS SISTER 1/Soal Quis.txt @@ -4,4 +4,4 @@ masukan Quis di Repo QUis Sister 1 1. Buat Folder nama masing-masing 2. Kerjakan Chapter 2 -3. Upload, Push ke Git saya \ No newline at end of file +3. Upload, Push ke Gi \ No newline at end of file