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/1194008_Burhanudin Zuhri/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

jml_turis = 3
destinasi = Barrier(jml_turis)
nama_turis = ['Andi', 'Budi', 'Caca']

def wisata():
name = nama_turis.pop()
sleep(randrange(2, 5))
print('%s Berangkat sekolah pada hari: %s ' % (name, ctime()))
destinasi.wait()

def main():
threads = []
print('Perjalanan Dimulai')
for i in range(jml_turis):
threads.append(Thread(target=wisata))
threads[-1].start()
for thread in threads:
thread.join()
print('Semuanya sampai di sekolah tepat waktu')

if __name__ == "__main__":
main()
70 changes: 70 additions & 0 deletions QUIS SISTER 1/1194008_Burhanudin Zuhri/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 Pembeli(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def consume(self):

with condition:

if len(items) == 0:
logging.info('Tidak ada sepeda yang dapat dibeli')
condition.wait()

items.pop()
logging.info('Anda membeli 1 buah sepeda')

condition.notify()

def run(self):
for i in range(20):
time.sleep(2)
self.consume()


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

def produce(self):

with condition:

if len(items) == 10:
logging.info('Pada toko terdapat {} buah sepeda. Produksi berhenti untuk sementara'.format(len(items)))
condition.wait()

items.append(1)
logging.info('Terdapat {} buah sepeda saat ini'.format(len(items)))

condition.notify()

def run(self):
for i in range(20):
time.sleep(0.5)
self.produce()


def main():
penjual = Penjual(name='Penjual')
pembeli = Pembeli(name='Pembeli')

penjual.start()
pembeli.start()

penjual.join()
pembeli.join()


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

def run(self):
while True:
time.sleep(2)
event.wait()
item = items.pop()
logging.info('Supplier memberitahu bahwa: item {} telah diambil oleh {}'\
.format(item, self.name))

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

def run(self):
for i in range(5):
time.sleep(2)
item = random.randint(0, 100)
items.append(item)
logging.info('Vendor memberitahu bahwa: item {} telah ditambahkan oleh {}'\
.format(item, self.name))
event.set()
event.clear()

if __name__ == "__main__":
t1 = Vendor()
t2 = Supplier()

t1.start()
t2.start()

t1.join()
t2.join()
63 changes: 63 additions & 0 deletions QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import time
import os
from random import randint
from threading import Thread

class Mahasiswa (Thread):
def __init__(self, name, duration):
Thread.__init__(self)
self.name = name
self.duration = duration
def run(self):
print ("---> " + self.name + \
" Sedang mengerjakan ujian dengan nomor ujian "\
+ str(os.getpid()) + "\n")
time.sleep(self.duration)
print ("---> " + self.name + " pengerjaan ujian telah berakhir\n")


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

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

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


if __name__ == "__main__":
main()
56 changes: 56 additions & 0 deletions QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import threading
import time
import os
from threading import Thread
from random import randint

# Lock Definition
threadLock = threading.Lock()

class Absen(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 + \
" dengan absen "\
+ str(os.getpid()) + "\n")
time.sleep(self.duration)
print ("---> " + self.name + " selesai absen\n")
#Release the Lock
threadLock.release()


def main():
start_time = time.time()
# Thread Creation
thread1 = Absen("Siswa", randint(1,10))
thread2 = Absen("Guru", randint(1,10))



# Thread Running
thread1.start()
thread2.start()



# Thread joining
thread1.join()
thread2.join()



# End
print("Jagalah ketertiban")

#Execution Time
print("Waktu yang dibutuhkan untuk mengabsen siswa dan guru yaitu %s detik" % (time.time() - start_time))


if __name__ == "__main__":
main()

26 changes: 26 additions & 0 deletions QUIS SISTER 1/1194008_Burhanudin Zuhri/MyThreadClass_lock_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import threading
import time

kendaraan = threading.Lock()

def Mobil(kendaraan):
kendaraan.acquire()
print('Mobil sedang melewati terowongan')
time.sleep(1)
print('Mobil sudah melewati terowongan')

def Motor(kendaraan):
print('Motor sedang melewati terowongan')
kendaraan.release()
time.sleep(1)
print('Motor sudah melewati terowongan')


t1 = threading.Thread(target=Mobil, args=(kendaraan, ))
t2 = threading.Thread(target=Motor, args=(kendaraan, ))

t1.start()
t2.start()

t1.join()
t2.join()
60 changes: 60 additions & 0 deletions QUIS SISTER 1/1194008_Burhanudin Zuhri/Rlock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import threading
import time
import random


class Ricecooker:
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(kotak_nasi, items):
print("Banyaknya nasi {} kg yang ditambahkan \n".format(items))
while items:
kotak_nasi.add()
time.sleep(1)
items -= 1
print("Sisa nasi saat ini -->{} kg \n".format(items))



def remover(kotak_nasi, items):
print("Banyaknya nasi {} kg yang dimakan \n".format(items))
while items:
kotak_nasi.remove()
time.sleep(1)
items -= 1
print("Nasi yang dimakan -->{} kg \n".format(items))


def main():
items = 10
kotak_nasi = Ricecooker()

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

t1.start()
t2.start()


t1.join()
t2.join()


if __name__ == "__main__":
main()
39 changes: 39 additions & 0 deletions QUIS SISTER 1/1194008_Burhanudin Zuhri/Semaphore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 Penulis():
logging.info('Permintaan dibuat')
semaphore.acquire()
logging.info('Penulis menulis surat nomor {}'.format(item))


def Pengirim():
global item
time.sleep(2)
item = random.randint(0, 1000)
logging.info('Pengirim merespon permintaan nomor {}'.format(item))
semaphore.release()


def main():
for i in range(10):
t1 = threading.Thread(target=Penulis)
t2 = threading.Thread(target=Pengirim)

t1.start()
t2.start()

t1.join()
t2.join()


if __name__ == "__main__":
main()
Loading