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
1 change: 1 addition & 0 deletions Python-Pararel_SISTER
Submodule Python-Pararel_SISTER added at 5d0a2d
28 changes: 28 additions & 0 deletions QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Barrier_Quis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


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

jumlah_running = 5
selesai = Barrier(jumlah_running)
mobil = ['Honda', 'Toyota', 'Daihatsu', 'Hyundai', 'Suzuki', 'Mitsubishi', 'Nissan']

def jenis():
brand = mobil.pop()
sleep(randrange(2, 5))
print('%s Merupakan brand mobil \n' % (brand))
selesai.wait()

def main():
threads = []
print('Jenis Brand Mobil!')
for i in range(jumlah_running):
threads.append(Thread(target=jenis))
threads[-1].start()
for thread in threads:
thread.join()
print('Udah Dulu Ya!')

if __name__ == "__main__":
main()
50 changes: 50 additions & 0 deletions QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Condition_Quis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#import library
import threading
import time
import random


condition = threading.Condition() #membuat variabel condition untuk menampung fitur condition pada threading

#membuat class
class Ngambil():
#membuat fungsi

def kuning(self):
print('Hati-Hati')

with condition:
print('Lampu Kuning')
condition.wait()
print('Tetap Waspada di jalan')

def lalin(self):
print('Berhenti')

with condition:
print('Lampu Merah')
condition.wait()
print('Udah hijau, jalan')

def jalan(self):
print('nunggu lajur yang lain')

with condition:
time = random.randint(25,30)
waktu = random.randint(5,20)
print('{} detik lagi'.format(time))
print('{} detik lagi'.format(waktu))
condition.notify()

ambil = Ngambil()
kuning = threading.Thread(target=ambil.kuning)
lalin = threading.Thread(target=ambil.lalin)
jalan = threading.Thread(target=ambil.jalan)

kuning.start()
lalin.start()
jalan.start()

kuning.join()
lalin.join()
jalan.join()
20 changes: 20 additions & 0 deletions QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Event_Quis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
import threading
import time

logging.basicConfig(level=logging.DEBUG,
format='[%(levelname)s] (%(threadName)-10s) %(message)s',)

def worker(i,dt,e):
tStart=time.time()
e.wait(dt)
logging.debug('{0} tried to wait {1} seconds but really waited {2}'.format(i,dt, time.time()-tStart ))


e = threading.Event()

maxThreads=10
for i in range(maxThreads):
dt=1+i # (s)
w = threading.Thread(target=worker, args=(i,dt,e))
w.start()
35 changes: 35 additions & 0 deletions QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/MyThreadClass_Quis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/python

import threading
import time

exitFlag = 0

class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name)
print_time(self.name, 5, self.counter)
print ("Exiting " + self.name)

def print_time(threadName, counter, delay):
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1

# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)

# Start new Threads
thread1.start()
thread2.start()

print ("Exiting Main Thread")
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/python

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

exitFlag = 0
lock = threading.Lock()
bounded_semaphore = threading.BoundedSemaphore(100) #penggunaan semaphore

class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name)
print_time(self.name, 5, self.counter)
print ("Exiting " + self.name)

#membuat fungsi
def f1():
bounded_semaphore.acquire()
print("%s acquired lock." % (threading.current_thread().name))
print(bounded_semaphore._value)
bounded_semaphore.release()
print("%s released lock." % (threading.current_thread().name))
print(bounded_semaphore._value)


def print_time(threadName, counter, delay):
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1

# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)

# Start new Threads
thread1.start()
thread2.start()

t1 = threading.Thread(target=f1)
t2 = threading.Thread(target=f1)
t3 = threading.Thread(target=f1)
t4 = threading.Thread(target=f1)
t5 = threading.Thread(target=f1)
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()

print("Main Thread Exited.", threading.main_thread())

print ("Exiting Main Thread")
67 changes: 67 additions & 0 deletions QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Rlock_Quis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import threading
import time
import random

noabsen = int(input('Masukan No Absen Anda : '))

if noabsen > 100 :
print("Anda Anggota Dapartemen Manifest")
elif noabsen > 1 < 80 :
print("Anda Anggota Dapartemen Beacukai")
else:
print("Anda Pegawai Operation")

class Container:
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 getin(container, items):
print("Barang {} Telah Masuk \n".format(items))
while items:
container.add()
time.sleep(1)
items -= 1
print("Menambahkan Satu Barang -->{} Barang Ditambahkan \n".format(items))



def getout(container, items):
print("Barang {} Telah Keluar \n".format(items))
while items:
container.remove()
time.sleep(1)
items -= 1
print("Mengeluarkan Satu Barang -->{} Barang Dikeluarkan \n".format(items))


def main():
items = 10
container = Container()

t1 = threading.Thread(target=getin, \
args=(container, random.randint(10,20)))
t2 = threading.Thread(target=getout, \
args=(container, random.randint(1,10)))

t1.start()
t2.start()


t1.join()
t2.join()

if __name__ == "__main__":
main()
26 changes: 26 additions & 0 deletions QUIS SISTER 1/1184070 - YOLA VEGITA - QUIS/Semaphore_Quis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#imprt library
import threading

bounded_semaphore = threading.BoundedSemaphore(100) #penggunaan semaphore

#membuat fungsi
def f1():
bounded_semaphore.acquire()
print("%s acquired lock." % (threading.current_thread().name))
print(bounded_semaphore._value)
bounded_semaphore.release()
print("%s released lock." % (threading.current_thread().name))
print(bounded_semaphore._value)

t1 = threading.Thread(target=f1)
t2 = threading.Thread(target=f1)
t3 = threading.Thread(target=f1)
t4 = threading.Thread(target=f1)
t5 = threading.Thread(target=f1)
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()

print("Main Thread Exited.", threading.main_thread())
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from random import randrange
import threading
from time import ctime, sleep

hobby = ['Singing','Cooking','Swiming','Drawing','Play Game']

def hobbies(thread_number):
mine = hobby.pop()
sleep(randrange(2, 5))
print('My Hobbies List {}'.format(thread_number))
return print('%s Merupakan Hobby Saya \n' % (mine))


def main():
threads = []
for i in range(5):
t = threading.Thread(target=hobbies, args=(i,))
threads.append(t)
t.start()
t.join()

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import threading
import time


def merah():
print ('Lampu '+ str(threading.currentThread().getName())+( '--> Berhenti \n'))
return

def kuning():
print ('Lampu '+ str(threading.currentThread().getName())+( '--> Bersiap \n'))
return

def hijau():
time.sleep(2) #waktu jeda
print ('Lampu '+str(threading.currentThread().getName())+( '--> Jalan \n'))
return


t1 = threading.Thread(name='Merah', target=merah)
t2 = threading.Thread(name='kuning', target=kuning)
t3 = threading.Thread(name='hijau',target=hijau)

t1.start()
t2.start()
t3.start()

t1.join()
t2.join()
t3.join()
Loading