diff --git a/QUIS SISTER 1/Eni Lestari/Condition.py b/QUIS SISTER 1/Eni Lestari/Condition.py new file mode 100644 index 0000000..8c365a5 --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/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 Mahasiswa(threading.Thread): + def _init_(self, *args, **kwargs): + super()._init_(*args, **kwargs) + + def urut(self): + + with condition: + + if len(items) == 5: + logging.info('no urut') + condition.wait() + + items.pop() + logging.info('no urut {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(7): + time.sleep(2) + self.urut() + + +class Dosen(threading.Thread): + def _init_(self, *args, **kwargs): + super()._init_(*args, **kwargs) + + def Quiz(self): + + with condition: + + if len(items) == 13: + logging.info('Quiz {}. Stopped'.format(len(items))) + condition.wait() + + items.append(1) + logging.info('waktu Quiz {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(20): + time.sleep(0.5) + self.Quiz() + + +def main(): + urut = Mahasiswa(name='Mahasiswa') + Quiz = Dosen(name='Dosen') + + urut.start() + Quiz.start() + + urut.join() + Quiz.join() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Eni Lestari/Event.py b/QUIS SISTER 1/Eni Lestari/Event.py new file mode 100644 index 0000000..9cffb00 --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/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 Mahasiswa(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('Mahasiswa notify: {} Mahasiswa menuju ruang Dosen {}'\ + .format(item, self.name)) + +class Dosen(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('Dosen notify: Dosen menandatangani laporan hasil proyek3 Mahasiswa {}'\ + .format(item, self.name)) + event.set() + event.clear() + +if __name__ == "__main__": + t1 = Mahasiswa() + t2 = Dosen() + + t1.start() + t2.start() + + t1.join() + t2.join() \ No newline at end of file diff --git a/QUIS SISTER 1/Eni Lestari/MyThreadClass.py b/QUIS SISTER 1/Eni Lestari/MyThreadClass.py new file mode 100644 index 0000000..5039bf7 --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/MyThreadClass.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 CerdasCermat (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 + \ + " kodenya adalah "\ + + str(os.getpid()) + "\n") + threadLock.release() + time.sleep(self.duration) + print ("---> " + self.name + " telah siap \n") + #Release the Lock + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = CerdasCermat("Ruangan Cerdas cermat IPA yang digunakan ", randint(1,10)) + thread2 = CerdasCermat("Soal 4", randint(1,10)) + + + + # Thread Running + thread1.start() + thread2.start() + + + + # Thread joining + thread1.join() + thread2.join() + + + # End + print("Selamat Datang di Ruangan Cerdas cermat IPA para peserta acara lomba ") + print("Selamat Mengerjakan ") + + #Execution Time + print("waktu yang dibutuhkan untuk mengerjakan soal %s seconds soal" % (time.time() - start_time)) + + + +if __name__ == "_main_": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Eni Lestari/MyThreadClass_lock.py b/QUIS SISTER 1/Eni Lestari/MyThreadClass_lock.py new file mode 100644 index 0000000..47344ea --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/MyThreadClass_lock.py @@ -0,0 +1,45 @@ +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 + \ + " Mahasiswa diberikan no urut "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " over\n") + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = Mahasiswa("no urut mahasiswa ", randint(1,10)) + thread2 = Mahasiswa("no urut mahasiswa", randint(1,10)) + + + # Thread Running + thread1.start() + thread2.start() + + + # Thread joining + thread1.join() + thread2.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/Eni Lestari/MyThreadClass_lock_2.py b/QUIS SISTER 1/Eni Lestari/MyThreadClass_lock_2.py new file mode 100644 index 0000000..29cbb1c --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/MyThreadClass_lock_2.py @@ -0,0 +1,49 @@ +import threading +import time +import os +from threading import Thread +from random import randint + +# Lock Definition +threadLock = threading.Lock() + +class Mahasiswa (Thread): + def _init_(self, name, duration): + Thread._init_(self) + self.name = name + self.duration = duration + def run(self): + print ("---> " + self.name + \ + " mahasiswa diberikan no urut "\ + + str(os.getpid()) + "\n") + time.sleep(self.duration) + print ("---> " + self.name + " over\n") + + +def main(): + start_time = time.time() + + # Thread Creation + thread1 = Mahasiswa("no urut mahasiswa ", randint(1,10)) + thread2 = Mahasiswa("no urut mahasiswa", randint(1,10)) + + + # Thread Running + thread1.start() + thread2.start() + + + # Thread joining + thread1.join() + thread2.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/Eni Lestari/Thread_definition.py b/QUIS SISTER 1/Eni Lestari/Thread_definition.py new file mode 100644 index 0000000..b5052ca --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/Thread_definition.py @@ -0,0 +1,18 @@ +import threading + + +def jilbab(thread_number): + return print('Jumlah Jilbab yang ada ditoko{} pcs'.format(thread_number)) + + +def main(): + threads = [] + for i in range(10): + t = threading.Thread(target=jilbab, args=(i,)) + threads.append(t) + t.start() + t.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Eni Lestari/Thread_determine.py b/QUIS SISTER 1/Eni Lestari/Thread_determine.py new file mode 100644 index 0000000..4f53832 --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/Thread_determine.py @@ -0,0 +1,33 @@ +import threading +import time + +from django.urls import clear_script_prefix + +def Minum_Sahur(): + print (threading.currentThread().getName()+str('--> Energen Coklat \n')) + time.sleep(3) + print (threading.currentThread().getName()+str( '-->selesai minum energen coklat \n')) + return + +def Minum_Buka(): + print (threading.currentThread().getName()+str('--> Es Buah \n')) + time.sleep(2) + print (threading.currentThread().getName()+str( '--> selesai minum es buah \n')) + return + + + + +if __name__ == "__main__": + + t1 = threading.Thread(name='Minum_Sahur', target=Minum_Sahur) + t2 = threading.Thread(name='Minum_Buka', target=Minum_Buka) + + + t1.start() + t2.start() + clear_script_prefix + + t1.join() + t2.join() + diff --git a/QUIS SISTER 1/Eni Lestari/Thread_name_and_processes.py b/QUIS SISTER 1/Eni Lestari/Thread_name_and_processes.py new file mode 100644 index 0000000..138d7c1 --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/Thread_name_and_processes.py @@ -0,0 +1,38 @@ +from threading import Thread +import time +import os + +class TarianClass (Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + def run(self): + print("Tarian Khas Sumatera Selatan {}".format(self.name)) #, " is {} \n".format(os.getpid())) + +def main(): + from random import randint + # Thread Creation + thread1 = TarianClass("Gending Sriwijaya") + thread2 = TarianClass("Tanggai") + thread3 = TarianClass("Erai-Erai") + thread4 = TarianClass("Setudung Sedulang") + + # Thread Running + thread4.start() + thread2.start() + thread3.start() + thread1.start() + + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + thread4.join() + + # End + print("End") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Eni Lestari/Threading_with_queue.py b/QUIS SISTER 1/Eni Lestari/Threading_with_queue.py new file mode 100644 index 0000000..bdefb42 --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/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 PanitiaKurban(Thread): + + def init(self, queue): + Thread.init(self) + self.queue = queue + + def run(self): + for i in range(5): + item = random.randint(0, 256) + self.queue.put(item) + print('Panitia Kurban notify : Penerima kurban dengan nomor %d ditambahkan pada antrian oleh %s\n'\ + % (item, self.name)) + time.sleep(1) + + +class PenerimaKurban(Thread): + + def init(self, queue): + Thread.init(self) + self.queue = queue + + def run(self): + while True: + item = self.queue.get() + print('Penerima kurban notify :nomor %d Penerima Kurban dimunculkan oleh %s'\ + % (item, self.name)) + self.queue.task_done() + +if __name__ == '_main_': + queue = Queue() + + t1 = PanitiaKurban(queue) + t2 = PenerimaKurban(queue) + t3 = PenerimaKurban(queue) + t4 = PenerimaKurban(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/Eni Lestari/barrier.py b/QUIS SISTER 1/Eni Lestari/barrier.py new file mode 100644 index 0000000..37beb49 --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/barrier.py @@ -0,0 +1,26 @@ +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +num_QuisDadakan = 5 +hasilakhir = Barrier(num_QuisDadakan) +peserta = ['eni', 'eris', 'nazhim', 'helmi', 'siti'] + +def siswa(): + name = peserta.pop() + sleep(randrange(2, 7)) + print('%s selesai mengerjakan pada: %s \n' % (name, ctime())) + hasilakhir.wait() + +def main(): + threads = [] + print('mulai mengerjakan!!') + for i in range(num_QuisDadakan): + threads.append(Thread(target=siswa)) + threads[-1].start() + for thread in threads: + thread.join() + print('selesai!') + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Eni Lestari/rlock.py b/QUIS SISTER 1/Eni Lestari/rlock.py new file mode 100644 index 0000000..d50373e --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/rlock.py @@ -0,0 +1,60 @@ +import threading +import time +import random + + +class BoxKurma: + 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(boxkurma, items): + print("Banyaknya kurma {} yang ditambahkan \n".format(items)) + while items: + boxkurma.add() + time.sleep(1) + items -= 1 + print("stok buah kurma saat ini -->{} buah \n".format(items)) + + + +def remover(boxkurma, items): + print("banyaknya kurma {} yang di jual \n".format(items)) + while items: + boxkurma.remove() + time.sleep(1) + items -= 1 + print("kurma yang dijual -->{} buah \n".format(items)) + + +def main(): + items = 12 + box = BoxKurma() + + t1 = threading.Thread(target=adder, \ + args=(box, random.randint(1,10))) + t2 = threading.Thread(target=remover, \ + args=(box, random.randint(1,10))) + + t1.start() + t2.start() + + + t1.join() + t2.join() + + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Eni Lestari/semaphore.py b/QUIS SISTER 1/Eni Lestari/semaphore.py new file mode 100644 index 0000000..bd8e2c0 --- /dev/null +++ b/QUIS SISTER 1/Eni Lestari/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 pemenangundian(): + logging.info('pengundian tahunan bank dimulai semua nasabah harap menunggu') + semaphore.acquire() + logging.info('pemenang undian dengan nomor {} harap dapat datang ke bank'.format(item)) + + +def nomorundian(): + global item + time.sleep(3) + item = random.randint(1, 10) + logging.info('nomor {} adalah pemenang undian'.format(item)) + semaphore.release() + + +def main(): + for i in range(1): + t1 = threading.Thread(target=pemenangundian) + t2 = threading.Thread(target=nomorundian) + + t1.start() + t2.start() + + t1.join() + t2.join() + + +if __name__ == "__main__": + main()