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_fadhelrahmawan/Barrier.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 = ['Ragil kuncoro', 'dimana', 'Alamat rumahmu']

def runner():
name = runners.pop()
sleep(randrange(3, 10))
print('%s reached the barrier at: %s \n' % (name, ctime()))#
finish_line.wait()

def main():
threads = []
print('Ayo Pergi jalan sama aku?!!!!')
for i in range(num_runners):
threads.append(Thread(target=runner))
threads[-1].start()
for thread in threads:
thread.join()
print('Ayo berangkat!')

if __name__ == "__main__":
main()
70 changes: 70 additions & 0 deletions QUIS SISTER 1/quis_fadhelrahmawan/Condition.py
Original file line number Diff line number Diff line change
@@ -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 Consumer(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def consume(self):

with condition:

if len(items) == 0:
logging.info('no items to consume')
condition.wait()

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) == 10:
logging.info('items produced {}. Stopped'.format(len(items)))
condition.wait()

items.append(1)
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_fadhelrahmawan/Event.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('sebentar')
event_is_set = e.wait()
logging.debug('kamu ada dimana?: %s', event_is_set)

def pergi(e, t):
while not e.isSet():
logging.debug('ayo kita pergi, kemana yaa?')
event_is_set = e.wait(t)
logging.debug('ke bandung yok: %s', event_is_set)
if event_is_set:
logging.debug('terimakasih waktunya')
else:
logging.debug('mobilnya bagus juga?')

if __name__ == "__main__":
e = threading.Event()
t1 = threading.Thread(name='bintang',
target=jalan,
args=(e,))
t1.start()

t2 = threading.Thread(name='bulan',
target=pergi,
args=(e, 2))
t2.start()

logging.debug('Waiting before calling Event.set()')
time.sleep(3)
e.set()
logging.debug('melelahkan juga ya')
67 changes: 67 additions & 0 deletions QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import time
import os
from random import randint
from threading import Thread

class MyThreadClass (Thread):
def __init__(self, name, duration):
Thread.__init__(self)
self.name = name
self.duration = duration
def run(self):
print ("---> " + self.name + \
" running, belonging to process ID "\
+ str(os.getpid()) + "\n")
time.sleep(self.duration)
print ("---> " + self.name + " over\n")


def main():
start_time = time.time()

# Thread Creation
thread1 = MyThreadClass("Thread#1 ", randint(1,10))
thread2 = MyThreadClass("Thread#2 ", randint(1,10))
thread3 = MyThreadClass("Thread#3 ", randint(1,10))
thread4 = MyThreadClass("Thread#4 ", randint(1,10))
thread5 = MyThreadClass("Thread#5 ", randint(1,10))
thread6 = MyThreadClass("Thread#6 ", randint(1,10))
thread7 = MyThreadClass("Thread#7 ", randint(1,10))
thread8 = MyThreadClass("Thread#8 ", randint(1,10))
thread9 = MyThreadClass("Thread#9 ", randint(1,10))

# Thread Running
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread8.start()
thread9.start()

# Thread joining
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
thread6.join()
thread7.join()
thread8.join()
thread9.join()

# End
print("End")

#Execution Time
print("--- %s seconds ---" % (time.time() - start_time))


if __name__ == "__main__":
main()




74 changes: 74 additions & 0 deletions QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 + \
" running, belonging to process ID "\
+ str(os.getpid()) + "\n")
time.sleep(self.duration)
print ("---> " + self.name + " over\n")
#Release the Lock
threadLock.release()


def main():
start_time = time.time()
# Thread Creation
thread1 = MyThreadClass("Thread#1 ", randint(1,10))
thread2 = MyThreadClass("Thread#2 ", randint(1,10))
thread3 = MyThreadClass("Thread#3 ", randint(1,10))
thread4 = MyThreadClass("Thread#4 ", randint(1,10))
thread5 = MyThreadClass("Thread#5 ", randint(1,10))
thread6 = MyThreadClass("Thread#6 ", randint(1,10))
thread7 = MyThreadClass("Thread#7 ", randint(1,10))
thread8 = MyThreadClass("Thread#8 ", randint(1,10))
thread9 = MyThreadClass("Thread#9 ", randint(1,10))

# Thread Running
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread8.start()
thread9.start()

# Thread joining
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
thread6.join()
thread7.join()
thread8.join()
thread9.join()

# End
print("End")

#Execution Time
print("--- %s seconds ---" % (time.time() - start_time))


if __name__ == "__main__":
main()




75 changes: 75 additions & 0 deletions QUIS SISTER 1/quis_fadhelrahmawan/MyThreadClass_lock_2.py
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 + \
" running, belonging to process ID "\
+ 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
thread1 = MyThreadClass("Thread#1 ", randint(1,10))
thread2 = MyThreadClass("Thread#2 ", randint(1,10))
thread3 = MyThreadClass("Thread#3 ", randint(1,10))
thread4 = MyThreadClass("Thread#4 ", randint(1,10))
thread5 = MyThreadClass("Thread#5 ", randint(1,10))
thread6 = MyThreadClass("Thread#6 ", randint(1,10))
thread7 = MyThreadClass("Thread#7 ", randint(1,10))
thread8 = MyThreadClass("Thread#8 ", randint(1,10))
thread9 = MyThreadClass("Thread#9 ", randint(1,10))

# Thread Running
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread8.start()
thread9.start()

# Thread joining
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
thread6.join()
thread7.join()
thread8.join()
thread9.join()

# End
print("End")

#Execution Time
print("--- %s seconds ---" % (time.time() - start_time))


if __name__ == "__main__":
main()




27 changes: 27 additions & 0 deletions QUIS SISTER 1/quis_fadhelrahmawan/Rlock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# program to illustrate the use of RLocks

# importing the module
from csv import list_dialects
import threading

# initializing the shared resource
Lidiya = 18

# creating an RLock object instead
# of Lock object
lock = threading.RLock()

# the below thread is trying to access
# the shared resource
lock.acquire()
Lidiya = Lidiya + 19

# the below thread is trying to access
# the shared resource
lock.acquire()
Lidiya = Lidiya - 24
lock.release()
lock.release()

# displaying the value of shared resource
print(Lidiya)
Loading