Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions QUIS SISTER 1/Quis Vicky Saira 1184037/Barrier_Quis Vicky.py
Original file line number Diff line number Diff line change
@@ -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()
73 changes: 73 additions & 0 deletions QUIS SISTER 1/Quis Vicky Saira 1184037/Condition_Quis Vicky.py
Original file line number Diff line number Diff line change
@@ -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()
38 changes: 38 additions & 0 deletions QUIS SISTER 1/Quis Vicky Saira 1184037/Event_Quis Vicky.py
Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
@@ -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()




35 changes: 35 additions & 0 deletions QUIS SISTER 1/Quis Vicky Saira 1184037/MyThreadClass_Quis Vicky.py
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
@@ -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")
26 changes: 26 additions & 0 deletions QUIS SISTER 1/Quis Vicky Saira 1184037/Rlock_Quis Vicky.py
Original file line number Diff line number Diff line change
@@ -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")
33 changes: 33 additions & 0 deletions QUIS SISTER 1/Quis Vicky Saira 1184037/Semaphore_Quis Vicky.py
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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()
Loading