diff --git a/Python-Pararel_SISTER b/Python-Pararel_SISTER new file mode 160000 index 0000000..5d0a2d8 --- /dev/null +++ b/Python-Pararel_SISTER @@ -0,0 +1 @@ +Subproject commit 5d0a2d8b3bedb03c7dd16ed5f6a24f0d4607d5b0 diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Barrier_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Barrier_Quis.py new file mode 100644 index 0000000..764332f --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Barrier_Quis.py @@ -0,0 +1,28 @@ + + +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +jumlah_running = 5 +selesai = Barrier(jumlah_running) +mobil = ['Honda', 'Toyota', 'Daihatsu', 'Hyundai', 'Suzuki', 'Mitsubishi', 'Nissan'] + +def jenis(): + brand = mobil.pop() + sleep(randrange(2, 5)) + print('%s Merupakan brand mobil \n' % (brand)) + selesai.wait() + +def main(): + threads = [] + print('Jenis Brand Mobil!') + for i in range(jumlah_running): + threads.append(Thread(target=jenis)) + threads[-1].start() + for thread in threads: + thread.join() + print('Udah Dulu Ya!') + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Condition_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Condition_Quis.py new file mode 100644 index 0000000..41b8b81 --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Condition_Quis.py @@ -0,0 +1,50 @@ +#import library +import threading +import time +import random + + +condition = threading.Condition() #membuat variabel condition untuk menampung fitur condition pada threading + +#membuat class +class Ngambil(): + #membuat fungsi + + def kuning(self): + print('Hati-Hati') + + with condition: + print('Lampu Kuning') + condition.wait() + print('Tetap Waspada di jalan') + + def lalin(self): + print('Berhenti') + + with condition: + print('Lampu Merah') + condition.wait() + print('Udah hijau, jalan') + + def jalan(self): + print('nunggu lajur yang lain') + + with condition: + time = random.randint(25,30) + waktu = random.randint(5,20) + print('{} detik lagi'.format(time)) + print('{} detik lagi'.format(waktu)) + condition.notify() + +ambil = Ngambil() +kuning = threading.Thread(target=ambil.kuning) +lalin = threading.Thread(target=ambil.lalin) +jalan = threading.Thread(target=ambil.jalan) + +kuning.start() +lalin.start() +jalan.start() + +kuning.join() +lalin.join() +jalan.join() \ No newline at end of file diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Event_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Event_Quis.py new file mode 100644 index 0000000..be25efc --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Event_Quis.py @@ -0,0 +1,20 @@ +import logging +import threading +import time + +logging.basicConfig(level=logging.DEBUG, + format='[%(levelname)s] (%(threadName)-10s) %(message)s',) + +def worker(i,dt,e): + tStart=time.time() + e.wait(dt) + logging.debug('{0} tried to wait {1} seconds but really waited {2}'.format(i,dt, time.time()-tStart )) + + +e = threading.Event() + +maxThreads=10 +for i in range(maxThreads): + dt=1+i # (s) + w = threading.Thread(target=worker, args=(i,dt,e)) + w.start() \ No newline at end of file diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/MyThreadClass_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/MyThreadClass_Quis.py new file mode 100644 index 0000000..3ac1731 --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/MyThreadClass_Quis.py @@ -0,0 +1,35 @@ +#!/usr/bin/python + +import threading +import time + +exitFlag = 0 + +class myThread (threading.Thread): + def __init__(self, threadID, name, counter): + threading.Thread.__init__(self) + self.threadID = threadID + self.name = name + self.counter = counter + def run(self): + print ("Starting " + self.name) + print_time(self.name, 5, self.counter) + print ("Exiting " + self.name) + +def print_time(threadName, counter, delay): + while counter: + if exitFlag: + threadName.exit() + time.sleep(delay) + print ("%s: %s" % (threadName, time.ctime(time.time()))) + counter -= 1 + +# Create new threads +thread1 = myThread(1, "Thread-1", 1) +thread2 = myThread(2, "Thread-2", 2) + +# Start new Threads +thread1.start() +thread2.start() + +print ("Exiting Main Thread") \ No newline at end of file diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/MyThreadClass_lock_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/MyThreadClass_lock_Quis.py new file mode 100644 index 0000000..937266e --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/MyThreadClass_lock_Quis.py @@ -0,0 +1,63 @@ +#!/usr/bin/python + +import os +import threading +import time +from threading import Thread +from random import randint + +exitFlag = 0 +lock = threading.Lock() +bounded_semaphore = threading.BoundedSemaphore(100) #penggunaan semaphore + +class myThread (threading.Thread): + def __init__(self, threadID, name, counter): + threading.Thread.__init__(self) + self.threadID = threadID + self.name = name + self.counter = counter + def run(self): + print ("Starting " + self.name) + print_time(self.name, 5, self.counter) + print ("Exiting " + self.name) + +#membuat fungsi +def f1(): + bounded_semaphore.acquire() + print("%s acquired lock." % (threading.current_thread().name)) + print(bounded_semaphore._value) + bounded_semaphore.release() + print("%s released lock." % (threading.current_thread().name)) + print(bounded_semaphore._value) + + +def print_time(threadName, counter, delay): + while counter: + if exitFlag: + threadName.exit() + time.sleep(delay) + print ("%s: %s" % (threadName, time.ctime(time.time()))) + counter -= 1 + +# Create new threads +thread1 = myThread(1, "Thread-1", 1) +thread2 = myThread(2, "Thread-2", 2) + +# Start new Threads +thread1.start() +thread2.start() + +t1 = threading.Thread(target=f1) +t2 = threading.Thread(target=f1) +t3 = threading.Thread(target=f1) +t4 = threading.Thread(target=f1) +t5 = threading.Thread(target=f1) +t1.start() +t2.start() +t3.start() +t4.start() +t5.start() + +print("Main Thread Exited.", threading.main_thread()) + +print ("Exiting Main Thread") diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Rlock_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Rlock_Quis.py new file mode 100644 index 0000000..614996a --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Rlock_Quis.py @@ -0,0 +1,67 @@ +import threading +import time +import random + +noabsen = int(input('Masukan No Absen Anda : ')) + +if noabsen > 100 : + print("Anda Anggota Dapartemen Manifest") +elif noabsen > 1 < 80 : + print("Anda Anggota Dapartemen Beacukai") +else: + print("Anda Pegawai Operation") + +class Container: + 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 getin(container, items): + print("Barang {} Telah Masuk \n".format(items)) + while items: + container.add() + time.sleep(1) + items -= 1 + print("Menambahkan Satu Barang -->{} Barang Ditambahkan \n".format(items)) + + + +def getout(container, items): + print("Barang {} Telah Keluar \n".format(items)) + while items: + container.remove() + time.sleep(1) + items -= 1 + print("Mengeluarkan Satu Barang -->{} Barang Dikeluarkan \n".format(items)) + + +def main(): + items = 10 + container = Container() + + t1 = threading.Thread(target=getin, \ + args=(container, random.randint(10,20))) + t2 = threading.Thread(target=getout, \ + args=(container, random.randint(1,10))) + + t1.start() + t2.start() + + + t1.join() + t2.join() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Semaphore_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Semaphore_Quis.py new file mode 100644 index 0000000..3c3a829 --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Semaphore_Quis.py @@ -0,0 +1,26 @@ +#imprt library +import threading + +bounded_semaphore = threading.BoundedSemaphore(100) #penggunaan semaphore + +#membuat fungsi +def f1(): + bounded_semaphore.acquire() + print("%s acquired lock." % (threading.current_thread().name)) + print(bounded_semaphore._value) + bounded_semaphore.release() + print("%s released lock." % (threading.current_thread().name)) + print(bounded_semaphore._value) + +t1 = threading.Thread(target=f1) +t2 = threading.Thread(target=f1) +t3 = threading.Thread(target=f1) +t4 = threading.Thread(target=f1) +t5 = threading.Thread(target=f1) +t1.start() +t2.start() +t3.start() +t4.start() +t5.start() + +print("Main Thread Exited.", threading.main_thread()) \ No newline at end of file diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Thread_definition_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Thread_definition_Quis.py new file mode 100644 index 0000000..7e0db82 --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Thread_definition_Quis.py @@ -0,0 +1,23 @@ +from random import randrange +import threading +from time import ctime, sleep + +hobby = ['Singing','Cooking','Swiming','Drawing','Play Game'] + +def hobbies(thread_number): + mine = hobby.pop() + sleep(randrange(2, 5)) + print('My Hobbies List {}'.format(thread_number)) + return print('%s Merupakan Hobby Saya \n' % (mine)) + + +def main(): + threads = [] + for i in range(5): + t = threading.Thread(target=hobbies, args=(i,)) + threads.append(t) + t.start() + t.join() + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Thread_determine_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Thread_determine_Quis.py new file mode 100644 index 0000000..60d30c4 --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Thread_determine_Quis.py @@ -0,0 +1,30 @@ + +import threading +import time + + +def merah(): + print ('Lampu '+ str(threading.currentThread().getName())+( '--> Berhenti \n')) + return + +def kuning(): + print ('Lampu '+ str(threading.currentThread().getName())+( '--> Bersiap \n')) + return + +def hijau(): + time.sleep(2) #waktu jeda + print ('Lampu '+str(threading.currentThread().getName())+( '--> Jalan \n')) + return + + +t1 = threading.Thread(name='Merah', target=merah) +t2 = threading.Thread(name='kuning', target=kuning) +t3 = threading.Thread(name='hijau',target=hijau) + +t1.start() +t2.start() +t3.start() + +t1.join() +t2.join() +t3.join() diff --git a/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Threading_with_queue_Quis.py b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Threading_with_queue_Quis.py new file mode 100644 index 0000000..c41a668 --- /dev/null +++ b/QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Threading_with_queue_Quis.py @@ -0,0 +1,59 @@ +from datetime import timedelta, datetime +from time import sleep + +class Queue: + def __init__(self): + self.items=[] + def isEmpty(self): + return self.items == [] + def enqueue(self,item): + self.items.insert(0,item) + def dequeue(self): + return self.items.pop() + def rear(self): + return self.items[0] + def front(self): + return self.items[len(self.items)-1] + def size(self): + return len(self.items) + def open(self): + return self.items + +def antrian(): + endtime = datetime.now() + timedelta(seconds = 2) + tanda='n' + m = Queue() + cad = Queue() + inputan = int(input('Masukan berapa orang yang ingin antri = ')) + for i in range(inputan): + nama = input('Masukan nama costumer ke %i = '%(i+1)) + m.enqueue(nama) + cad.enqueue(nama) + + print("Estimasi Jam Pelayanan Customer") + while not m.isEmpty(): + if not m.isEmpty(): + if tanda=='n': + print(m.dequeue(),'akan dilayani pada :',datetime.now()) + tanda='y' + else: + print(m.dequeue(),'akan dilayani pada :',endtime) + endtime = endtime + timedelta(seconds = 2) + + tanda='n' + print("=======================Antrian======================") + while not cad.isEmpty(): + if not cad.isEmpty(): + if tanda==0: + print(cad.dequeue(),'sedang dilayani') + tanda=1 + else: + sleep(2) + print(cad.dequeue(),'sedang dilayani') + + if cad.isEmpty(): + print('===============Antrian Kosong==================') + antrian() + +antrian() + \ No newline at end of file