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
29 changes: 29 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/Barrier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Tugas Studi Kasus
import threading
import time

def start_checkout():
print("starting checkout the payment...")
time.sleep(2)

def payment(b):
start_checkout()
b.wait()
print("Successfully payment")

def confirmation(b):
print("waiting for payment getting ready...")
b.wait()
print("sending email confirmation...")

if __name__=='__main__':

b = threading.Barrier(2, timeout=5)
payment = threading.Thread(target=payment, args=(b,))
payment.start()
confirmation = threading.Thread(target=confirmation, args=(b,))
confirmation.start()

payment.join()
confirmation.join()
print("Done")
40 changes: 40 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/Condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Tugas Studi Kasus
import logging
import threading
import random

logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-9s) %(message)s',)

condition = threading.Condition()

class Mengantre():

def pengunjung(self):
logging.debug('pengunjung mulai mengantre...')

with condition:
logging.debug('pengunjung menunggu giliran...')
condition.wait()
logging.debug('sukses mendapat giliran')

def loket(self):
logging.debug('loket mulai dibuka...')

with condition:
logging.debug('loket memanggil nomor antrean...')
na=0
na=random.randint(1,13)
print('cek nomor antrean...')
print('memanggil nomor antrean ke {}'.format(na))
condition.notify()

antre = Mengantre()
pengunjung = threading.Thread(name='Pengunjung', target=antre.pengunjung)
loket = threading.Thread(name='loket', target=antre.loket)

pengunjung.start()
loket.start()

pengunjung.join()
loket.join()
34 changes: 34 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/Event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Tugas Studi Kasus
import threading
import time
import logging

logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-9s) %(message)s',)

def wait_for_event(e):
logging.debug('Menunggu film dimulai...')
event_is_set = e.wait()
logging.debug('Film dimulai: %s', event_is_set)

def wait_for_event_timeout(e, t):
while not e.isSet():
logging.debug('Menunggu Film Selesai')
event_is_set = e.wait(t)
logging.debug('Film dimulai: %s', event_is_set)
if event_is_set:
logging.debug('Memproses Film')
else:
logging.debug('Iklan')

if __name__ == '__main__':
e = threading.Event()
t1 = threading.Thread(name='blocking', target=wait_for_event,args=(e,))
t2 = threading.Thread(name='non-blocking', target=wait_for_event_timeout, args=(e, 2))
t1.start()
t2.start()

logging.debug('Menunggu sebelum film dimulai')
time.sleep(3)
e.set()
logging.debug('Film dimulai')
30 changes: 30 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Tugas Studi Kasus
import time
import os
from random import randint
from threading import Thread

class MyThread(Thread):
def __init__(self, i, duration):
Thread.__init__(self)
self.x = i
self.duration = duration

def run(self):
print("Value stored is: ", self.x)
time.sleep(self.duration)
print("Exiting thread with value: ", self.x)


thread1 = MyThread(1, randint(1,10))
thread2 = MyThread(2, randint(1,10))
thread3 = MyThread(3, randint(1,10))

thread1.start()
thread2.start()
thread3.start()

thread1.join()
thread2.join()
thread3.join()

39 changes: 39 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Tugas Studi Kasus

import threading
import time
from threading import Thread
from random import randint

# Lock Definition
threadLock = threading.Lock()

class MyThread(Thread):
def __init__(self, i, duration):
Thread.__init__(self)
self.x = i
self.duration = duration

def run(self):
# Acquire the Lock
threadLock.acquire()

print("Value stored is: ", self.x)
time.sleep(self.duration)
print("Exiting thread with value: ", self.x)

# Release the Lock
threadLock.release()


thread1 = MyThread(1, randint(1,10))
thread2 = MyThread(2, randint(1,10))
thread3 = MyThread(3, randint(1,10))

thread1.start()
thread2.start()
thread3.start()

thread1.join()
thread2.join()
thread3.join()
44 changes: 44 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/MyThreadClass_lock_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Tugas Studi Kasus

import threading
import time
import os
from threading import Thread
from random import randint

# Lock Definition
threadLock = threading.Lock()

class MyThread(Thread):
# overriding constructor
def __init__(self, i, duration):
# calling parent class constructor
Thread.__init__(self)
self.x = i
self.duration = duration

# define your own run method
def run(self):
# Acquire the Lock
threadLock.acquire()

print("Value stored is: ", self.x)

# Release the Lock
threadLock.release()
time.sleep(self.duration)
print("Exiting thread with value: ", self.x)


thread1 = MyThread(1, randint(1,10))
thread2 = MyThread(2, randint(1,10))
thread3 = MyThread(3, randint(1,10))

thread1.start()
thread2.start()
thread3.start()

thread1.join()
thread2.join()
thread3.join()

61 changes: 61 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/Rlock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Tugas Studi Kasus
import threading
import time
import random


class Box:
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 adder(box, items):
print("{} Barang yang ada di box \n".format(items))
while items:
box.add()
time.sleep(1)
items -= 1
print("Tambah barang ke box sebanyak -->{} \n".format(items))



def remover(box, items):
print("{} Barang di box yang terjual \n".format(items))
while items:
box.remove()
time.sleep(1)
items -= 1
print("Barang yang terjual sebanyak -->{} \n".format(items))


def main():
items = 10
box = Box()

t1 = threading.Thread(target=adder, \
args=(box, random.randint(10,20)))
t2 = threading.Thread(target=remover, \
args=(box, random.randint(1,10)))

t1.start()
t2.start()


t1.join()
t2.join()


if __name__ == "__main__":
main()
43 changes: 43 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/Semaphore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Tugas Studi Kasus

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)


semaphore = threading.Semaphore(0)
item = 0


def pemenangundian():
logging.info('Menunggu Kereta datang...')
semaphore.acquire()
logging.info('Kereta {} Sudah Berangkat'.format(item))


def nomorundian():
global item
time.sleep(3)
item = random.randint(1, 10)
logging.info('Kereta {} Sudah Datang '.format(item))
semaphore.release()


def main():
for i in range(1):
t1 = threading.Thread(target=pemenangundian)
t2 = threading.Thread(target=nomorundian)

t1.start()
t2.start()

t1.join()
t2.join()


if __name__ == "__main__":
main()
13 changes: 13 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/Thread_definition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tugas Studi Kasus
import time
from threading import Thread

def Proses(i):
print("Thread %i memproses dalam 5 detik." % i)
time.sleep(5)
print("Thread %i selesai di proses." % i)

for i in range(10):
th = Thread(target=Proses, args=(i, ))
th.start()
th.join()
29 changes: 29 additions & 0 deletions QUIS SISTER 1/Eriskiannisa Febrianty/Thread_determine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#Tugas Studi Kasus
import threading
import time

def thread_pertama(i):
time.sleep(5)
print('Nilai dari '+ str(threading.current_thread().getName())+" adalah: ", i)

def thread_kedua(i):
time.sleep(6)
print('Nilai dari '+ str(threading.current_thread().getName())+" adalah: ", i)

def thread_ketiga(i):
time.sleep(7)
print('Nilai dari '+ str(threading.current_thread().getName())+" adalah: ", i)


thread1 = threading.Thread(target=thread_pertama, args=(10,)) # nama default
thread2 = threading.Thread(name='Thread Kedua', target=thread_kedua, args=(20,))
thread3 = threading.Thread(name='Thread Ketiga', target=thread_ketiga, args=(30,))

# Running the threads
thread1.start()
thread2.start()
thread3.start()

thread1.join()
thread2.join()
thread3.join()
Loading