diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Barrier.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Barrier.py new file mode 100644 index 0000000..c73c38c --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Barrier.py @@ -0,0 +1,27 @@ +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +num_persons = 3 +finish = Barrier(num_persons) +persons = ['Si A', 'Si B', 'Si C'] + +def editing(): + name = persons.pop() + sleep(randrange(2, 5)) + print('%s Mulai mengedit pada: %s ' % (name, ctime())) + finish.wait() + print('%s Selesai mengedit pada: %s ' % (name, ctime())) + +def main(): + threads = [] + print('Editing dimulai!') + for i in range(num_persons): + threads.append(Thread(target=editing)) + threads[-1].start() + for thread in threads: + thread.join() + print('Editing selesai!') + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Condition.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Condition.py new file mode 100644 index 0000000..08d9480 --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Condition.py @@ -0,0 +1,69 @@ +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 beli(self): + + with condition: + + if len(items) == 0: + logging.info('Tidak ada makanan yang dapat dibeli') + condition.wait() + + items.pop() + logging.info('Kamu membeli 1 item makanan') + + condition.notify() + + def run(self): + for i in range(20): + time.sleep(2) + self.beli() + + +class Penjual(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 {} item makanan saat ini'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(20): + time.sleep(0.5) + self.jual() + + +def main(): + pembeli = Penjual(name='Penjual') + penjual = Pembeli(name='Pembeli') + + penjual.start() + pembeli.start() + + penjual.join() + pembeli.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Event.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Event.py new file mode 100644 index 0000000..3b5e93f --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Event.py @@ -0,0 +1,45 @@ +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 Pelanggan(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('Kendaraan dengan no. urut {} telah dicuci {} dan siap dikeluarkan'.format(item, self.name)) + +class Pencuci(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def run(self): + for i in range(5): + time.sleep(3) + item = random.randint(0, 100) + items.append(item) + logging.info('Kendaraan dengan no. urut {} masuk ke ruang cuci yang dikendarai oleh petugas cuci {}'.format(item, self.name)) + event.set() + event.clear() + +if __name__ == "__main__": + t1 = Pencuci() + t2 = Pelanggan() + + t1.start() + t2.start() + + t1.join() + t2.join() diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/MyThreadClass.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/MyThreadClass.py new file mode 100644 index 0000000..a3143f8 --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/MyThreadClass.py @@ -0,0 +1,49 @@ +import time +import os +from random import randint +from threading import Thread + +class PetugasCucian (Thread): + def __init__(self, name, duration): + Thread.__init__(self) + self.name = name + self.duration = duration + def run(self): + print ("---> " + self.name + \ + " sedang bekerja, mencuci kendaraan dengan plat nomor "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " telah selesai mengerjakan\n") + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = PetugasCucian("Petugas 1 ", randint(1,10)) + thread2 = PetugasCucian("Petugas 2 ", randint(1,10)) + thread3 = PetugasCucian("Petugas 3 ", randint(1,10)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + + # End + print("Kendaraan diserahkan ke pemilik") + + #Execution Time + print("Total waktu mencuci dengan 3 orang pekerja adalah %s detik" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/MyThreadClass_lock.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/MyThreadClass_lock.py new file mode 100644 index 0000000..fa9336a --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/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 Montir (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 + \ + " sedang bekerja, memperbaiki mobil dengan plat nomor "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " istirahat\n") + #Release the Lock + threadLock.release() + + +def main(): + start_time = time.time() + # Thread Creation + thread1 = Montir("Montir 1 ", randint(1,10)) + thread2 = Montir("Montir 2 ", randint(1,10)) + thread3 = Montir("Montir 3 ", randint(1,10)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + + # End + print("Mobil selesai diperbaiki") + + #Execution Time + print("Perbaikan memakan waktu %s detik" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/MyThreadClass_lock_2.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/MyThreadClass_lock_2.py new file mode 100644 index 0000000..e33d850 --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/MyThreadClass_lock_2.py @@ -0,0 +1,57 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# Lock Definition +threadLock = threading.Lock() + +class PitStopF1 (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 + \ + " mulai mengganti roda F1, ini merupakan roda pengganti F1 ke "\ + + str(os.getpid()) + "\n") + threadLock.release() + time.sleep(self.duration) + print ("---> " + self.name + " selesai mengganti roda F1\n") + #Release the Lock + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = PitStopF1("Montir 1 ", randint(1,5)) + thread2 = PitStopF1("Montir 2 ", randint(1,5)) + thread3 = PitStopF1("Montir 3 ", randint(1,5)) + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + + # End + print("Mobil F1 melaju kembali ke jalur balapan") + + #Execution Time + print("Service pitstop memakan waktu %s detik" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Rlock.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Rlock.py new file mode 100644 index 0000000..2289cc5 --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Rlock.py @@ -0,0 +1,60 @@ +import threading +import time +import random + + +class RuangCuci: + def __init__(self): + self.lock = threading.RLock() + self.total_items = 0 + + def execute(self, value): + with self.lock: + self.total_items += value + + def masuk(self): + with self.lock: + self.execute(1) + + def keluar(self): + with self.lock: + self.execute(-1) + +def memasukkan(ruangcuci, items): + print("Terdapat {} kendaraan yang akan dicuci \n".format(items)) + while items: + ruangcuci.masuk() + time.sleep(1) + items -= 1 + print("1 kendaraan telah masuk -->{} kendaraan menunggu untuk masuk \n".format(items)) + + + +def mengeluarkan(ruangcuci, items): + print("Terdapat {} slot tempat pencucian. Kendaraan harus segera diselesaikan dan dikeluarkan \n".format(items)) + while items: + ruangcuci.keluar() + time.sleep(1) + items -= 1 + print("1 kendaraan telah keluar -->{} kendaraan nanti akan dikeluarkan bertahap \n".format(items)) + + +def main(): + items = 10 + ruangcuci = RuangCuci() + + t1 = threading.Thread(target=memasukkan, \ + args=(ruangcuci, random.randint(10,20))) + t2 = threading.Thread(target=mengeluarkan, \ + args=(ruangcuci, random.randint(1,10))) + + t1.start() + t2.start() + + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Semaphore.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Semaphore.py new file mode 100644 index 0000000..e61e10f --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Semaphore.py @@ -0,0 +1,41 @@ +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 pelanggan(): + logging.info('Pelanggan order!') + semaphore.acquire() + logging.info('Pesanan selesai: Pelanggan mengambil pesanan dengan id {}'.format(item)) + + +def restaurant(): + global item + time.sleep(3) + item = random.randint(0, 1000) + logging.info('Pesanan diterima: Cheff memasak pesanan dengan id {}'.format(item)) + semaphore.release() + + +def main(): + for i in range(10): + t1 = threading.Thread(target=pelanggan) + t2 = threading.Thread(target=restaurant) + + t1.start() + t2.start() + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_definition.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_definition.py new file mode 100644 index 0000000..1fe503c --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_definition.py @@ -0,0 +1,11 @@ +import time +from threading import Thread + +def myfunc(i): + print ("Peserta yang berangkat dengan nomor urut %d" % i) + time.sleep(5) + print ("Peserta yang kembali dengan nomor urut %d" % i) + +for i in range(10): + t = Thread(target=myfunc, args=(i,)) + t.start() \ No newline at end of file diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_determine.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_determine.py new file mode 100644 index 0000000..9c7fdc2 --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_determine.py @@ -0,0 +1,26 @@ +import threading +import time + +def CuciMobil(): + print (threading.currentThread().getName()+str(' -> Pencucian mobil dimulai \n')) + time.sleep(5) + print (threading.currentThread().getName()+str( ' -> Pencucian mobil baru saja selesai \n')) + return + +def CuciMotor(): + print (threading.currentThread().getName()+str(' -> Pencucian motor dimulai \n')) + time.sleep(2) + print (threading.currentThread().getName()+str( ' -> Pencucian motor telah selesai lebih dulu \n')) + return + + +if __name__ == "__main__": + + t1 = threading.Thread(name='Cuci Mobil', target=CuciMobil) + t2 = threading.Thread(name='Cuci Motor', target=CuciMotor) + + t1.start() + t2.start() + + t1.join() + t2.join() diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_name_and_processes.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_name_and_processes.py new file mode 100644 index 0000000..3f8978e --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Thread_name_and_processes.py @@ -0,0 +1,35 @@ +from threading import Thread +import time +import os + +class Loket (Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + def run(self): + print("Penumpang yang sedang melakukan check-in berada pada {}".format(self.name)) + +def main(): + from random import randint + antrian1 = Loket("Loket 1 ") + antrian2 = Loket("Loket 2 ") + antrian3 = Loket("Loket 3 ") + + antrian1.start() + antrian2.start() + antrian3.start() + + antrian1.join() + antrian2.join() + antrian3.join() + + print("Selesai!") + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Threading_with_queue.py b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Threading_with_queue.py new file mode 100644 index 0000000..39b09d9 --- /dev/null +++ b/QUIS SISTER 1/QUIS SISTER 1194004 Alwizain Almas/Threading_with_queue.py @@ -0,0 +1,53 @@ +""""Thread synchronisation with queue""" + +from threading import Thread +from queue import Queue +import time +import random + + +class CuciMobil(Thread): + + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + + def run(self): + for i in range(10): + item = random.randint(1, 20) + self.queue.put(item) + print('Informasi : mobil dengan antrian nomor urut %d silahkan masuk -> %s\n'\ + % (item, self.name)) + time.sleep(1) + + +class Pengunjung(Thread): + + def __init__(self, queue): + Thread.__init__(self) + self.queue = queue + + def run(self): + while True: + item = self.queue.get() + print('Mobil dengan nomor urut %d memasuki ruang cuci -> %s'\ + % (item, self.name)) + self.queue.task_done() + +if __name__ == '__main__': + queue = Queue() + + t1 = CuciMobil(queue) + t2 = Pengunjung(queue) + t3 = Pengunjung(queue) + t4 = Pengunjung(queue) + + t1.start() + t2.start() + t3.start() + t4.start() + + t1.join() + t2.join() + t3.join() + t4.join() \ No newline at end of file