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
31 changes: 31 additions & 0 deletions QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Barrier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


from random import randrange
from threading import Barrier, Thread
from time import ctime, sleep

jumlah_menu = 4
hasilnya = Barrier(jumlah_menu)
menunya = ['BAKSO', 'BATAGOR', 'SIOMAY', 'MIE AYAM', 'KUE BERAS', 'SEBLAK']



def menu():
cobain = menunya.pop()
sleep(randrange(2, 3 ))
print('%s JUGA TERMASUK DALAM MENU \n' % (cobain))
hasilnya.wait()


def main():
threads = []
print('Yang ada disini')
for i in range(jumlah_menu):
threads.append(Thread(target=menu))
threads[-1].start()
for thread in threads:
thread.join()
print('okeh beres')

if __name__ == "__main__":
main()
70 changes: 70 additions & 0 deletions QUIS SISTER 1/Chapter 2 wahyu kurnia sari/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()

# yg ga boleh diganti def __init__
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 tepung')
condition.wait()

items.pop()
logging.info('tepung 1 ')

condition.notify()

def run(self):
for i in range(3):
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) == 3:
logging.info('jenis tepung {}. selesai'.format(len(items)))
condition.wait()

items.append(1)
logging.info('total {}'.format(len(items)))

condition.notify()

def run(self):
for i in range(3):
time.sleep(0.2)
self.produce()


def main():
producer = Producer(name='tepung')
consumer = Consumer(name='kue')

producer.start()
consumer.start()

producer.join()
consumer.join()


if __name__ == "__main__":
main()
41 changes: 41 additions & 0 deletions QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import threading
import time
import logging

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


def jalan(e):
logging.debug('Kamu lapar tidak')
event_is_set = e.wait()
logging.debug('Hari ini makan apa?: %s', event_is_set)


def pergi(e, t):
while not e.isSet():
logging.debug('Aku lapar sekali nih, makan sate yok')
event_is_set = e.wait(t)
logging.debug('Sate Apa: %s', event_is_set)
if event_is_set:
logging.debug('Sate Maranggi di warung Hj. Maya pasteur')
else:
logging.debug('Hayuk?')


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

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

logging.debug('Waiting before calling Event.set()')
time.sleep(3)
e.set()
logging.debug('Allhamdulillah kenyang')
56 changes: 56 additions & 0 deletions QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 + \
" Total Stok Smartphone tahun 2022 di BEC yaitu, " \
+ str(os.getpid()) + "\n")
time.sleep(self.duration)
print("---> " + self.name + " Update Terkini Stok habis\n")


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

thread1 = MyThreadClass("Tersedia Merk Samsung ", randint(1, 10))
thread2 = MyThreadClass("Tersedia Merk Iphone ", randint(1, 10))
thread3 = MyThreadClass("Tersedia Merk Redmi ", randint(1, 10))
thread4 = MyThreadClass("Tersedia Merk Oppo ", randint(1, 10))
thread5 = MyThreadClass("Tersedia Merk Xiomi ", randint(1, 10))

thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()


# Thread joining
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()

# End
print("Tunggu sampai barang tersedia kembali")

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


if __name__ == "__main__":
main()




76 changes: 76 additions & 0 deletions QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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 + \
" Jumlah stock motor di indonesia berdasarkan merk" \
+ str(os.getpid()) + "\n")
time.sleep(self.duration)
print("---> " + self.name + " matic\n")
# Release the Lock
threadLock.release()


def main():
start_time = time.time()
# Thread Creation
thread1 = MyThreadClass("Vario#1 ", randint(1, 10))
thread2 = MyThreadClass("Beat#2 ", randint(1, 10))
thread3 = MyThreadClass("PCX#3 ", randint(1, 10))
thread4 = MyThreadClass("Piaggio#4 ", randint(1, 10))
thread5 = MyThreadClass("Mio#5 ", randint(1, 10))
thread6 = MyThreadClass("Nmax#6 ", randint(1, 10))
thread7 = MyThreadClass("Lexi#7 ", randint(1, 10))
thread8 = MyThreadClass("Aerox#8 ", randint(1, 10))
thread9 = MyThreadClass("ADV#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("Motor Matic Siap di Distribusikan")

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


if __name__ == "__main__":
main()




77 changes: 77 additions & 0 deletions QUIS SISTER 1/Chapter 2 wahyu kurnia sari/MyThreadClass_lock_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 + \
" Pendistribusian motor matic sesuai di regional Jawa Barat " \
+ str(os.getpid()) + "\n")
threadLock.release()
time.sleep(self.duration)
print("---> " + self.name + " Pendistribusian di regional III\n")
# Release the Lock


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

# Thread Creation
thread1 = MyThreadClass("Bandung#1 ", randint(1, 8))
thread2 = MyThreadClass("Subang#2 ", randint(1, 8))
thread3 = MyThreadClass("Bekasi#3 ", randint(1, 8))
thread4 = MyThreadClass("Bogor#4 ", randint(1, 8))
thread5 = MyThreadClass("Tangerang#5 ", randint(1, 8))
thread6 = MyThreadClass("Tasikmalaya#6 ", randint(1, 8))
thread7 = MyThreadClass("Sumedang#7 ", randint(1, 8))
thread8 = MyThreadClass("Cianjur#8 ", randint(1, 8))
thread9 = MyThreadClass("Ciamis#9 ", randint(1, 8))

# 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()




26 changes: 26 additions & 0 deletions QUIS SISTER 1/Chapter 2 wahyu kurnia sari/Rlock.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
baksobakar = 0

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

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

# the below thread is trying to access
# the shared resource
lock.acquire()
baksobakar = baksobakar + 32
lock.release()
lock.release()

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