Skip to content
Open

quiz #17

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
34 changes: 34 additions & 0 deletions QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Barrier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from random import randrange
from threading import Barrier, Thread
from time import ctime, sleep

minuman = ['thai tea', 'mixue', 'janjiw', 'boba', 'josu']
finish_line = Barrier(len(minuman))
result = []

def runner():
name = minuman.pop()
sleep(randrange(2, 10))
result.append(name)
print('%s Sudah siap: %s \n' % (name, ctime()))
finish_line.wait()


def finisher(result):
for idx, i in enumerate(result):
print(str(idx+1) + " Selamat menikmati "+ i)


def main():
threads = []
print('Memulai membikin minuman')
for i in range(len(minuman)):
threads.append(Thread(target=runner))
threads[-1].start()
for thread in threads:
thread.join()
print('Minuman Selesai\n')
print(finisher(result))

if __name__ == "__main__":
main()
67 changes: 67 additions & 0 deletions QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 Outbound(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def pick(self):
with condition:
if len(items) == 0:
logging.info('Baju kosong. Tidak ada Baju yang di ambil')
condition.wait()

items.pop()
logging.info('Mengambil 1 Baju')
logging.info('Sisa Baju dalam lemari {}'.format(len(items)))

condition.notify()

def run(self):
for i in range(11):
time.sleep(2)
self.pick()


class Inbound(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def store(self):
with condition:
if len(items) == 10:
logging.info('Total Baju dalam lemari {}. lemari penuh'.format(len(items)))
condition.wait()

items.append(1)
logging.info('Memasukan Baju pada lemari {}'.format(len(items)))

condition.notify()

def run(self):
for i in range(10):
time.sleep(0.5)
self.store()


def main():
inbound = Inbound(name='Inbound')
outbound = Outbound(name='Outbound')

inbound.start()
outbound.start()

inbound.join()
outbound.join()


if __name__ == "__main__":
main()
55 changes: 55 additions & 0 deletions QUIS SISTER 1/1194017_Haekal Hilmi Zain_D4 TI - 3A/Event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 Service(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.name = "Waiter"


def run(self):
while True:
time.sleep(2)
event.wait()
item = items.pop()
logging.info('Mengambil pesanan untuk meja No. {} dan sedang di antarkan oleh {} ke meja'.format(item, self.name))


class Chef(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.name = "Chef Arnold"


def run(self):
for i in range(5):
time.sleep(2)
item = random.randint(0, 100)
items.append(item)
logging.info('Chef membuat makanan untuk meja No. {} di buat oleh {}'.format(item, self.name))
event.set()
event.clear()


if __name__ == "__main__":
t1 = Chef()
t2 = Service()


t1.start()
t2.start()


t1.join()
t2.join()
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 sum_pid_duration(self, pid):
total = 0
for i in range(self.duration):
total += (i+1) * pid

return total

def run(self):
print("---> " + self.name + " running, MemProses ID = " + str(os.getpid()) + "\n")
time.sleep(self.duration)
print("---> Total perhitungan Proses ID * Durasi = " + str(os.getpid()) + " * " + str(self.duration) + " = " + str(self.sum_pid_duration(os.getpid())))
print("---> " + self.name + " Selesai\n")


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

# Thread Creation
thread1 = MyThreadClass("Nomor Proses ID #1 ", randint(1,10))
thread2 = MyThreadClass("Nomor Proses ID #2 ", randint(1,10))
thread3 = MyThreadClass("Nomor Proses ID #3 ", randint(1,10))
thread4 = MyThreadClass("Nomor Proses ID #4 ", randint(1,10))
thread5 = MyThreadClass("Nomor Proses ID #5 ", randint(1,10))
thread6 = MyThreadClass("Nomor Proses ID #6 ", randint(1,10))
thread7 = MyThreadClass("Nomor Proses ID #7 ", randint(1,10))
thread8 = MyThreadClass("Nomor Proses ID #8 ", randint(1,10))
thread9 = MyThreadClass("Nomor Proses ID #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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 sum_pid_duration(self, pid):
total = 0
for i in range(self.duration):
total += (i+1) * pid

return total

def run(self):
#Acquire the Lock
threadLock.acquire()
print("---> " + self.name + " running, MemProses ID = " + str(os.getpid()) + "\n")
time.sleep(self.duration)
print("---> Total perhitungan Proses ID * Durasi = " + str(os.getpid()) + " * " + str(self.duration) + " = " + str(self.sum_pid_duration(os.getpid())))
print("---> " + self.name + " Selesai\n")
#Release the Lock
threadLock.release()


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

# Thread Creation
thread1 = MyThreadClass("Nomor Proses ID #1 ", randint(1,10))
thread2 = MyThreadClass("Nomor Proses ID #2 ", randint(1,10))
thread3 = MyThreadClass("Nomor Proses ID #3 ", randint(1,10))
thread4 = MyThreadClass("Nomor Proses ID #4 ", randint(1,10))
thread5 = MyThreadClass("Nomor Proses ID #5 ", randint(1,10))
thread6 = MyThreadClass("Nomor Proses ID #6 ", randint(1,10))
thread7 = MyThreadClass("Nomor Proses ID #7 ", randint(1,10))
thread8 = MyThreadClass("Nomor Proses ID #8 ", randint(1,10))
thread9 = MyThreadClass("Nomor Proses ID #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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 sum_pid_duration(self, pid):
total = 0
for i in range(self.duration):
total += (i+1) * pid

return total

def run(self):
#Acquire the Lock
threadLock.acquire()
print("---> " + self.name + " running, Sedang MemProses ID = " + str(os.getpid()) + "\n")
threadLock.release()
time.sleep(self.duration)
print("---> Total perhitungan Proses ID * Durasi = " + str(os.getpid()) + " * " + str(self.duration) + " = " + str(self.sum_pid_duration(os.getpid())))
print("---> " + self.name + " Selesai\n")
#Release the Lock


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

# Thread Creation
thread1 = MyThreadClass("Nomor Proses ID #1 ", randint(1,10))
thread2 = MyThreadClass("Nomor Proses ID #2 ", randint(1,10))
thread3 = MyThreadClass("Nomor Proses ID #3 ", randint(1,10))
thread4 = MyThreadClass("Nomor Proses ID #4 ", randint(1,10))
thread5 = MyThreadClass("Nomor Proses ID #5 ", randint(1,10))
thread6 = MyThreadClass("Nomor Proses ID #6 ", randint(1,10))
thread7 = MyThreadClass("Nomor Proses ID #7 ", randint(1,10))
thread8 = MyThreadClass("Nomor Proses ID #8 ", randint(1,10))
thread9 = MyThreadClass("Nomor Proses ID #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()
Loading