From a656355344dbe97aa6d226196b77cebb881da0be Mon Sep 17 00:00:00 2001 From: Vicky Safira 1184037 Date: Wed, 20 Apr 2022 17:41:53 +0700 Subject: [PATCH] Quis Vicky Safira 1184037 --- .../Barrier_Quis Vicky.py | 26 +++++++ .../Condition_Quis Vicky.py | 73 ++++++++++++++++++ .../Event_Quis Vicky.py | 38 ++++++++++ .../MyThreadClass_Lock_2_Quis Vicky.py | 75 +++++++++++++++++++ .../MyThreadClass_Quis Vicky.py | 35 +++++++++ .../MyThreadClass_lock_Quis Vicky.py | 63 ++++++++++++++++ .../Rlock_Quis Vicky.py | 26 +++++++ .../Semaphore_Quis Vicky.py | 33 ++++++++ .../Thread_definition_Quis Vicky.py | 17 +++++ .../Thread_name_and_processes_Quis Vicky.py | 40 ++++++++++ .../Threading_with_queue_Quis Vicky.py | 68 +++++++++++++++++ 11 files changed, 494 insertions(+) create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/Barrier_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/Condition_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/Event_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Lock_2_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_lock_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/Rlock_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/Semaphore_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/Thread_definition_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/Thread_name_and_processes_Quis Vicky.py create mode 100644 QUIS SISTER 1/Quis Vicky Saira 1184037/Threading_with_queue_Quis Vicky.py diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/Barrier_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/Barrier_Quis Vicky.py new file mode 100644 index 0000000..cf55ec1 --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/Barrier_Quis Vicky.py @@ -0,0 +1,26 @@ +from random import randrange +from threading import Barrier, Thread +from time import ctime, sleep + +num_runners = 3 +finish_line = Barrier(num_runners) +runners = ['Vicky', 'Zamzam', 'Kurnia'] + +def runner(): + name = runners.pop() + sleep(randrange(2, 5)) + print('%s reached the barrier at: %s \n' % (name, ctime())) + finish_line.wait() + +def main(): + threads = [] + print('MELUNCUR !!!') + for i in range(num_runners): + threads.append(Thread(target=runner)) + threads[-1].start() + for thread in threads: + thread.join() + print('STOP!') + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/Condition_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/Condition_Quis Vicky.py new file mode 100644 index 0000000..d54a13f --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/Condition_Quis Vicky.py @@ -0,0 +1,73 @@ +import logging +import threading + +LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s' +logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) + +items = [] +condition = threading.Condition() + + +class Consumer(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def consume(self): + + with condition: + + if len(items) == 0:#kondisi stack kosong + logging.info('gaada apa apa') + logging.info('stop gaes') + condition.wait()#melakukan pause sampai dapat notify baru jalan lagi + logging.info('consume : kalo dapet notif jalan lagi') + + items.pop() + logging.info('consumed 1 item') + + condition.notify() + + def run(self): + for i in range(20): + #time.sleep(2) + self.consume() + + +class Producer(threading.Thread): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def produce(self): + + with condition: + + if len(items) == 5:#kondisi penuh + logging.info('items produced {}. Stopped'.format(len(items))) + logging.info('produce : ini mau berenti') + condition.wait()#pause disini setelah dapat notify baru jalan kembali + logging.info('produce : kalo dapet notif jalan lagi') + + items.append(11) + logging.info('total items {}'.format(len(items))) + + condition.notify() + + def run(self): + for i in range(20): + #time.sleep(0.5) + self.produce() + + +def main(): + producer = Producer(name='Producer') + consumer = Consumer(name='Consumer') + + producer.start() + consumer.start() + + producer.join() + consumer.join() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/Event_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/Event_Quis Vicky.py new file mode 100644 index 0000000..69a93b5 --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/Event_Quis Vicky.py @@ -0,0 +1,38 @@ +import threading +import time +import logging + +logging.basicConfig(level=logging.DEBUG, + format='(%(threadName)-9s) %(message)s',) + +def jalan(e): + logging.debug('Tunggu dulu') + event_is_set = e.wait() + logging.debug('kamu ada dimana?: %s', event_is_set) + +def pergi(e, t): + while not e.isSet(): + logging.debug('Kita healing kuy, kemana yaa serunya?') + event_is_set = e.wait(t) + logging.debug('ke Jogja kuy: %s', event_is_set) + if event_is_set: + logging.debug('terimakasih waktunya') + else: + logging.debug('Naik becak?') + +if __name__ == "__main__": + e = threading.Event() + t1 = threading.Thread(name='Vicky', + target=jalan, + args=(e,)) + t1.start() + + t2 = threading.Thread(name='Safira', + target=pergi, + args=(e, 2)) + t2.start() + + logging.debug('Waiting before calling Event.set()') + time.sleep(3) + e.set() + logging.debug('Capek juga') \ No newline at end of file diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Lock_2_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Lock_2_Quis Vicky.py new file mode 100644 index 0000000..c341000 --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Lock_2_Quis Vicky.py @@ -0,0 +1,75 @@ +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 run(self): + #Acquire the Lock + threadLock.acquire() + print ("---> " + self.name + \ + " Games nya idol "\ + + str(os.getpid()) + "\n") + threadLock.release() + time.sleep(self.duration) + print ("---> " + self.name + " over\n") + #Release the Lock + + +def main(): + start_time = time.time() + + # Thread Creation + vicky1 = MyThreadClass("Vicky#1 ", randint(1,10)) + heeseung2 = MyThreadClass("Heeseung#2 ", randint(1,10)) + jake3 = MyThreadClass("Jake#3 ", randint(1,10)) + haruto4 = MyThreadClass("Haruto#4 ", randint(1,10)) + winwin5 = MyThreadClass("Winwin#5 ", randint(1,10)) + hyunsuk6 = MyThreadClass("Hyunsuk#6 ", randint(1,10)) + junkyu7 = MyThreadClass("Junkyu#7 ", randint(1,10)) + jisung8 = MyThreadClass("Jisung#8 ", randint(1,10)) + minho9 = MyThreadClass("Minho#9 ", randint(1,10)) + + # Thread Running + vicky1.start() + heeseung2.start() + jake3.start() + haruto4.start() + winwin5.start() + hyunsuk6.start() + junkyu7.start() + jisung8.start() + minho9.start() + + # Thread joining + vicky1.join() + heeseung2.join() + jake3.join() + haruto4.join() + winwin5.join() + hyunsuk6.join() + junkyu7.join() + jisung8.join() + minho9.join() + + # End + print("End") + + #Execution Time + print("--- %s seconds ---" % (time.time() - start_time)) + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Quis Vicky.py new file mode 100644 index 0000000..29a60f8 --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Quis Vicky.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/Quis Vicky Saira 1184037/MyThreadClass_lock_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_lock_Quis Vicky.py new file mode 100644 index 0000000..b9f9951 --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_lock_Quis Vicky.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/Quis Vicky Saira 1184037/Rlock_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/Rlock_Quis Vicky.py new file mode 100644 index 0000000..0ba5cd8 --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/Rlock_Quis Vicky.py @@ -0,0 +1,26 @@ +# program to illustrate the use of RLocks + +# importing the module +import threading + +# initializing the shared resource +VickySafira = 0 + +# creating an RLock object instead +# of Lock object +lock = threading.RLock() + +# the below thread is trying to access +# the shared resource +lock.acquire() +VickySafira = VickySafira + 25 + +# the below thread is trying to access +# the shared resource +lock.acquire() +VickySafira = VickySafira + 32 +lock.release() +lock.release() + +# displaying the value of shared resource +print("VickySafira = 57") diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/Semaphore_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/Semaphore_Quis Vicky.py new file mode 100644 index 0000000..988fcf1 --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/Semaphore_Quis Vicky.py @@ -0,0 +1,33 @@ +# importing the modules +from threading import * +import time + +# creating thread instance where count = 3 +obj = Semaphore(1) + +# creating instance +def display(name): + + # calling acquire method + obj.acquire() + for i in range(1): + print('Jenis Bakso, ', end = '') + time.sleep(1) + print(name) + + # calling release method + obj.release() + +# creating multiple thread +t1 = Thread(target = display , args = ('Urat-1',)) +t2 = Thread(target = display , args = ('Beranak-2',)) +t3 = Thread(target = display , args = ('Mercon-3',)) +t4 = Thread(target = display , args = ('Telur-4',)) +t5 = Thread(target = display , args = ('Kecil-5',)) + +# calling the threads +t1.start() +t2.start() +t3.start() +t4.start() +t5.start() diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/Thread_definition_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/Thread_definition_Quis Vicky.py new file mode 100644 index 0000000..56493ef --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/Thread_definition_Quis Vicky.py @@ -0,0 +1,17 @@ +import threading + + +def my_func(thread_number): + return print('panggil vicky kalo ada perlu {}'.format(thread_number)) + + +def main(): + threads = [] + for i in range(5): + t = threading.Thread(target=my_func, args=(i,)) + threads.append(t) + t.start() + t.join() + +if __name__ == "__main__": + main() diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/Thread_name_and_processes_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/Thread_name_and_processes_Quis Vicky.py new file mode 100644 index 0000000..8eb2b0e --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/Thread_name_and_processes_Quis Vicky.py @@ -0,0 +1,40 @@ +from threading import Thread +import time +import os + +class MyThreadClass (Thread): + def __init__(self, name): + Thread.__init__(self) + self.name = name + + def run(self): + print("Vicky nctzen {}".format(self.name)) #, " is {} \n".format(os.getpid())) + +def main(): + from random import randint + # Thread Creation + thread1 = MyThreadClass("Thread#1 ") + thread2 = MyThreadClass("Thread#2 ") + thread3 = MyThreadClass("Thread#3 ") + + # Thread Running + thread1.start() + thread2.start() + thread3.start() + + + # Thread joining + thread1.join() + thread2.join() + thread3.join() + + # End + print("End") + + +if __name__ == "__main__": + main() + + + + diff --git a/QUIS SISTER 1/Quis Vicky Saira 1184037/Threading_with_queue_Quis Vicky.py b/QUIS SISTER 1/Quis Vicky Saira 1184037/Threading_with_queue_Quis Vicky.py new file mode 100644 index 0000000..8a37cd6 --- /dev/null +++ b/QUIS SISTER 1/Quis Vicky Saira 1184037/Threading_with_queue_Quis Vicky.py @@ -0,0 +1,68 @@ +def queue(): + s = [] + return s + + +def enqueue(s, i): + s.insert(0, i) + return s + + +def dequeue(s): + return s.pop() + + +def rear(s): + return (s[0]) + + +def front(s): + return (s[len(s) - 1]) + + +def size(s): + return len(s) + + +def isEmpty(s): + return s == [] + + +def No2(): + s = queue() + k = '' + while True: + banyak = int(input('Game valo berhadiah, mau ikut? isi partner tim mulai dari 1 - 9 = ')) + for j in range(banyak): + orang = input('undang teman kamu %i yang mau masuk dalam games ini = ' % (j + 1)) + enqueue(s, orang) + s.reverse() + print('jadi teman kamu masuk dalam permainan %s' % s) + s.reverse() + o = input('masukkan nama musuh kamu = ') + ditemukan = 't' + itung = 0 + while ditemukan == 't': + if o == front(s): + print('Chukkae! musuh ditemukan') + ditemukan = 'ok' + elif o != front(s): + masukan = dequeue(s) + enqueue(s, masukan) + ditemukan = 't' + s.reverse() + print('Looping %i = %s' % ((itung + 1), s)) + s.reverse() + itung += 1 + if itung > len(s): + print('Maaf kamu kurang beruntung, Coba lagi ya') + ditemukan = 'ya' + print('Total peserta yang di perlukan', str(itung - 5)) + k = input('apakah kamu mau melanjutkan permainannya? (y/t) ? ') + if k != 'y': + break + else: + print('Semangat, jangan menyerah' ) + + +No2() \ No newline at end of file