diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..adb0256
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,149 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+.idea/
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+#lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+pip-wheel-metadata/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+# Usually these files are written by a python script from a template
+# before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+.python-version
+
+# pipenv
+# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+# However, in case of collaboration, if having platform-specific dependencies or dependencies
+# having no cross-platform support, pipenv may install dependencies that don't work, or not
+# install all needed dependencies.
+#Pipfile.lock
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# Croot
+*.croot
+ani.croot
+anu.croot
+ano.croot
+nilai.html
+nilai
+nilai.txt
+value
+value.txt
+value
+value.pdf
+newravi.docx
+movie_2020_0
+movie_2020_1
+
+luffy.txt
+ravi.txt
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..d564bde
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,10 @@
+sudo: enabled
+language: python
+script:
+ - pytest -v -s --cov=Chapter02 --cov-report term-missing
+before_script:
+ - echo "Menjalankan Testing"
+install:
+ - pip install -r requirements.txt
+before_install:
+ - git status
\ No newline at end of file
diff --git a/Chapter01/__init__.py b/Chapter01/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/Chapter01/do_something.py b/Chapter01/do_something.py
index db4c9f6..41f4880 100644
--- a/Chapter01/do_something.py
+++ b/Chapter01/do_something.py
@@ -2,5 +2,4 @@
def do_something(count,out_list):
for i in range(count):
- out_list.append(random.random())
-
+ out_list.append(random.random())
\ No newline at end of file
diff --git a/Chapter01/multiprocessing_test.py b/Chapter01/multiprocessing_test.py
index 2480586..27dcad5 100644
--- a/Chapter01/multiprocessing_test.py
+++ b/Chapter01/multiprocessing_test.py
@@ -1,9 +1,9 @@
-from do_something import *
+from Chapter01.do_something import *
import time
import multiprocessing
-if __name__ == "__main__":
+def multiprocessing_test():
start_time = time.time()
size = 10000000
procs = 10
@@ -23,3 +23,4 @@
print ("List processing complete.")
end_time = time.time()
print("multiprocesses time=", end_time - start_time)
+ return True
diff --git a/Chapter01/multithreading_test.py b/Chapter01/multithreading_test.py
index 055abdd..f827350 100644
--- a/Chapter01/multithreading_test.py
+++ b/Chapter01/multithreading_test.py
@@ -1,8 +1,8 @@
-from do_something import *
+from Chapter01.do_something import *
import time
import threading
-if __name__ == "__main__":
+def multithreading_test():
start_time = time.time()
size = 10000000
threads = 10
@@ -21,4 +21,5 @@
print ("List processing complete.")
end_time = time.time()
print("multithreading time=", end_time - start_time)
+ return True
diff --git a/Chapter01/serial_test.py b/Chapter01/serial_test.py
index a7f4d31..c9c99c9 100644
--- a/Chapter01/serial_test.py
+++ b/Chapter01/serial_test.py
@@ -1,7 +1,9 @@
import time
-from do_something import *
+from Chapter01.do_something import *
-if __name__ == "__main__":
+
+
+def serial_test():
start_time = time.time()
size = 10000000
n_exec = 10
@@ -13,3 +15,4 @@
print ("List processing complete.")
end_time = time.time()
print("serial time=", end_time - start_time)
+ return True
diff --git a/Chapter02/Alif1184068.py b/Chapter02/Alif1184068.py
new file mode 100644
index 0000000..c7f9877
--- /dev/null
+++ b/Chapter02/Alif1184068.py
@@ -0,0 +1,71 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class Alif1184068 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ awal = 1
+ akhir = 30000
+ self.bil_geometri(awal,akhir)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def bil_geometri(self,awal,akhir):
+ for n in range(1,30000):
+ suku = 1*(2**(n-1))
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Alif1184068("Thread#1 ", 1,randint(1,10))
+ thread2 = Alif1184068("Thread#2 ", 2,randint(1,10))
+ thread3 = Alif1184068("Thread#3 ", 3,randint(1,10))
+ thread4 = Alif1184068("Thread#4 ", 4,randint(1,10))
+ thread5 = Alif1184068("Thread#5 ", 5,randint(1,10))
+ thread6 = Alif1184068("Thread#6 ", 6,randint(1,10))
+ thread7 = Alif1184068("Thread#7 ", 7,randint(1,10))
+ thread8 = Alif1184068("Thread#8 ", 8,randint(1,10))
+ thread9 = Alif1184068("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/AlifEnam1184068.py b/Chapter02/AlifEnam1184068.py
new file mode 100644
index 0000000..fd54328
--- /dev/null
+++ b/Chapter02/AlifEnam1184068.py
@@ -0,0 +1,57 @@
+from threading import Barrier, Thread
+import os
+import requests
+from time import ctime, sleep
+
+angka = 1
+b = Barrier(angka)
+file = "alif"
+file2 = "alifbaru"
+filename = os.path.join(os.path.dirname(__file__), file)
+filename2 = os.path.join(os.path.dirname(__file__), file2)
+
+def getapi():
+ apiurl='http://www.emsifa.com/api-wilayah-indonesia/api/provinces.json'
+ response = requests.get(apiurl)
+ html=response.json()
+ handlingfile(html)
+
+def handlingfile(isi):
+ print('Yoo filenya dibuat dulu ya : '+file+'.txt \n')
+ f = open(filename+".txt", "w")
+ print('Filenya udah dibuat nih \n')
+ f.write(str(isi))
+ f.close()
+ print('Isi filenya apaan yaa: '+file+'.txt')
+ x = open(filename+".txt", "r")
+ print(x.read()+'\n')
+ x.close()
+ print('coba ganti nama filenya dong \n')
+ print('Rename File menjadi: '+file2+'.txt \n')
+ os.rename(filename+'.txt', filename2+'.txt')
+ sleep(2)
+ b.wait()
+ print('File:'+file+'.txt'+' telah berhasil di Rename \n')
+ sleep(2)
+ b.wait()
+ print('Create, Read, and delete file: '+file+' mencapai barrier pada waktu: %s \n' % (ctime()))
+
+def run():
+ getapi()
+ sleep(2)
+ b.wait()
+ print('Semua mencapai barrier pada waktu: %s \n' % (ctime()))
+
+def main():
+ threads = []
+ print('Gasskeun! Semoga berhasil')
+ for i in range(angka):
+ threads.append(Thread(target=run))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Yeaayy berhasil berhasil! Horeee!')
+ return True
+
+#if __name__ == "__main__":
+# main()
\ No newline at end of file
diff --git a/Chapter02/AlifTujuh1184068.py b/Chapter02/AlifTujuh1184068.py
new file mode 100644
index 0000000..c269944
--- /dev/null
+++ b/Chapter02/AlifTujuh1184068.py
@@ -0,0 +1,87 @@
+from threading import Thread,currentThread, Lock, RLock, Event
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = Event()
+
+class alifHandlingFile (Thread):
+ def __init__(self,name,thread_number,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.event = event
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ print('class alifHandlingFile mau menjalankan event wait dilanjutkan clear untuk baca dan rename file')
+ self.event.wait()
+ self.threadLock.acquire()
+ print('melakukan baca file : '+self.namafile)
+ self.readfile()
+ print('melakukan rename file : '+self.namafile)
+ self.renamefile()
+ self.event.wait()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "Baca file dan delete file selesai")
+ print ("menjalankan event clear")
+ self.event.clear()
+
+ def readfile(self):
+ q=queue.get()
+ f = open(self.namafile, "r")
+ print("Isi Filenya apaan yaa : "+f.read())
+ print("Sudah selesai baca isi file nya nih, jalanin queue task done \n")
+ queue.task_done()
+
+ def renamefile(self):
+ print('coba ganti nama filenya dong \n')
+ os.rename(self.namafile,self.namafile+'.txt')
+ print("nama file telah diganti \n")
+
+class alifTujuh1184068 (Thread):
+ def __init__(self, name,thread_number,a,b ,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.event = event
+ self.rlock = RLock()
+ self.name = name
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.thread_number = thread_number
+ self.a=a
+ self.b=b
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ self.threadLock.acquire()
+ print("threeadlock acquire utama")
+ self.hitung()
+ self.event.set()
+ print('class alifTujuh1184068 sudah selesai melakukan event set')
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def getapi(self):
+ with self.rlock:
+ print('didalam rlock apipangkat, akses web service...')
+ apiurl='http://www.emsifa.com/api-wilayah-indonesia/api/provinces.json'
+ response = requests.get(apiurl)
+ html=response.json()
+ queue.put(html)
+ self.createfile(html)
+
+ def hitung(self):
+ with self.rlock:
+ print('rlock hitung')
+ self.getapi()
+
+ def createfile(self,isi):
+ print('membuat file baru : '+ self.namafile)
+ f = open(self.namafile, "x")
+ f.write(str(isi))
+ f.close()
+ print('sudah membuat file baru, mau menjalankan event set')
+
diff --git a/Chapter02/Anisa1184016.py b/Chapter02/Anisa1184016.py
new file mode 100644
index 0000000..07dc905
--- /dev/null
+++ b/Chapter02/Anisa1184016.py
@@ -0,0 +1,70 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class AnisaRos1184016 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ self.pangkat(randint(2,19), 50)
+ print (str(self.thread_number)+". ---> " + self.name + " over.")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def pangkat(self, x, y):
+ while y>0:
+ x=x**2
+ y=y-1
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = AnisaRos1184016 ("Thread#1 ", 1,randint(1,10))
+ thread2 = AnisaRos1184016 ("Thread#2 ", 2,randint(1,10))
+ thread3 = AnisaRos1184016 ("Thread#3 ", 3,randint(1,10))
+ thread4 = AnisaRos1184016 ("Thread#4 ", 4,randint(1,10))
+ thread5 = AnisaRos1184016 ("Thread#5 ", 5,randint(1,10))
+ thread6 = AnisaRos1184016 ("Thread#6 ", 6,randint(1,10))
+ thread7 = AnisaRos1184016 ("Thread#7 ", 7,randint(1,10))
+ thread8 = AnisaRos1184016 ("Thread#8 ", 8,randint(1,10))
+ thread9 = AnisaRos1184016 ("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/AnisaRosalinaTujuh1184016.py b/Chapter02/AnisaRosalinaTujuh1184016.py
new file mode 100644
index 0000000..f86467d
--- /dev/null
+++ b/Chapter02/AnisaRosalinaTujuh1184016.py
@@ -0,0 +1,50 @@
+from threading import Thread
+import os
+import requests
+from queue import Queue
+
+drink = []
+queue = Queue()
+
+apiurl='https://www.thecocktaildb.com/api/json/v1/1/list.php?g=list'
+response = requests.get(apiurl)
+html=response.json()
+
+
+class Anisa(Thread):
+ def __init__(self, name, thread_number, filename):
+ Thread.__init__(self)
+ self.name = name
+ self.thread_number = thread_number
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def Glassapi(self):
+ string = "Drinks :"
+ for i in range(len(html["drinks"])):
+ hasil = html["drinks"][i]["strGlass"]
+ angka = "\n"+str(i)+". "
+ string = string+angka+hasil
+ drink.append(hasil)
+ queue.put(drink)
+ print(str(i)+'. Jenis Glass %s Appended from queue by %s' % (hasil, self.name))
+ self.fileresult(string)
+
+ def fileresult(self, isi):
+ f = open(self.filename+".txt", "w")
+ f.write(isi)
+ f.close()
+ read = queue.get()
+ r = len(read)
+ while r > 0:
+ f = open(self.filename+".txt", "r")
+ baca = f.readlines()
+ print('\n'+str(r)+' Hasil Readed from queue by %s' % ( self.name))
+ print(baca[r])
+ f.close()
+ r = r - 1
+ queue.task_done()
+
+ def run(self):
+ print("Mulai Queue!")
+ self.Glassapi()
+ print("Queue Selesai!")
diff --git a/Chapter02/AriyoEnam1184056.py b/Chapter02/AriyoEnam1184056.py
new file mode 100644
index 0000000..b178d5b
--- /dev/null
+++ b/Chapter02/AriyoEnam1184056.py
@@ -0,0 +1,62 @@
+from threading import Barrier, Thread
+import requests
+from time import ctime, sleep
+
+num_list = 1
+list_cocktail = Barrier(num_list)
+file = []
+filename = "list_cocktail.txt"
+
+def api():
+ apiurl ='https://www.thecocktaildb.com/api/json/v1/1/list.php?a=list'
+ response = requests.get(apiurl)
+ html=response.json()
+ string ="list: "
+ for i in range(len(html)):
+ hasil =html["drinks"]
+ tulis = "\n"+str(i)+". "
+ string = string+tulis+str(hasil)
+ createfile(string)
+ x = open(filename, "r")
+ print(x.read())
+
+
+
+def createfile(isi):
+ f = open(filename, "w")
+ f.write(str(isi))
+ f.close()
+
+def read():
+ x = open(filename, "r")
+ print(x.read())
+ x.close()
+
+def start():
+ api()
+ sleep(2)
+ list_cocktail.wait()
+ sleep(2)
+ print(' ini barrier nya : %s \n' % (ctime()))
+ list_cocktail.wait()
+ print('Create dan Read File Barrier: %s \n' % (ctime()))
+ read()
+ list_cocktail.wait()
+
+def main():
+ threads = []
+ print('Ayo Mulai')
+ for i in range(num_list):
+ threads.append(Thread(target=start))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Udah Selasi')
+ return True
+
+#if __name__ == "__main__":
+# main()
+
+
+
+
diff --git a/Chapter02/AriyoTujuh1184056.py b/Chapter02/AriyoTujuh1184056.py
new file mode 100644
index 0000000..0252721
--- /dev/null
+++ b/Chapter02/AriyoTujuh1184056.py
@@ -0,0 +1,71 @@
+from threading import Thread
+from queue import Queue
+import os
+import requests
+
+queue = Queue()
+ayo = []
+apiurl ='https://www.thecocktaildb.com/api/json/v1/1/list.php?a=list'
+response = requests.get(apiurl)
+html=response.json()
+
+class ayomaju(Thread):
+ def __init__(self, namathread, filename):
+ Thread.__init__(self)
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+ self.namathread = namathread
+
+ def mainapi(self):
+ string = "minuman:"
+ for i in range(len(html["drinks"])):
+ sini = html["drinks"][i]["strAlcoholic"]
+ nomor = "\n"+str(i)+". "
+ string = string + nomor + sini
+ ayo.append(sini)
+ queue.put(ayo)
+ print(str(i)+". %s yg di append" % (sini))
+ self.createfile(string)
+ self.mundur()
+
+ def run(self):
+ print("Kita mulai queunya")
+ self.mainapi()
+ print("queue selesai")
+
+ def createfile(self, isi):
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
+ def read(self):
+ x = open(self.filename, "r")
+ print(x.read())
+ x.close()
+
+ def mundur(self):
+ ayomundur = queue.get()
+ dur = len(ayomundur)
+ while dur > 0:
+ print("hasil pop")
+ dur = dur -1
+ self.read()
+ queue.task_done()
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Chapter02/Barrier.py b/Chapter02/Barrier.py
index 7afa6f7..9e1cd29 100644
--- a/Chapter02/Barrier.py
+++ b/Chapter02/Barrier.py
@@ -1,26 +1,30 @@
from random import randrange
from threading import Barrier, Thread
from time import ctime, sleep
-
+#akan menentukan berapa thread yang akan di tahan untuk diijinkan selesai eksekusi, tengan penanda wait()
+# intinya tentukan jumlah thread dan gunakaan wait()
num_runners = 3
finish_line = Barrier(num_runners)
-runners = ['Huey', 'Dewey', 'Louie']
+runners = ['Hermawan', 'Dadang', 'Asep']
def runner():
name = runners.pop()
- sleep(randrange(2, 5))
+ waktulari=randrange(2, 5)
+ print('Pelari : '+name+str(waktulari)+' detik, ')
+ sleep(waktulari)#hanya untuk memberikan jeda waktu random
+ finish_line.wait()#tidak akan lanjut ke perintah selanjutnya selama 3(num_runners) thread belum di eksesk
print('%s reached the barrier at: %s \n' % (name, ctime()))
- finish_line.wait()
+# In[]:
def main():
- threads = []
+ threads = []#buat list kosong namanya threads
print('START RACE!!!!')
- for i in range(num_runners):
- threads.append(Thread(target=runner))
- threads[-1].start()
+ for i in range(num_runners):#lakukan perulangan sebanyak 3 kali adalah jumlah pelari
+ threads.append(Thread(target=runner))#menyimpan objek thread ke dalam variable list threads
+ threads[-1].start()#mengambil list thread paling belakang
for thread in threads:
thread.join()
print('Race over!')
-if __name__ == "__main__":
- main()
+#if __name__ == "__main__":
+# main()
diff --git a/Chapter02/Condition.py b/Chapter02/Condition.py
index 828ceec..1e9893e 100644
--- a/Chapter02/Condition.py
+++ b/Chapter02/Condition.py
@@ -1,6 +1,5 @@
import logging
import threading
-import time
LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s'
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
@@ -17,9 +16,11 @@ def consume(self):
with condition:
- if len(items) == 0:
+ if len(items) == 0:#kondisi stack kosong
logging.info('no items to consume')
- condition.wait()
+ logging.info('concume : ini mau berenti')
+ condition.wait()#melakukan pause sampai dapat notify baru jalan lagi
+ logging.info('consume : dapat notifi jalan lagi')
items.pop()
logging.info('consumed 1 item')
@@ -28,7 +29,7 @@ def consume(self):
def run(self):
for i in range(20):
- time.sleep(2)
+ #time.sleep(2)
self.consume()
@@ -40,18 +41,20 @@ def produce(self):
with condition:
- if len(items) == 10:
+ if len(items) == 5:#kondisi penuh
logging.info('items produced {}. Stopped'.format(len(items)))
- condition.wait()
+ logging.info('produce : ini mau berenti')
+ condition.wait()#pause disini setelah dapat notify baru jalan kembali
+ logging.info('produce : dapat notifi jalan lagi')
- items.append(1)
+ items.append(11)
logging.info('total items {}'.format(len(items)))
condition.notify()
def run(self):
for i in range(20):
- time.sleep(0.5)
+ #time.sleep(0.5)
self.produce()
diff --git a/Chapter02/DiarTujuh1184083.py b/Chapter02/DiarTujuh1184083.py
new file mode 100644
index 0000000..e59772a
--- /dev/null
+++ b/Chapter02/DiarTujuh1184083.py
@@ -0,0 +1,89 @@
+import threading
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = threading.Event()
+
+class DiarQue(threading.Thread):
+ def __init__(self,name, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+ self.name + "Start")
+ self.threadLock.acquire()
+ print('kodekabupaten')
+ event.wait()
+ print('ubah : '+self.filename)
+ self.baca()
+ print("kode sudah diubah")
+ self.buat()
+ event.clear()
+ self.threadLock.release()
+ print("\n" + threading.currentThread().getName() + "berhasil")
+
+ def baca(self):
+ f = open(self.filename, "r")
+ print("isi file : \n "+f.read())
+
+ def buat(self):
+ f = open(self.filename, "r")
+ bikin = open(self.filename+'.txt', "w")
+ for line in f:
+ bikin.write(line.replace('kodepos', 'kabupaten'))
+ bikin.close()
+ new = open(self.filename+'.txt', "r")
+ print(new.read())
+ new.close()
+
+
+class DiarTujuh1184083 (threading.Thread):
+ def __init__(self, name, filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+
+
+ def run(self):
+ print("\n"+ self.name + " Start")
+ self.main()
+ cb = queue.get()
+ print("\n queuenya")
+ print(cb)
+ queue.task_done()
+ event.set()
+ print("\n" + threading.currentThread().getName() + "Finish")
+
+ def webapi(self):
+ with self.rlock:
+ print('Webservice nya')
+ apiurl='https://nbc.vanmason.web.id/service/kodepos/42173'
+ response = requests.get(apiurl)
+ html=response.json()
+ string = ""
+ for i in range(len(html["kodepos"])):
+ baru = html["kodepos"][i]["kabupaten"]
+ new = "\n"+str(i)+". "
+ string = string+new+str(baru)
+ queue.put(string)
+ self.createfile(string)
+
+
+ def main(self):
+ with self.rlock:
+ self.webapi()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
+
+
diff --git a/Chapter02/Event.py b/Chapter02/Event.py
index 4cc0eca..0a6b08a 100644
--- a/Chapter02/Event.py
+++ b/Chapter02/Event.py
@@ -3,8 +3,8 @@
import time
import random
-LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s'
-logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
+#LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s'
+#logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
items = []
event = threading.Event()
@@ -15,33 +15,39 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def run(self):
+ a=0
while True:
+ #for i in range(5):
time.sleep(2)
- event.wait()
- item = items.pop()
- logging.info('Consumer notify: {} popped by {}'\
- .format(item, self.name))
+ print('Konsumer : menunggu event set dari produser, status event.wait')
+ event.wait() #menunggu jika event=False / jika event=true maka event jalan
+ isinya=items.pop()
+ print(str(a)+'. Konsumer : sudah di pop,tumpukan list sebanyak : '+str(len(items))+' isinya : '+str(isinya))
+ #logging.info('Consumer notify: {} popped by {}'.format(item, self.name))
+ a=a+1
+ print('Konsumer : event clear')
+ event.clear() # set event=false
class Producer(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def run(self):
- for i in range(5):
+ for i in range(5):#ini perulangan berapa kali
time.sleep(2)
- item = random.randint(0, 100)
- items.append(item)
- logging.info('Producer notify: item {} appended by {}'\
- .format(item, self.name))
- event.set()
- event.clear()
+ isinya = random.randint(0, 100)
+ items.append(isinya) # add items
+ print(str(i)+'. Produser : sudah di append,tumpukan list sebanyak : '+str(len(items))+' isinya : '+str(isinya))
+ #logging.info('Producer notify: item {} appended by {}'.format(item, self.name))
+ print('Produser : event set')
+ event.set() # karena dia cuma ngubah variabel jadi true, jika set dan clear jalan cepat beberapa kali, itu dianggap satu kali jika ketemu wait
if __name__ == "__main__":
t1 = Producer()
t2 = Consumer()
- t1.start()
t2.start()
+ t1.start()
- t1.join()
- t2.join()
+ #t1.join()
+ #t2.join()
diff --git a/Chapter02/Ferdy1184112.py b/Chapter02/Ferdy1184112.py
new file mode 100644
index 0000000..2cb23bc
--- /dev/null
+++ b/Chapter02/Ferdy1184112.py
@@ -0,0 +1,73 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class Ferdy1184112 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ time.sleep(self.duration)
+ out_list = list()
+ self.do_something(randint(1,14), 1000000)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def do_something(self, x, y):
+ while y > 0:
+ for x in range(15):
+ h = 2**x
+ y = y-1
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Ferdy1184112("Thread#1 ", 1,randint(1,10))
+ thread2 = Ferdy1184112("Thread#2 ", 2,randint(1,10))
+ thread3 = Ferdy1184112("Thread#3 ", 3,randint(1,10))
+ thread4 = Ferdy1184112("Thread#4 ", 4,randint(1,10))
+ thread5 = Ferdy1184112("Thread#5 ", 5,randint(1,10))
+ thread6 = Ferdy1184112("Thread#6 ", 6,randint(1,10))
+ thread7 = Ferdy1184112("Thread#7 ", 7,randint(1,10))
+ thread8 = Ferdy1184112("Thread#8 ", 8,randint(1,10))
+ thread9 = Ferdy1184112("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/FerdyEnam1184112.py b/Chapter02/FerdyEnam1184112.py
new file mode 100644
index 0000000..9dbdb82
--- /dev/null
+++ b/Chapter02/FerdyEnam1184112.py
@@ -0,0 +1,47 @@
+from threading import Barrier, Thread
+import requests
+from time import ctime, sleep
+
+nomor = 1
+finish = Barrier(nomor)
+yuhu = []
+
+def webservice():
+ apiurl='https://www.thecocktaildb.com/api/json/v1/1/list.php?c=list'
+ response = requests.get(apiurl)
+ html=response.json()
+ for i in range(len(html["drinks"])):
+ cocokin = html["drinks"][i]
+ yuhu.append(cocokin)
+
+def bacafile():
+ f = open("barrier.txt", "r")
+ print("isi file setelah di pop: \n" +f.read())
+
+def utama():
+ webservice()
+ sleep(2)
+ name = yuhu.pop()
+ print('%s reached the barrier at: %s \n' % (name, ctime()))
+ finish.wait()
+ print('buat file baru namanya: barrier.txt \n')
+ f = open("barrier.txt","w")
+ f.write(str(yuhu))
+ f.close()
+ bacafile()
+
+def main ():
+ hiya=[]
+ print("\n Barrier Mulai \n")
+ for i in range(nomor):
+ hiya.append(Thread(target=utama))
+ hiya[-1].start()
+ for thread in hiya:
+ thread.join()
+ print("finish")
+ return True
+
+
+
+#if __name__ == "__main__":
+# main()
\ No newline at end of file
diff --git a/Chapter02/FerdyTiga1184112.py b/Chapter02/FerdyTiga1184112.py
new file mode 100644
index 0000000..7a44fc4
--- /dev/null
+++ b/Chapter02/FerdyTiga1184112.py
@@ -0,0 +1,87 @@
+import threading
+import requests
+import os
+
+event = threading.Event()
+
+class FerdyEventGI(threading.Thread):
+ def __init__(self,name, thread_number, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.thread_number = thread_number
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "mulai.")
+ self.threadLock.acquire()
+ print('Cocktail 1(')
+ event.wait()
+ print('ubah : '+self.filename)
+ self.readfile()
+ print("pas udah diubah")
+ self.bikinfile()
+ event.clear()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "berhasil")
+
+ def readfile(self):
+ f = open(self.filename, "r")
+ print("File's Content is : \n "+f.read())
+
+ def bikinfile(self):
+ f = open(self.filename, "r")
+ bikin = open(self.filename+'.txt', "w")
+ for line in f:
+ bikin.write(line.replace('strCategory', 'Jenis'))
+ bikin.close()
+ baru = open(self.filename+'.txt', "r")
+ print(baru.read())
+ baru.close()
+
+
+class FerdyGITiga1184112 (threading.Thread):
+ def __init__(self, name, thread_number ,filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+ self.thread_number = thread_number
+
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "mulai.")
+ self.hasil()
+ print("Event")
+ event.set()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "selesai.")
+
+ def apiwebservice(self):
+ with self.rlock:
+ print('Webservice nya')
+ apiurl='https://www.thecocktaildb.com/api/json/v1/1/list.php?c=list'
+ response = requests.get(apiurl)
+ html=response.json()
+ string = ""
+ for i in range(len(html["drinks"])):
+ cocokin = html["drinks"][i]
+ baru = "\n"+str(i)+". "
+ string = string+baru+str(cocokin)
+ self.createfile(string)
+ x = open(self.filename, "r")
+ print(x.read())
+
+ def hasil(self):
+ with self.rlock:
+ self.apiwebservice()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
+
+
+
diff --git a/Chapter02/FerdyTujuh1184112.py b/Chapter02/FerdyTujuh1184112.py
new file mode 100644
index 0000000..3d18037
--- /dev/null
+++ b/Chapter02/FerdyTujuh1184112.py
@@ -0,0 +1,90 @@
+import threading
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = threading.Event()
+
+class FerdyQueue(threading.Thread):
+ def __init__(self,name, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+ self.name + "mulai.")
+ self.threadLock.acquire()
+ print('Cocktail 1')
+ event.wait()
+ print('ubah : '+self.filename)
+ self.readfile()
+ print("pas udah diubah")
+ self.bikinfile()
+ event.clear()
+ self.threadLock.release()
+ print("\n" + threading.currentThread().getName() + "berhasil")
+
+ def readfile(self):
+ f = open(self.filename, "r")
+ print("File's Content is : \n "+f.read())
+
+ def bikinfile(self):
+ f = open(self.filename, "r")
+ bikin = open(self.filename+'.txt', "w")
+ for line in f:
+ bikin.write(line.replace('strCategory', 'Jenis'))
+ bikin.close()
+ baru = open(self.filename+'.txt', "r")
+ print(baru.read())
+ baru.close()
+
+
+class Ferdy (threading.Thread):
+ def __init__(self, name, filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+
+
+ def run(self):
+ print("\n"+ self.name + "mulai.")
+ self.hasil()
+ cb = queue.get()
+ print("\n queuenya")
+ print(cb)
+ queue.task_done()
+ event.set()
+ print("\n" + threading.currentThread().getName() + "selesai.")
+
+ def apiwebservice(self):
+ with self.rlock:
+ print('Webservice nya')
+ apiurl='https://www.thecocktaildb.com/api/json/v1/1/list.php?c=list'
+ response = requests.get(apiurl)
+ html=response.json()
+ string = ""
+ for i in range(len(html["drinks"])):
+ cocokin = html["drinks"][i]
+ baru = "\n"+str(i)+". "
+ string = string+baru+str(cocokin)
+ queue.put(string)
+ self.createfile(string)
+
+
+ def hasil(self):
+ with self.rlock:
+ self.apiwebservice()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
+
+
+
diff --git a/Chapter02/Hanif1184058.py b/Chapter02/Hanif1184058.py
new file mode 100644
index 0000000..96a1a50
--- /dev/null
+++ b/Chapter02/Hanif1184058.py
@@ -0,0 +1,72 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class Hanif1184058 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ time.sleep(self.duration)
+ out_list = list()
+ self.do_something(1000000,out_list)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def do_something(self,count,out_list):
+ for out_list in range(count):
+ if out_list % 2 == 1:
+ out_list = list()
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Hanif1184058("Thread#1 ", 1,randint(1,10))
+ thread2 = Hanif1184058("Thread#2 ", 2,randint(1,10))
+ thread3 = Hanif1184058("Thread#3 ", 3,randint(1,10))
+ thread4 = Hanif1184058("Thread#4 ", 4,randint(1,10))
+ thread5 = Hanif1184058("Thread#5 ", 5,randint(1,10))
+ thread6 = Hanif1184058("Thread#6 ", 6,randint(1,10))
+ thread7 = Hanif1184058("Thread#7 ", 7,randint(1,10))
+ thread8 = Hanif1184058("Thread#8 ", 8,randint(1,10))
+ thread9 = Hanif1184058("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/HanifDua1184058.py b/Chapter02/HanifDua1184058.py
new file mode 100644
index 0000000..9525aa5
--- /dev/null
+++ b/Chapter02/HanifDua1184058.py
@@ -0,0 +1,83 @@
+from threading import Thread,currentThread, Lock, RLock, Semaphore
+import requests
+import os
+import shutil
+
+semaphore = Semaphore(0)
+hasilperhitungan=0
+
+class hanifCopy (Thread):
+ def __init__(self,name,thread_number,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.semaphore = semaphore
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "mulai")
+ print('menjalankan semaphore acquire')
+ self.threadLock.acquire()
+ self.semaphore.acquire()
+ print('membaca file : '+self.namafile)
+ self.readfile()
+ print('menyalin file : '+self.namafile)
+ self.copyfile()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def readfile(self):
+ f = open(self.namafile, "r")
+ print("file : "+f.read())
+
+ def copyfile(self):
+ shutil.copy('.\Chapter02\provinsi.txt', '.\Chapter02')
+
+class hanifDua113040087 (Thread):
+ def __init__(self, name,thread_number,a,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.semaphore = semaphore
+ self.rlock = RLock()
+ self.name = name
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.thread_number = thread_number
+ self.a=a
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ self.threadLock.acquire()
+ print("threadlock acquire utama")
+ self.hitung()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def apitempat(self):
+ with self.rlock:
+ print('mengakses web service')
+ apiurl='https://dev.farizdotid.com/api/daerahindonesia/kota?id_provinsi='
+ eq=str(self.a)
+ response = requests.get(apiurl+eq)
+ html=response.json()
+ hasil = html["nama"]
+ print("Nama Kabupaten : "+str(hasil))
+ self.createfile(hasil)
+
+ def hitung(self):
+ with self.rlock:
+ print('rlock hitung')
+ self.apitempat()
+
+ def createfile(self,isi):
+ print('membuat file baru : '+ self.namafile)
+ f = open(self.namafile, "x")
+ f.write(str(isi))
+ f.close()
+ print('me-release semaphore')
+ self.semaphore.release()
+ print('semaphore selesai di release')
+
+
+
+
diff --git a/Chapter02/HanifEnam1184058.py b/Chapter02/HanifEnam1184058.py
new file mode 100644
index 0000000..b2aeef4
--- /dev/null
+++ b/Chapter02/HanifEnam1184058.py
@@ -0,0 +1,57 @@
+from threading import Barrier, Thread
+import os
+import requests
+from time import ctime, sleep
+
+bar = Barrier(1)
+file = "pokemon"
+filename = os.path.join(os.path.dirname(__file__), file)
+
+def createfile(isi):
+ print('Buat file : '+file+'.txt \n')
+ f = open(filename+".txt", "w")
+ f.write(str(isi))
+ f.close()
+ print('Membaca file: '+file+'.txt \n')
+ x = open(filename+".txt", "r")
+ print(x.read()+'\n')
+ x.close()
+ print("Copy file ke direktori luar")
+ copyfile()
+ print('Membuat, membaca dan copy '+file+' menggapai barrier dalam: %s \n' % (ctime()))
+ sleep(2)
+ bar.wait()
+
+def copyfile():
+ with open ("F:\\Kuliah\\Semester 6\\Sistem Tersebar\\Python-Parallel-Programming-Cookbook-Second-Edition\\Chapter02\\pokemon.txt", "rb") as read:
+ with open ("F:\\Kuliah\\Semester 6\\Sistem Tersebar\\Python-Parallel-Programming-Cookbook-Second-Edition\\pokemon_copy.txt", "wb") as filetobecopied:
+ filetobecopied.write(read.read())
+
+def randomapi():
+ print('akses web service...')
+ apiurl='https://pokeapi.co/api/v2/pokemon/ditto'
+ response = requests.get(apiurl)
+ html=response.json()
+ string = "ability :"
+ for i in range(len(html["abilities"])):
+ hasil = html["abilities"][i]["ability"]["name"]
+ tulis = "\n"+str(i)+". "
+ string = string+tulis+str(hasil)
+ createfile(string)
+
+def run():
+ randomapi()
+ sleep(2)
+ bar.wait()
+ print('All reached the barrier at: %s \n' % (ctime()))
+
+def main():
+ threads = []
+ print('\nBismillah berkah Ramadhan \n')
+ for i in range(1):
+ threads.append(Thread(target=run))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Alhamdulillah bisa')
+ return True
diff --git a/Chapter02/HanifTiga1184058.py b/Chapter02/HanifTiga1184058.py
new file mode 100644
index 0000000..a11949f
--- /dev/null
+++ b/Chapter02/HanifTiga1184058.py
@@ -0,0 +1,80 @@
+import threading
+import requests
+import os
+
+event = threading.Event()
+
+class hanifRename (threading.Thread):
+ def __init__(self,name, thread_number, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.thread_number = thread_number
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.threadLock.acquire()
+ print('Menunggu event')
+ event.wait()
+ print('Nama setelah rename: '+self.filename)
+ self.readfile()
+ print("Baca file setelah rename")
+ self.rewritefile()
+ event.clear()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "end")
+
+ def readfile(self):
+ f = open(self.filename, "r")
+ print("File's Content is : \n "+f.read())
+ f.close()
+
+ def rewritefile(self):
+ os.rename(self.filename, self.filename+'.docx')
+
+class hanifTiga1184058 (threading.Thread):
+ def __init__(self, name, thread_number, a ,filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+ self.thread_number = thread_number
+ self.a=a
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.count()
+ print("Even set")
+ event.set()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "finish.")
+
+ def randomapi(self):
+ with self.rlock:
+ print('akses web service...')
+ apiurl='https://pokeapi.co/api/v2/pokemon/ditto'
+ response = requests.get(apiurl)
+ html=response.json()
+ string = "ability :"
+ for i in range(len(html["abilities"])):
+ hasil = html["abilities"][i]["ability"]["name"]
+ tulis = "\n"+str(i)+". "
+ string = string+tulis+str(hasil)
+ self.createfile(string)
+ x = open(self.filename, "r")
+ print(x.read())
+
+ def count(self):
+ with self.rlock:
+ self.randomapi()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
+
+
+
diff --git a/Chapter02/HanifTujuh1184058.py b/Chapter02/HanifTujuh1184058.py
new file mode 100644
index 0000000..094efe3
--- /dev/null
+++ b/Chapter02/HanifTujuh1184058.py
@@ -0,0 +1,83 @@
+import threading
+import requests
+from queue import Queue
+import os
+
+event = threading.Event()
+queue = Queue()
+
+class HanifCopy (threading.Thread):
+ def __init__(self, name, thread_number, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.thread_number = thread_number
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.threadLock.acquire()
+ event.wait()
+ print('Copy file : '+self.filename)
+ self.readfile()
+ print("Read file")
+ self.copyfile()
+ event.clear()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "end")
+
+ def readfile(self):
+ f = open(self.filename, "r")
+ print("Isi file : "+f.read())
+
+ def copyfile(self):
+ queue.get()
+ with open ("F:\\Kuliah\\Semester 6\\Sistem Tersebar\\Python-Parallel-Programming-Cookbook-Second-Edition\\Chapter02\\pokemon.txt", "rb") as read:
+ with open ("F:\\Kuliah\\Semester 6\\Sistem Tersebar\\Python-Parallel-Programming-Cookbook-Second-Edition\\pokemon.txt", "wb") as filetobecopied:
+ filetobecopied.write(read.read())
+ queue.task_done()
+
+
+class HanifTujuh1184058 (threading.Thread):
+ def __init__(self, name, thread_number, a ,filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+ self.thread_number = thread_number
+ self.a=a
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.count()
+ event.set()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "finish.")
+
+ def getapi(self):
+ with self.rlock:
+ print('Akses web service')
+ apiurl='https://pokeapi.co/api/v2/pokemon/ditto'
+ response = requests.get(apiurl)
+ html=response.json()
+ string = "ability :"
+ global x
+ x = self.a
+ for i in range(len(html["abilities"])):
+ hasil = html["abilities"][i]["ability"]["name"]
+ tulis = "\n"+str(i)+". "
+ string = string+tulis+str(hasil)
+ queue.put(x)
+ print('%d Writed from queue by %s' % (x, self.name))
+ x = x-1
+ self.createfile(string)
+
+ def count(self):
+ with self.rlock:
+ self.getapi()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
\ No newline at end of file
diff --git a/Chapter02/Ida1184113.py b/Chapter02/Ida1184113.py
new file mode 100644
index 0000000..2358fa1
--- /dev/null
+++ b/Chapter02/Ida1184113.py
@@ -0,0 +1,69 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class Ida1184113 (Thread):
+ def __init__(self, name,thread_number):
+ Thread.__init__(self)
+ self.name = name
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ self.do_something(randint(200, 999), 170000)
+ print (str(self.thread_number)+". ---> " + self.name + " over.")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def do_something(self, n, x):
+ while x > 0:
+ n = n*3
+ x = x-1
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Ida1184113("Thread#1 ", 1)
+ thread2 = Ida1184113("Thread#2 ", 2)
+ thread3 = Ida1184113("Thread#3 ", 3)
+ thread4 = Ida1184113("Thread#4 ", 4)
+ thread5 = Ida1184113("Thread#5 ", 5)
+ thread6 = Ida1184113("Thread#6 ", 6)
+ thread7 = Ida1184113("Thread#7 ", 7)
+ thread8 = Ida1184113("Thread#8 ", 8)
+ thread9 = Ida1184113("Thread#9 ", 9)
+
+ # 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/IdaEnam1184113.py b/Chapter02/IdaEnam1184113.py
new file mode 100644
index 0000000..7d5a07c
--- /dev/null
+++ b/Chapter02/IdaEnam1184113.py
@@ -0,0 +1,57 @@
+from threading import Barrier, Thread
+from time import ctime, sleep
+import requests
+
+jumlah = 3
+shield = Barrier(jumlah)
+runners = []
+filename = "luffy.txt"
+#api ida
+def api():
+ url = "https://api.jikan.moe/v3/search/anime?q=One%20Piece"
+ response = requests.get(url)
+ html=response.json()
+ string = "Anime: "
+ for i in range(len(html)):
+ hasil = html["results"][i]["title"]
+ data = str(hasil)
+ ent = "\n"+str(i)+". "
+ string = string+ent+data
+ runners.append(data)
+ createfile(string)
+
+def read():
+ x = open(filename, "r")
+ print(x.read())
+
+def createfile(isi):
+ f = open(filename, "w")
+ f.write(str(isi))
+ f.close()
+
+def runner():
+ api()
+ sleep(2)
+ shield.wait()
+ name = runners.pop()
+ sleep(2)
+ print('%s sudah mencaapai barier pada: %s \n' % (name, ctime()))
+ shield.wait()
+ print('Create dan Read File sudah mencaapai barier pada: %s \n' % (ctime()))
+ read()
+ shield.wait()
+
+
+def main():
+ threads = []
+ print('Mulai!')
+ for i in range(jumlah):
+ threads.append(Thread(target=runner))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Selesai!')
+ return True
+
+#if __name__ == "__main__":
+# main()
diff --git a/Chapter02/IdaTiga1184113.py b/Chapter02/IdaTiga1184113.py
new file mode 100644
index 0000000..91d7be7
--- /dev/null
+++ b/Chapter02/IdaTiga1184113.py
@@ -0,0 +1,84 @@
+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 ida1184113_Catcher (threading.Thread):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+
+
+ def trying(self):
+
+ with condition:
+ f = open("idanilai.txt", "w+")
+ for i in range(10):
+ f.write("how are you? %d\r\n" % (i+1))
+
+ if len(items) == 0:
+ logging.info('no items to consume')
+
+ condition.wait()
+
+ items.pop()
+ logging.info('consumed 1 item')
+
+ condition.notify()
+
+
+ def run(self):
+ for i in range(20):
+ time.sleep(2)
+ self.trying()
+
+
+class ida1184113Write_Trier(threading.Thread):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+ def apiIda(self):
+ print('Dalam apiIda, akses webservice...')
+ apiurl='https://api.instagram.com/v1/tags/{tag-name}/media/recent'
+ eq=str(self.r)+'*'+str(self.f)
+ requests=str()
+ response = requests.get(apiurl)
+ html=response.content.decode(response.encoding)
+ hasil = int(html)
+ string = "hasil isinya = "
+ i = 1
+ for i in range (1,10):
+ konten = (1,10)
+ items.append(konten)
+ string = string+str(i)
+ i = i + 1
+ self.createfile(string)
+ x = open(self.nfile, "r+")
+ print(x.read())
+ konten = (1,10)
+ items.append(konten)
+
+ def catching(self):
+
+ with condition:
+
+ if len(items) == 10:
+ logging.info('items cathed {}. Stopped'.format(len(items)))
+ condition.wait()
+
+ items.append(1)
+ logging.info('total items {}'.format(len(items)))
+
+ condition.notify()
+
+ def run(self):
+ for i in range(20):
+ time.sleep(0.5)
+ self.catching()
+
\ No newline at end of file
diff --git a/Chapter02/IdaTujuh1184113.py b/Chapter02/IdaTujuh1184113.py
new file mode 100644
index 0000000..5dbd4c4
--- /dev/null
+++ b/Chapter02/IdaTujuh1184113.py
@@ -0,0 +1,93 @@
+from threading import Thread,currentThread, Lock, RLock, Event
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = Event()
+
+class Director (Thread):
+ def __init__(self,name,thread_number,idafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.idafile=os.path.join(os.path.dirname(__file__), idafile)
+ self.event = event
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ print('class Director mau menjalankan event wait dilanjutkan clear untuk baca dan rename file')
+ self.event.wait()
+ self.threadLock.acquire()
+ print('read file kamu: '+self.idafile)
+ self.readfile()
+ print('rename file kamu: '+self.idafile)
+ self.renamefile()
+ self.event.wait()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "Baca file dan delete file selesai")
+ print ("menjalankan event clear")
+ self.event.clear()
+
+ def readfile(self):
+ ida=queue.get()
+ f = open(self.idafile, "r")
+ print("Isi Filenya apa hayo : "+f.read())
+ print("Sudah selesai baca isi file nya yaw, jalanin queue task done \n")
+ queue.task_done()
+
+ def renamefile(self):
+ print('coba ganti nama filenya dulu dong \n')
+ os.rename(self.idafile,self.idafile+'.txt')
+ print("nama file udah diganti yaww \n")
+
+class idaTujuh1184113 (Thread):
+ def __init__(self, name,thread_number,a,b ,idafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.event = event
+ self.rlock = RLock()
+ self.name = name
+ self.idafile=os.path.join(os.path.dirname(__file__), idafile)
+ self.thread_number = thread_number
+ self.a=a
+ self.b=b
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ self.threadLock.acquire()
+ print("threeadlock acquire utama")
+ self.hitung()
+ self.event.set()
+ print('class idaTujuh1184113 sudah selesai melakukan event set')
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def getapi(self):
+ with self.rlock:
+ print('akses web service...')
+ apiurl = "https://api.jikan.moe/v3/search/anime?q=One%20Piece"
+ response = requests.get(apiurl)
+ html=response.json()
+ string = "Anime: "
+ for i in range(len(html)):
+ hasil = html["results"][i]["title"]
+ data = str(hasil)
+ ent = "\n"+str(i)+". "
+ string = string+ent+data
+ queue.put(string)
+ self.createfile(string)
+
+ def hitung(self):
+ with self.rlock:
+ print('rlock hitung')
+ self.getapi()
+
+ def createfile(self,isi):
+ print('file baru kamu: '+ self.idafile)
+ f = open(self.idafile, "x")
+ f.write(str(isi))
+ f.close()
+ print('sudah membuat file baru, mau menjalankan event set')
+
diff --git a/Chapter02/IraDwita1184024.py b/Chapter02/IraDwita1184024.py
new file mode 100644
index 0000000..b30762a
--- /dev/null
+++ b/Chapter02/IraDwita1184024.py
@@ -0,0 +1,70 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class IraDwita1184024 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ self.do_something(randint(300,999),1000000)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def do_something(self, n, x):
+ while x > 0:
+ n = n /3
+ x = x -1
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread2 = IraDwita1184024("Thread#2 ", 2,randint(1,10))
+ thread3 = IraDwita1184024("Thread#3 ", 3,randint(1,10))
+ thread1 = IraDwita1184024("Thread#1 ", 1,randint(1,10))
+ thread4 = IraDwita1184024("Thread#4 ", 4,randint(1,10))
+ thread5 = IraDwita1184024("Thread#5 ", 5,randint(1,10))
+ thread6 = IraDwita1184024("Thread#6 ", 6,randint(1,10))
+ thread7 = IraDwita1184024("Thread#7 ", 7,randint(1,10))
+ thread8 = IraDwita1184024("Thread#8 ", 8,randint(1,10))
+ thread9 = IraDwita1184024("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/IraEnam1184024.py b/Chapter02/IraEnam1184024.py
new file mode 100644
index 0000000..4c15031
--- /dev/null
+++ b/Chapter02/IraEnam1184024.py
@@ -0,0 +1,58 @@
+from threading import Barrier, Thread
+from time import ctime, sleep
+import requests
+import os
+
+jumlah = 1
+shield = Barrier(jumlah)
+filename = "iraa.txt"
+filename2 = "iranew.txt"
+
+def getapi():
+ apiurl='https://api.thecatapi.com/v1/images/search'
+ response = requests.get(apiurl)
+ html=response.json()
+ createfile(html)
+
+def createfile(isi):
+ f = open(filename, "w")
+ f.write(str(isi))
+ f.close()
+
+def read():
+ x = open(filename, "r")
+ print(x.read())
+
+def renamefile():
+ os.rename(filename, filename2)
+ print('file sudah direname')
+
+def deletefile():
+ os.remove(filename2)
+ print('File sudah dihapus')
+
+def runner():
+ getapi()
+ sleep(2)
+ shield.wait()
+ sleep(2)
+ print('udah mencapai barrier nih : %s \n' % (ctime()))
+ shield.wait()
+ print('Semua mencapai barrier pada waktunya nih: %s \n' % (ctime()))
+ read()
+ shield.wait()
+
+
+def main():
+ threads = []
+ print('Start!')
+ for i in range(jumlah):
+ threads.append(Thread(target=runner))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Finish!')
+ return True
+
+#if __name__ == "__main__":
+# main()
\ No newline at end of file
diff --git a/Chapter02/IraTujuh1184024.py b/Chapter02/IraTujuh1184024.py
new file mode 100644
index 0000000..b0bb23b
--- /dev/null
+++ b/Chapter02/IraTujuh1184024.py
@@ -0,0 +1,92 @@
+from threading import Thread,currentThread, Lock, RLock, Event
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = Event()
+
+class iraHandlingFile (Thread):
+ def __init__(self,name,thread_number,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.event = event
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ print('class iraHandlingFile menjalankan event wait dilanjutkan clear untuk baca dan rename file')
+ self.event.wait()
+ self.threadLock.acquire()
+ print('melakukan baca file : '+self.namafile)
+ self.readfile()
+ print('melakukan rename file : '+self.namafile)
+ self.renamefile()
+ self.event.wait()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "Baca file dan delete file sudah selesai")
+ print ("menjalankan event clear")
+ self.event.clear()
+
+ def readfile(self):
+ q=queue.get()
+ f = open(self.namafile, "r")
+ print("melihat apa isi file : "+f.read())
+ print("selesai membaca isi file , jalanin queue task done \n")
+
+ a = open(self.namafile, "a+")
+ a.write("\n ini adalah api tentang menampilkan gambar kucing %d\r\n")
+ a.close()
+ print('Setelah append file')
+ queue.task_done()
+
+ def renamefile(self):
+ print('mengganti nama file \n')
+ os.rename(self.namafile,self.namafile+'.txt')
+ print("nama file telah diganti \n")
+
+class iraTujuh1184024 (Thread):
+ def __init__(self, name,thread_number,a,b ,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.event = event
+ self.rlock = RLock()
+ self.name = name
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.thread_number = thread_number
+ self.a=a
+ self.b=b
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ self.threadLock.acquire()
+ print("threeadlock acquire utama")
+ self.hitung()
+ self.event.set()
+ print('class iraTujuh1184024 sudah selesai melakukan event set')
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def getapi(self):
+ with self.rlock:
+ print('akses web service...')
+ apiurl='https://api.thecatapi.com/v1/images/search'
+ response = requests.get(apiurl)
+ html=response.json()
+ queue.put(html)
+ self.createfile(html)
+
+ def hitung(self):
+ with self.rlock:
+ print('rlock hitung')
+ self.getapi()
+
+ def createfile(self,isi):
+ print('membuat file baru : '+ self.namafile)
+ f = open(self.namafile, "x")
+ f.write(str(isi))
+ f.close()
+ print('sudah selesai membuat file baru, mau menjalankan event set')
+
diff --git a/Chapter02/IrfanHernandez1184014.py b/Chapter02/IrfanHernandez1184014.py
new file mode 100644
index 0000000..7326e1f
--- /dev/null
+++ b/Chapter02/IrfanHernandez1184014.py
@@ -0,0 +1,76 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class IrfanHernandez1184014 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ time.sleep(self.duration)
+ out_list = [2,54,38,76,23,56,84,90]
+ self.do_something(out_list)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def do_something(self,out_list):
+ for j in range(len(out_list)-1,-1,-1):
+ hole = j
+ while hole <(len(out_list)-1) and out_list[hole+1]>out_list[hole]:
+ out_list[hole] = out_list[hole+1]
+ hole = hole+1
+ out_list[hole] = out_list[j]
+ out_list = list()
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = IrfanHernandez1184014("Thread#1 ", 1,randint(1,10))
+ thread2 = IrfanHernandez1184014("Thread#2 ", 2,randint(1,10))
+ thread3 = IrfanHernandez1184014("Thread#3 ", 3,randint(1,10))
+ thread4 = IrfanHernandez1184014("Thread#4 ", 4,randint(1,10))
+ thread5 = IrfanHernandez1184014("Thread#5 ", 5,randint(1,10))
+ thread6 = IrfanHernandez1184014("Thread#6 ", 6,randint(1,10))
+ thread7 = IrfanHernandez1184014("Thread#7 ", 7,randint(1,10))
+ thread8 = IrfanHernandez1184014("Thread#8 ", 8,randint(1,10))
+ thread9 = IrfanHernandez1184014("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/IrfanHernandezEnam1184014.py b/Chapter02/IrfanHernandezEnam1184014.py
new file mode 100644
index 0000000..b58b819
--- /dev/null
+++ b/Chapter02/IrfanHernandezEnam1184014.py
@@ -0,0 +1,53 @@
+from threading import Barrier, Thread
+import requests
+import os
+from time import ctime, sleep
+
+total = 1
+nama_minuman = Barrier(total)
+file = "Miramas"
+filename=os.path.join(os.path.dirname(__file__), file)
+
+def api():
+ apiurl='https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita'
+ response = requests.get(apiurl)
+ html=response.json()
+ string="minuman : "
+ for i in range(len(html["drinks"])):
+ hasil = html["drinks"][i]["strDrink"]
+ tulis = "\n"+str(i)+". "
+ string = string+tulis+str(hasil)
+ createfile(string)
+ x = open(filename, "r")
+ print(x.read())
+
+def createfile(isi):
+ print('Create File : '+ filename)
+ f = open(filename, "w")
+ f.write(str(isi))
+ f.close()
+
+def readfile():
+ f = open(filename, "r")
+ print("Isi Dari File ini : \n "+f.read())
+ f.close()
+
+def start():
+ api()
+ sleep(4)
+ readfile()
+ print('api dan readfile freached the barrier at: %s \n' % (ctime()))
+ nama_minuman.wait()
+
+
+def main():
+ threads = []
+ print('Minuman Yang tersedia')
+ for i in range(total):
+ threads.append(Thread(target=start))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Telah Muncul')
+ return True
+
diff --git a/Chapter02/IrfanHernandezTujuh1184014.py b/Chapter02/IrfanHernandezTujuh1184014.py
new file mode 100644
index 0000000..148dd99
--- /dev/null
+++ b/Chapter02/IrfanHernandezTujuh1184014.py
@@ -0,0 +1,58 @@
+from threading import Thread
+import os
+import requests
+from queue import Queue
+
+queue = Queue()
+drink = []
+
+apiurl='https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita'
+response = requests.get(apiurl)
+html=response.json()
+
+
+class IrfanPut(Thread):
+ def __init__(self, name, thread_number, filename):
+ Thread.__init__(self)
+ self.name = name
+ self.thread_number = thread_number
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def margaritaapi(self):
+ string = "Drinks :"
+ for i in range(len(html["drinks"])):
+ hasil = html["drinks"][i]["strDrink"]
+ angka = "\n"+str(i)+". "
+ string = string+angka+hasil
+ drink.append(hasil)
+ queue.put(drink)
+ print(str(i)+'. Margarita %s Appended from queue by %s' % (hasil, self.name))
+ self.fileresult(string)
+
+ def fileresult(self, isi):
+ f = open(self.filename+".txt", "w")
+ f.write(isi)
+ f.close()
+
+ def readfile(self):
+ read = queue.get()
+ r = len(read)
+ while r > 0:
+ f = open(self.filename+".txt", "r")
+ baca = f.readlines()
+ print('\n'+str(r)+' Tulis Readed from queue by %s' % ( self.name))
+ print(baca[r])
+ f.close()
+ r = r - 1
+ queue.task_done()
+
+ def run(self):
+ print("Start Queue!")
+ self.margaritaapi()
+ self.readfile()
+ print("Queue Finish!")
+
+
+
+
+
diff --git a/Chapter02/JosuansefDua_1184091.py b/Chapter02/JosuansefDua_1184091.py
new file mode 100644
index 0000000..9269cfb
--- /dev/null
+++ b/Chapter02/JosuansefDua_1184091.py
@@ -0,0 +1,85 @@
+from threading import Thread, currentThread, Lock, RLock, Semaphore
+import requests
+import os
+
+
+semaphore = Semaphore(0)
+hasilperhitungan = 0
+
+
+class josua1184091Arrangement (Thread):
+ def __init__(self, nama, thread_number, filenya):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.nama = nama
+ self.thread_number = thread_number
+ self.filenya = os.path.join(os.path.dirname(__file__), filenya)
+ self.semaphore = semaphore
+
+ def jalankan(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.nama + "run")
+ print('run semaphore yaitu untuk membuat dan menghapus file')
+ self.threadLock.acquire()
+ self.semaphore.acquire()
+ print('buat file : '+self.filenya)
+ self.createfile()
+ print('ubah file : '+self.filenya)
+ self.renamefile()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " +
+ currentThread().getName() + "selesai")
+
+ def bacafile(self):
+ f = open(self.filenya, "r")
+ print("Josua1184091 : "+f.read())
+
+ def ubahnamafile(self):
+ os.rename(self.filenya, self.filenya+'.Josuainthehouse')
+
+
+class josua1184091Api (Thread):
+ def __init__(self, nama, thread_number, bravo, delta, filenya):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.semaphore = semaphore
+ self.rlock = RLock()
+ self.nama = nama
+ self.filenya = os.path.join(os.path.dirname(__file__), filenya)
+ self.thread_number = thread_number
+ self.bravo = bravo
+ self.delta = delta
+
+ def jalankan(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.nama + "run")
+ self.threadLock.acquire()
+ print("ini adalah threadlock aqcuire")
+ self.kalkulasi()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " +
+ currentThread().getName() + "yoi beres")
+
+ def fungsiapi(self):
+ with self.rlock:
+ print('Mengakses fungsi api web service pada Rlock')
+ api_url = 'https://api.mathjs.org/v4/?expr='
+
+ eq = str(self.bravo)+'^'+str(self.delta)
+ response = requests.get(api_url+eq)
+ html = response.content.decode(response.encoding)
+ result = int(html)
+ print("result : "+str(result))
+ self.createfile(result)
+
+ def kalkulasi(self):
+ with self.rlock:
+ print('mengkalkulasi Rlock')
+ self.fungsiapi()
+
+ def buatfile(self, konten):
+ print('buat file : ' + self.filenya)
+ f = open(self.filenya, "x")
+ f.write(str(konten))
+ f.close()
+ print('buat file baru')
+ self.semaphore.release()
+ print('semaphore sudah di release')
diff --git a/Chapter02/JosuansefEnam_1184091.py b/Chapter02/JosuansefEnam_1184091.py
new file mode 100644
index 0000000..4dfe326
--- /dev/null
+++ b/Chapter02/JosuansefEnam_1184091.py
@@ -0,0 +1,67 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu Apr 15 12:31:12 2021
+
+@author: Josuansef Pardede (1184091)
+"""
+from threading import Barrier, Thread
+import os
+import requests
+from time import ctime, sleep
+
+jenis_file = 1
+batas = Barrier(jenis_file)
+file = "movie_2020_"
+filename = os.path.join(os.path.dirname(__file__), file)
+
+
+def ApiJosuansef1184091():
+ url = "https://movie-database-imdb-alternative.p.rapidapi.com/"
+ querystring = {"s":"Avengers Endgame","page":"1","r":"json"}
+
+ headers = {
+ 'x-rapidapi-key': "03ea4d51b5mshde64ddab3687a71p1fb88fjsn3c194b2ea8bc",
+ 'x-rapidapi-host': "movie-database-imdb-alternative.p.rapidapi.com"
+ }
+
+ response = requests.request("GET", url, headers=headers, params=querystring)
+ html=response.json()
+ print(response.text)
+ Josuansef1184091_CreateAppendFile(html)
+
+def Josuansef1184091_CreateAppendFile(konten):
+ for i in range(2):
+ print('Create File : '+file+str(i)+'.txt \n')
+ f = open(filename+str(i)+".txt", "w+")
+ f.write(str(konten))
+ f.close()
+
+ print('Append File: '+file+str(i)+'.txt')
+ a = open(filename+str(i)+".txt", "a+")
+ a.write(" => Film Terbaik Sepanjang Masa setelah 'Titanic' %d\r\n")
+ a.close()
+ print('Create & Append '+file+str(i)+' reached the barrier at: %s \n' % (ctime()))
+ sleep(2)
+ batas.wait()
+
+def run():
+ ApiJosuansef1184091()
+ sleep(2)
+ batas.wait()
+ print('All reached the barrier at: %s \n' % (ctime()))
+
+### In[]:
+
+def main():
+ threads = []
+ print('MULAI LAH, APA LAGI YEKAN!')
+ for i in range(jenis_file):
+ threads.append(Thread(target=run))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('UDAH UDAH! UDAH SELESAI GUYS!')
+ return True
+
+#if __name__ == "__main__":
+ #main()
\ No newline at end of file
diff --git a/Chapter02/JosuansefTiga1184091.py b/Chapter02/JosuansefTiga1184091.py
new file mode 100644
index 0000000..5cf31c4
--- /dev/null
+++ b/Chapter02/JosuansefTiga1184091.py
@@ -0,0 +1,105 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu Apr 8 13:56:38 2021
+
+@author: ASUS
+"""
+
+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 josua1184091Write_Trier(threading.Thread):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+
+
+ def trying(self):
+
+ with condition:
+ f = open("josh123.txt", "w+")
+ for i in range(10):
+ f.write("hi are you okay number %d\r\n" % (i+1))
+
+ if len(items) == 0:
+ logging.info('no items to try')
+
+ condition.wait()
+
+ items.pop()
+ logging.info('tried 1 item')
+
+ condition.notify()
+
+
+ def run(self):
+ for i in range(20):
+ time.sleep(2)
+ self.trying()
+
+
+class josua1184091_Catcher(threading.Thread):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+ def apiJosua(self):
+ print('Dalam apiJosua, akses webservice...')
+ apiurl='https://www.beanstream.com/api/v1'
+ eq=str(self.r)+'*'+str(self.f)
+ requests=str()
+ response = requests.get(apiurl)
+ html=response.content.decode(response.encoding)
+ hasil = int(html)
+ string = "hasil isinya = "
+ i = 1
+ for i in range (1,10):
+ konten = (1,10)
+ items.append(konten)
+ string = string+str(i)
+ i = i + 1
+ self.createfile(string)
+ x = open(self.nfile, "r+")
+ print(x.read())
+ konten = (1,10)
+ items.append(konten)
+
+ def catching(self):
+
+ with condition:
+
+ if len(items) == 10:
+ logging.info('items cathed {}. Stopped'.format(len(items)))
+ condition.wait()
+
+ items.append(1)
+ logging.info('total items {}'.format(len(items)))
+
+ condition.notify()
+
+ def run(self):
+ for i in range(20):
+ time.sleep(1)
+ self.catching()
+
+
+def main():
+ josh_catcher = josua1184091_Catcher(name='Josh_Catcher')
+ josh_trier = josua1184091Write_Trier(name='Josh_Trier')
+
+ josh_catcher.start()
+ josh_trier.start()
+
+ josh_catcher.join()
+ josh_trier.join()
+
+
+if __name__ == "__main__":
+ main()
diff --git a/Chapter02/JosuansefTujuh_1184091.py b/Chapter02/JosuansefTujuh_1184091.py
new file mode 100644
index 0000000..c71dbea
--- /dev/null
+++ b/Chapter02/JosuansefTujuh_1184091.py
@@ -0,0 +1,89 @@
+"""
+Created on Tue Apr 20 16:06:43 2021
+
+@author: Josuansef Pardede (1184091) Tugas 7 DS
+"""
+from threading import Thread, currentThread, Lock, RLock, Event
+from queue import Queue
+import os
+import requests
+
+queue = Queue()
+event = Event()
+
+
+class Josuansef1184091Tujuh_Consumer(Thread):
+
+ def __init__(self,name,thread_number,filenya):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.filenya=os.path.join(os.path.dirname(__file__), filenya)
+ self.event = event
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ print('class Josuansef1184091Tujuh_Consumer eksekusi event.wait lalu clear. Jalankan fungsi Read File')
+ self.event.wait()
+ self.threadLock.acquire()
+ print('read file : '+self.filenya)
+ self.readfile()
+ self.event.wait()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "Read file Done")
+ print ("eksekusi event clear")
+ self.event.clear()
+
+ def readfile(self):
+ q=queue.get()
+ f = open(self.filenya, "r+")
+ print("Cek konten isi file : "+f.read())
+ print("Selesai membaca file, eksekusi queue task done \n")
+ queue.task_done()
+
+class Josuansef1184091Tujuh_Producer(Thread):
+
+ def __init__(self, name,thread_number, alpha, beta, filenya):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.event = event
+ self.rlock = RLock()
+ self.name = name
+ self.alpha=alpha
+ self.beta=beta
+ self.filenya=os.path.join(os.path.dirname(__file__), filenya)
+ self.thread_number = thread_number
+
+ def run(self):
+
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ self.threadLock.acquire()
+ print("threeadlock acquire utama")
+ self.hitung()
+ self.event.set()
+ print('class Josuansef1184091Tujuh_Producer sudah selesai melakukan event set')
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "done")
+
+ def apijj(self):
+ with self.rlock:
+ print('akses web service api...')
+ url='https://api-lk21.herokuapp.com/comingsoon'
+ response = requests.get(url)
+ html=response.json()
+ queue.put(html)
+ self.createfile(html)
+
+ def hitung(self):
+ with self.rlock:
+ print('rlock hitung')
+ self.apijj()
+
+ def createfile(self, konten):
+ print('membuat file baru : '+ self.filenya)
+ f = open(self.filenya, "x")
+ f.write(str(konten))
+ f.close()
+ print('File sudah dibuat, selanjutnya, mau eksekusi event set')
+
diff --git a/Chapter02/Kaisar1184093.py b/Chapter02/Kaisar1184093.py
new file mode 100644
index 0000000..f84b9a1
--- /dev/null
+++ b/Chapter02/Kaisar1184093.py
@@ -0,0 +1,72 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class Kaisar1184093 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ x=randint(1,9)
+ y=randint(1,9)
+ self.pangkat(x, y, 5000000)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def pangkat(self, x, y, z):
+ while z>0:
+ x=x*y/2
+ z=z-1
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Kaisar1184093("Thread#1 ", 1,15)
+ thread2 = Kaisar1184093("Thread#2 ", 2,15)
+ thread3 = Kaisar1184093("Thread#3 ", 3,15)
+ thread4 = Kaisar1184093("Thread#4 ", 4,15)
+ thread5 = Kaisar1184093("Thread#5 ", 5,15)
+ thread6 = Kaisar1184093("Thread#6 ", 6,15)
+ thread7 = Kaisar1184093("Thread#7 ", 7,15)
+ thread8 = Kaisar1184093("Thread#8 ", 8,15)
+ thread9 = Kaisar1184093("Thread#9 ", 9,15)
+
+ # 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/MyThreadClass.py b/Chapter02/MyThreadClass.py
index 45e6fff..e1fc356 100644
--- a/Chapter02/MyThreadClass.py
+++ b/Chapter02/MyThreadClass.py
@@ -57,11 +57,9 @@ def main():
#Execution Time
print("--- %s seconds ---" % (time.time() - start_time))
+ return True
-if __name__ == "__main__":
- main()
-
diff --git a/Chapter02/OkkyTujuh1184087.py b/Chapter02/OkkyTujuh1184087.py
new file mode 100644
index 0000000..f33f6c4
--- /dev/null
+++ b/Chapter02/OkkyTujuh1184087.py
@@ -0,0 +1,78 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Wed Apr 21 14:20:29 2021
+
+@author: Okky Yudistira
+"""
+
+from threading import Thread
+from queue import Queue
+import requests
+import os
+import threading
+
+queue = Queue()
+person = []
+event = threading.Event()
+
+class Person(threading.Thread):
+ def __init__(self, name, thread_number, filename):
+ Thread.__init__(self)
+ self.name = name
+ self.thread_number = thread_number
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "Mulai")
+ self.event.wait()
+ self.threadLock.acquire()
+ print('melakukan baca file : '+self.namafile)
+ self.readfile()
+ self.event.wait()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "Membaca file telah selesai")
+ print ("menjalankan event clear")
+ self.event.clear()
+
+ def readfile(self):
+ queue.get()
+ f = open(self.namafile, "r")
+ print("Membaca isi file pada program Okky7 : "+f.read())
+ queue.task_done()
+
+class okky(threading.Thread):
+ def __init__(self, name, thread_number, a ,filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n" + self.name + "Start!")
+ self.result()
+ g = queue.get()
+ print("\n Queue. . .")
+ print(g)
+ queue.task_done()
+ event.set()
+ print("\n" + threading.currentThread().getName() + "End.")
+
+ def api(self):
+ with self.rlock:
+ print('Webservice Random User')
+ apiurl='https://randomuser.me/api'
+ response = requests.get(apiurl)
+ html=response.json()
+ queue.put(html)
+ self.createfile(html)
+
+ def result(self):
+ with self.rlock:
+ self.api()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
\ No newline at end of file
diff --git a/Chapter02/Parhan1184042.py b/Chapter02/Parhan1184042.py
new file mode 100644
index 0000000..8c9f316
--- /dev/null
+++ b/Chapter02/Parhan1184042.py
@@ -0,0 +1,76 @@
+import time
+import os
+import random
+from random import randint
+from threading import Thread,currentThread
+
+
+class Parhan1184042 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ color_number={'red','yellow','blue','green','black','pink','orange','jingga','Abu abu','blue black','coklat'}
+ self.get_color(1000000)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def get_color(color_number=10):
+ color_number = int(color_number)
+ def do_something(color_number):
+ student_array = []
+ for i in range(1000000):
+ random_color_number = random.randint(0, 4)
+ color = get_color(random_color_number)
+ student_array.append(color)
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Parhan1184042("Thread#1 ", 1,randint(1,10))
+ thread2 = Parhan1184042("Thread#2 ", 2,randint(1,10))
+ thread3 = Parhan1184042("Thread#3 ", 3,randint(1,10))
+ thread4 = Parhan1184042("Thread#4 ", 4,randint(1,10))
+ thread5 = Parhan1184042("Thread#5 ", 5,randint(1,10))
+ thread6 = Parhan1184042("Thread#6 ", 6,randint(1,10))
+ thread7 = Parhan1184042("Thread#7 ", 7,randint(1,10))
+ thread8 = Parhan1184042("Thread#8 ", 8,randint(1,10))
+ thread9 = Parhan1184042("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/ParhanEnam1184042.py b/Chapter02/ParhanEnam1184042.py
new file mode 100644
index 0000000..54a28ef
--- /dev/null
+++ b/Chapter02/ParhanEnam1184042.py
@@ -0,0 +1,55 @@
+from threading import Barrier, Thread
+import os
+import requests
+from time import ctime, sleep
+
+angka = 1
+b = Barrier(angka)
+file = "parhan"
+filename = os.path.join(os.path.dirname(__file__), file)
+
+def apiwebservice():
+ apiurl='https://api.kawalcorona.com/indonesia'
+ response = requests.get(apiurl)
+ html=response.json()
+ manajemenfile(html)
+
+def manajemenfile(isi):
+ print('buat file terlebih dahulu : '+file+'.txt \n')
+ f = open(filename+".txt", "w")
+ print('File telah berhasil dibuat \n')
+ f.write(str(isi))
+ f.close()
+ print('Isi file tersebut adalah: '+file+'.txt')
+ x = open(filename+".txt", "r")
+ print(x.read()+'\n')
+ x.close()
+ print('coba hapus file \n')
+ print('Hapus File: '+file+'.txt \n')
+ os.remove(filename+'.txt')
+ sleep(2)
+ b.wait()
+ print('File:'+file+'.txt'+' telah berhasil di Hapus \n')
+ sleep(2)
+ b.wait()
+ print('Create, Read, and delete file: '+file+' mencapai barrier pada waktu: %s \n' % (ctime()))
+
+def run():
+ apiwebservice()
+ sleep(2)
+ b.wait()
+ print('Semua mencapai barrier pada waktu: %s \n' % (ctime()))
+
+def main():
+ threads = []
+ print('Semoga berhasil')
+ for i in range(angka):
+ threads.append(Thread(target=run))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Success')
+ return True
+
+# if __name__ == "__main__":
+# main()
\ No newline at end of file
diff --git a/Chapter02/ParhanTujuh1184042.py b/Chapter02/ParhanTujuh1184042.py
new file mode 100644
index 0000000..aa19666
--- /dev/null
+++ b/Chapter02/ParhanTujuh1184042.py
@@ -0,0 +1,87 @@
+from threading import Thread,currentThread, Lock, RLock, Event
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = Event()
+
+class parhanmanajemenFile (Thread):
+ def __init__(self,name,thread_number,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.event = event
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ print('pada class parhanmanajemenFile jalankan event wait lalu clear untuk read dan rename file')
+ self.event.wait()
+ self.threadLock.acquire()
+ print('membaca file : '+self.namafile)
+ self.readfile()
+ print('merename file : '+self.namafile)
+ self.event.wait()
+ self.renamefile()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "Baca file dan delete file selesai")
+ print ("jalankan event clear")
+ self.event.clear()
+
+ def readfile(self):
+ q=queue.get()
+ f = open(self.namafile, "r")
+ print("isi file yang di read : "+f.read())
+ print("setelah baca file, jalankan queue task done \n")
+ queue.task_done()
+
+ def renamefile(self):
+ print('ganti nama file \n')
+ os.rename(self.namafile,self.namafile+'.txt')
+ print("nama file telah diganti \n")
+
+class parhanTujuh1184042 (Thread):
+ def __init__(self, name,thread_number,a,b ,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.event = event
+ self.rlock = RLock()
+ self.name = name
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.thread_number = thread_number
+ self.a=a
+ self.b=b
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ self.threadLock.acquire()
+ print("threeadlock acquire utama")
+ self.jumlah()
+ self.event.set()
+ print('class parhanTujuh1184042 selesai melakukan event set')
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def apiservice(self):
+ with self.rlock:
+ print('didalam rlock apiservice, akses web service...')
+ apiurl='https://gempa-api-zhirrr.vercel.app/api/gempa'
+ response = requests.get(apiurl)
+ z=response.json()
+ queue.put(z)
+ self.createfile(z)
+
+ def jumlah(self):
+ with self.rlock:
+ print('rlock jumlah')
+ self.apiservice()
+
+ def createfile(self,isi):
+ print('buat file baru : '+ self.namafile)
+ f = open(self.namafile, "x")
+ f.write(str(isi))
+ f.close()
+ print('sudah buat file baru, jalankan event set')
+
diff --git a/Chapter02/README.md b/Chapter02/README.md
new file mode 100644
index 0000000..bb03abc
--- /dev/null
+++ b/Chapter02/README.md
@@ -0,0 +1,42 @@
+# Kriteria Penilaian
+1. Fork hanya boleh sekali seumur hidup jika anda melakukan fork lagi maka diskon nilai 50 persen
+2. Selain di merge request ke branch 2021 maka merge akan ditolak
+3. Jika tidak pass hijau dari travis CI, maka merge ditolak
+4. Jika konflik, maka merge ditolak
+5. Jika melakukan perubahan selain file yang dikerjakan, maka merge ditolak
+6. Jika menghapus atau merubah pekerjaan orang lain, maka merge ditolak
+7. Merge ditolak, maka nilai nol
+8. Total nilai akan dikalikan dengan persentasi code coverage dari file(bukan total) yang dibuat hasil dari Travis CI
+
+# Tugas pertemuan 2
+1. Buatlah kelas thread dengan format NamaNPM.py yang mengandung arg dan name juga di dalam kelas tersebut
+2. fungsi yang kemaren dibuat dimasukkan ke kelas tersebut jadi bukan hanya sleep/ sleep harus di ganti sama proses yang lain > 5 detik
+3. fungsi main di panggil di file test_app.py dengan format yang sudah di contohkan
+4. Dikumpulkan dengan cara merge request ke branch 2021
+
+# Tugas pertemuan 3
+1. Buatlah kelas thread dalam file NamaDuaNPM.py
+2. Buatlah dua kelas thread untuk implementasi semaphore
+3. method method yang ada di dalam kelas tersebut mengimplementasikan web service dan manaajemen file
+4. Tidak boleh meniru logika yang dicontohkan, harus orisinil logika harus berbeda dengan contoh, tidak boleh hanya edit edit saja isi method rollyDua113040087
+5. Tulis pada test_app dengan mengecek isi file yang telah di buat, diubah dan di rename. Nilai di dalam file di assert
+6. Pull request maximal jumat jam 11
+7. Dikumpulkan dengan cara merge request ke branch 2021, judul diisi nama npm, deskripsi diisi penjelasan jelas bagaimana kelas kelas dan method tersebut bekerja. dijelaskan per poin
+
+# Tugas pertemuan 4
+1. Buatlah kelas thread dalam file NamaTigaNPM.py
+2. Buatlah dua kelas thread untuk implementasi: NPM mod 2 = 0 thread with event, 1 thread with condition
+3. method method yang ada di dalam kelas tersebut mengimplementasikan web service dan manaajemen file
+4. Tidak boleh meniru logika yang dicontohkan, harus orisinil logika harus berbeda dengan contoh
+5. Tulis pada test_app dengan mengecek isi file yang telah di buat, diubah dan di rename. Nilai di dalam file di assert
+6. Pull request maximal jumat jam 11 siang
+7. Dikumpulkan dengan cara merge request ke branch 2021, judul merge request diisi nama npm, deskripsi diisi penjelasan jelas bagaimana kelas dan method tersebut bekerja. dijelaskan per poin
+
+# Tugas pertemuan 6
+1. Buatlah fungsi thread dengan barrier dalam file NamaEnamNPM.py
+2. Minimal ada dua fungsi(dianjurkan lebih banyak), yaitu threadnya dan fungsi main nya
+3. Fungsi tersebut mengimplementasikan web service/manajemen file/variabel global
+4. Tidak boleh meniru logika teman sekelas, harus orisinil logika harus berbeda dengan temannya
+5. Tulis pada test_app dengan mengecek isi file yang telah di buat, diubah dan di rename. Nilai di dalam file di assert
+6. Pull request maximal jumat jam 11 siang
+7. Dikumpulkan dengan cara merge request ke branch 2021, judul merge request diisi nama npm, deskripsi diisi penjelasan jelas bagaimana fungsi fungsi tersebut bekerja. dijelaskan per poin
\ No newline at end of file
diff --git a/Chapter02/Rizaluardi1184102.py b/Chapter02/Rizaluardi1184102.py
new file mode 100644
index 0000000..87d5635
--- /dev/null
+++ b/Chapter02/Rizaluardi1184102.py
@@ -0,0 +1,72 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class Rizaluardi1184102 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ num=randint(3,12)
+ nom=randint(3,12)
+ self.rank(num, nom, 6600000)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def rank(self, num, nom, hasil):
+ while hasil>0:
+ num=num*nom/3
+ hasil=hasil-1
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Rizaluardi1184102("Thread#1 ", 1,12)
+ thread2 = Rizaluardi1184102("Thread#2 ", 2,12)
+ thread3 = Rizaluardi1184102("Thread#3 ", 3,12)
+ thread4 = Rizaluardi1184102("Thread#4 ", 4,12)
+ thread5 = Rizaluardi1184102("Thread#5 ", 5,12)
+ thread6 = Rizaluardi1184102("Thread#6 ", 6,12)
+ thread7 = Rizaluardi1184102("Thread#7 ", 7,12)
+ thread8 = Rizaluardi1184102("Thread#8 ", 8,12)
+ thread9 = Rizaluardi1184102("Thread#9 ", 9,12)
+
+ # 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/RizaluardiEnam1184102.py b/Chapter02/RizaluardiEnam1184102.py
new file mode 100644
index 0000000..e2fe3d4
--- /dev/null
+++ b/Chapter02/RizaluardiEnam1184102.py
@@ -0,0 +1,54 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Wed Apr 14 13:48:09 2021
+
+@author: Rizaluardi
+"""
+from threading import Barrier, Thread
+import os
+import requests
+from time import ctime, sleep
+
+kpos = 5
+bar = Barrier(kpos)
+file = "list kodepos"
+filename = os.path.join(os.path.dirname(file), file)
+
+def webapiservice():
+ apiurl=' https://kodepos-2d475.firebaseio.com/list_propinsi.json?print=pretty'
+ response = requests.get(apiurl)
+ html=response.json()
+ buatkodepos(html)
+
+def buatkodepos(isi):
+ for i in range(1):
+ print('Sebentar sedang membuat file bernama : '+file+'.txt \n')
+ k = open("Kodepos.txt", "w")
+ print('Oke file telah dibuat \n')
+ k.write(str(isi))
+ k.close()
+ k = open("Kodepos.txt", "r")
+ print("Berikut adalah data kodeposnya : \n" +k.read())
+ print('Create, Read this file: '+file+' telah mencapai barrier pada waktu: %s \n' % (ctime()))
+
+def eksekusi():
+ webapiservice()
+ sleep(2)
+ bar.wait()
+ print('All reached the barrier at: %s \n' % (ctime()))
+
+### In[]:
+
+def main():
+ threads = []
+ print('Tunggu sejenak')
+ for i in range(kpos):
+ threads.append(Thread(target=eksekusi))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Over!')
+ return True
+
+#if __name__ == "__main__":
+# main()
\ No newline at end of file
diff --git a/Chapter02/RizaluardiTujuh1184102.py b/Chapter02/RizaluardiTujuh1184102.py
new file mode 100644
index 0000000..480f51a
--- /dev/null
+++ b/Chapter02/RizaluardiTujuh1184102.py
@@ -0,0 +1,90 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu Apr 22 21:39:32 2021
+
+@author: Rizaluardi
+"""
+import threading
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = threading.Event()
+
+class RizaluardiIms(threading.Thread):
+ def __init__(self,name,filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n" + self.name + "mulai.")
+ self.threadLock.acquire()
+ print('Nama_Provinsi_Di_Indonesia')
+ event.wait()
+ print('ubah : '+self.filename)
+ self.bacafile()
+ print("data provinsi berubah")
+ self.buatfile()
+ event.clear()
+ self.threadLock.release()
+ print("\n" + threading.currentThread().getName() + "bekerja!")
+
+ def bacafile(self):
+ f = open(self.filename, "r")
+ print("isi filenya : \n "+f.read())
+
+ def buatfile(self):
+ f = open(self.filename, "r")
+ buat = open(self.filename+'.txt', "w")
+ for line in f:
+ buat.write(line.replace('provinsi', 'nama'))
+ buat.close()
+ new = open(self.filename+'.txt', "r")
+ print(new.read())
+ new.close()
+
+class RizaluardiTujuh1184102(threading.Thread):
+ def __init__(self, name, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+ self.name + " mulai.")
+ self.main()
+ qj = queue.get()
+ print("\n antrian queuenya")
+ print(qj)
+ queue.task_done()
+ event.set()
+ print("\n" + threading.currentThread().getName() + "selesai.")
+
+ def webapiprovinsi(self):
+ with self.rlock:
+ print('Webservice Api Jadwal Imsyak')
+ apiurl='https://dev.farizdotid.com/api/daerahindonesia/provinsi'
+ response = requests.get(apiurl)
+ html=response.json()
+ string = ""
+ for i in range(len(html["provinsi"])):
+ baru = html["provinsi"][i]
+ new = "\n"+str(i)+". "
+ string = string+new+str(baru)
+ queue.put(string)
+ self.createfile(string)
+
+ def main(self):
+ with self.rlock:
+ self.webapiprovinsi()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
+
\ No newline at end of file
diff --git a/Chapter02/Rolly113040087.py b/Chapter02/Rolly113040087.py
new file mode 100644
index 0000000..46e3ceb
--- /dev/null
+++ b/Chapter02/Rolly113040087.py
@@ -0,0 +1,71 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class Rolly113040087 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ time.sleep(self.duration)
+ out_list = list()
+ self.do_something(1000000,out_list)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def do_something(self,count,out_list):
+ for i in range(count):
+ out_list.append(randint)
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Rolly113040087("Thread#1 ", 1,randint(1,10))
+ thread2 = Rolly113040087("Thread#2 ", 2,randint(1,10))
+ thread3 = Rolly113040087("Thread#3 ", 3,randint(1,10))
+ thread4 = Rolly113040087("Thread#4 ", 4,randint(1,10))
+ thread5 = Rolly113040087("Thread#5 ", 5,randint(1,10))
+ thread6 = Rolly113040087("Thread#6 ", 6,randint(1,10))
+ thread7 = Rolly113040087("Thread#7 ", 7,randint(1,10))
+ thread8 = Rolly113040087("Thread#8 ", 8,randint(1,10))
+ thread9 = Rolly113040087("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/VickyEnam1184037.py.py b/Chapter02/VickyEnam1184037.py.py
new file mode 100644
index 0000000..9b6096f
--- /dev/null
+++ b/Chapter02/VickyEnam1184037.py.py
@@ -0,0 +1,63 @@
+from threading import Barrier, Thread
+import requests
+from time import ctime, sleep
+import os
+
+
+angka = 1
+finish = Barrier(angka)
+yuhu = []
+
+
+def webservices():
+ apiurl = 'https://koreanjson.com/users'
+ response = requests.get(apiurl)
+ html = response.json()
+ for i in range(len(html)):
+ link = html[i]["username"]
+ yuhu.append(link)
+
+ createfile(yuhu)
+
+def readfile():
+ f = open("vicky.txt", "r")
+ print("File content: " +f.read())
+ f.close()
+
+
+def createfile(isi):
+ print('Create File : vicky.txt')
+ f = open("vicky.txt", "w")
+ f.write(str(isi))
+ f.close()
+
+def rename():
+ os.rename("vicky.txt", "safira.txt")
+
+
+def run():
+ webservices()
+ sleep(3)
+ name = yuhu.pop()
+ print('%s reached the barrier at: %s \n' % (name, ctime()))
+ finish.wait()
+
+ readfile()
+ rename()
+
+
+def main():
+ baru = []
+ print("\n Mulai \n")
+ for i in range(angka):
+ baru.append(Thread(target=run))
+ baru[-1].start()
+ for thread in baru:
+ thread.join()
+ print("Selesai")
+ return True
+
+#if __name__ == "__main__":
+# main()
+
+
diff --git a/Chapter02/VickyTujuh1184037.py b/Chapter02/VickyTujuh1184037.py
new file mode 100644
index 0000000..3064bb9
--- /dev/null
+++ b/Chapter02/VickyTujuh1184037.py
@@ -0,0 +1,52 @@
+from threading import Thread
+from queue import Queue
+import os
+import requests
+
+queue = Queue()
+lagi = []
+apiurl ='https://www.thecocktaildb.com/api/json/v1/1/list.php?i=list'
+response = requests.get(apiurl)
+html=response.json()
+
+class vickysaf(Thread):
+ def __init__(self, namathread, filename):
+ Thread.__init__(self)
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+ self.namathread = namathread
+
+ def mainapi(self):
+ string = "nama kota:"
+ for i in range(len(html["drinks"])):
+ sini = html["drinks"][i]["strIngredient1"]
+ nomor = "\n"+str(i)+". "
+ string = string + nomor + sini
+ lagi.append(sini)
+ queue.put(lagi)
+ print(str(i)+". %s yg di append" % (sini))
+ self.createfile(string)
+ self.belakang()
+
+ def run(self):
+ print("Dimulai")
+ self.mainapi()
+ print("Selesai")
+
+ def createfile(self, isi):
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
+ def read(self):
+ x = open(self.filename, "r")
+ print(x.read())
+ x.close()
+
+ def belakang(self):
+ kebelakang = queue.get()
+ dur = len(kebelakang)
+ while dur > 0:
+ print("hasil pop")
+ dur = dur -1
+ self.read()
+ queue.task_done()
\ No newline at end of file
diff --git a/Chapter02/WahyuKurniaSariEnam1184001.py b/Chapter02/WahyuKurniaSariEnam1184001.py
new file mode 100644
index 0000000..39f8e5b
--- /dev/null
+++ b/Chapter02/WahyuKurniaSariEnam1184001.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Fri Apr 16 05:28:16 2021
+
+@author: NITRO 5 ACER
+"""
+
+import threading
+import time
+import random
+import requests
+
+br=threading.Barrier(2)
+
+def webservices():
+ apiurl = 'https://pokeapi.co/api/v2/pokemon/pikachu'
+ response = requests.get(apiurl)
+ html = response.json()
+ print(html["weight"])
+
+
+def run():
+ webservices()
+ br.wait()
+
+def main():
+
+ threads = []
+
+ for i in range(4):
+ thread = threading.Thread(target=run)
+ thread.start()
+ threads.append(thread)
+
+ for t in threads:
+ t.join()
+ return True
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Chapter02/WahyuKurniasariDua1184001.py b/Chapter02/WahyuKurniasariDua1184001.py
new file mode 100644
index 0000000..9348092
--- /dev/null
+++ b/Chapter02/WahyuKurniasariDua1184001.py
@@ -0,0 +1,87 @@
+from threading import Thread,currentThread, Lock, RLock, Semaphore
+import requests
+import os
+
+semaphore = Semaphore(0)
+
+class WahyuKurniaSariSemaphoreDeleteFile (Thread):
+ def __init__(self,name,threadNumber,filename):
+ Thread.__init__(self)
+ self.lock = Lock()
+ self.name = name
+ self.threadNumber = threadNumber
+ self.filename = filename
+ self.filelocation=os.path.join(os.path.dirname(__file__), filename)
+ self.semaphore = semaphore
+
+ def run(self):
+ print("\n"+str(self.threadNumber)+". menjalankan " + self.name )
+ self.lock.acquire()
+ self.readAndRename()
+ self.lock.release()
+ print("\n"+str(self.threadNumber)+". " + currentThread().getName() + " selesai")
+
+ def readAndRename(self):
+ self.semaphore.acquire()
+ self.renamefile()
+
+ def renamefile(self):
+ os.rename(self.filelocation,self.filelocation+'.txt')
+
+class WahyuKurniaSariDua1184001 (Thread):
+ def __init__(self, name,threadNumber, pokemon,filename):
+ Thread.__init__(self)
+ self.lock = Lock()
+ self.semaphore = semaphore
+ self.rlock = RLock()
+ self.name = name
+ self.pokemon = pokemon
+ self.filename = filename
+ self.filelocation=os.path.join(os.path.dirname(__file__), filename)
+ self.threadNumber = threadNumber
+
+ def run(self):
+ print("\n"+str(self.threadNumber)+". menjalankan " + self.name )
+ self.lock.acquire()
+ self.getWeight()
+ self.lock.release()
+ print("\n"+str(self.threadNumber)+". " + currentThread().getName() + " selesai")
+
+ def getWeight(self):
+ with self.rlock:
+ print("mencari pokemon : " + self.pokemon)
+ apiurl='https://pokeapi.co/api/v2/pokemon/'+self.pokemon
+ response = requests.get(apiurl)
+ try:
+ html=response.json()
+ hasil = html["weight"]
+ print(self.pokemon+" ditemukan, pokemon ini memiliki berat "+ str(hasil))
+ except:
+ print(self.pokemon+" tidak ditemukan")
+ hasil = "0"
+ self.createFile(hasil)
+ self.readFile()
+
+ def readFile(self):
+ f = open(self.filelocation, "r")
+ print("Isi Filenya : "+f.read())
+ self.semaphore.release()
+
+ def createFile(self,isi):
+ print('membuat file baru "'+ self.filename +'"')
+ f = open(self.filelocation, "x")
+ f.write(str(isi))
+ f.close()
+
+
+ def getFileLocation(self):
+ return self.filelocation+".txt"
+
+ def getFileContent(self):
+ f = open(self.filelocation+".txt", "r")
+ return int(f.read())
+
+
+
+
+
diff --git a/Chapter02/Zanwar1184050.py b/Chapter02/Zanwar1184050.py
new file mode 100644
index 0000000..420c0f9
--- /dev/null
+++ b/Chapter02/Zanwar1184050.py
@@ -0,0 +1,67 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class Zanwar1184050 (Thread):
+ def __init__(self, name,thread_number):
+ Thread.__init__(self)
+ self.name = name
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ self.urut_angka("", randint(1,99), randint(1,99), 500000)
+ print (str(self.thread_number)+". ---> " + self.name + " over.")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def urut_angka(self, string, x, y, i):
+ string = ""
+ while i>0:
+ z = (x**2 + y**2)
+ z = z ** 0.5
+ string = string+str(z)
+ i = i-1
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = Zanwar1184050("Thread#1 ", 1)
+ thread2 = Zanwar1184050("Thread#2 ", 2)
+ thread3 = Zanwar1184050("Thread#3 ", 3)
+ thread4 = Zanwar1184050("Thread#4 ", 4)
+ thread5 = Zanwar1184050("Thread#5 ", 5)
+ thread6 = Zanwar1184050("Thread#6 ", 6)
+ thread7 = Zanwar1184050("Thread#7 ", 7)
+ thread8 = Zanwar1184050("Thread#8 ", 8)
+ thread9 = Zanwar1184050("Thread#9 ", 9)
+
+ # 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))
+ return True
\ No newline at end of file
diff --git a/Chapter02/ZanwarDua1184050.py b/Chapter02/ZanwarDua1184050.py
new file mode 100644
index 0000000..2ef54d8
--- /dev/null
+++ b/Chapter02/ZanwarDua1184050.py
@@ -0,0 +1,92 @@
+from threading import Thread,currentThread, Lock, RLock, Semaphore
+import requests
+import os
+
+semaphore = Semaphore(0)
+hasilperhitungan=0
+
+class zanwarRewrite (Thread):
+ def __init__(self,name,thread_number,filename):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+ self.semaphore = semaphore
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ print('semaphore acquire')
+ self.threadLock.acquire()
+ self.semaphore.acquire()
+ print('rewrite : '+self.filename)
+ self.rewritefile()
+ self.threadLock.release()
+ print("Read setelah rewrite")
+ self.readfile()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "end")
+
+ def readfile(self):
+ f = open(self.filename, "r")
+ print("File's Content is : \n "+f.read())
+
+ def rewritefile(self):
+ f = open(self.filename, "r")
+ fd = open(self.filename+'.txt', "w")
+ for line in f:
+ fd.write(line.replace('Angka : ', ''))
+ fr = open(self.filename+'.txt', "r")
+ print(fr.read())
+
+
+class zanwarDua1184050 (Thread):
+ def __init__(self, name,thread_number,a,b ,filename):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.semaphore = semaphore
+ self.rlock = RLock()
+ self.name = name
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+ self.thread_number = thread_number
+ self.a=a
+ self.b=b
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.threadLock.acquire()
+ print("main threadlock acquire")
+ self.count()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "finish.")
+
+ def randomapi(self):
+ with self.rlock:
+ print('Inside rlock apipangkat, akses web service...')
+ apiurl='https://www.random.org/integers/?num=1&'
+ eq='min='+str(self.a)+'&max='+str(self.b)
+ form = '&col=1&base=10&format=plain&rnd=new'
+ response = requests.get(apiurl+eq+form)
+ html=response.content.decode(response.encoding)
+ hasil = int(html)
+ string = "Angka : "
+ i = 1
+ while i <= hasil:
+ string = string+str(i)
+ i = i +1
+ self.createfile(string)
+ x = open(self.filename, "r")
+ print(x.read())
+
+ def count(self):
+ with self.rlock:
+ self.randomapi()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+ print('Create File')
+ self.semaphore.release()
+ print('Semaphore released.')
+
\ No newline at end of file
diff --git a/Chapter02/ZanwarEnam1184050.py b/Chapter02/ZanwarEnam1184050.py
new file mode 100644
index 0000000..a45f1c7
--- /dev/null
+++ b/Chapter02/ZanwarEnam1184050.py
@@ -0,0 +1,57 @@
+from threading import Barrier, Thread
+import os
+import requests
+from time import ctime, sleep
+
+kali = 1
+barr = Barrier(kali)
+file = "za"
+filename = os.path.join(os.path.dirname(__file__), file)
+
+def randomapi():
+ apiurl='https://goquotes-api.herokuapp.com/api/v1/random?'
+ eq='count='+str(kali)
+ response = requests.get(apiurl+eq)
+ html=response.json()
+ string = "Quotes : "
+ for i in range(len(html["quotes"])):
+ hasil = html["quotes"][i]["text"]
+ tulis = "\n"+str(i)+". "
+ string = string+tulis+str(hasil)
+ createfile(string)
+
+def createfile(isi):
+ for i in range(3):
+ print('Create File : '+file+str(i)+'.txt \n')
+ f = open(filename+str(i)+".txt", "w")
+ f.write(str(isi))
+ f.close()
+ print('Read File: '+file+str(i)+'.txt')
+ x = open(filename+str(i)+".txt", "r")
+ print(x.read()+'\n')
+ x.close()
+ print('Create & Read '+file+str(i)+' reached the barrier at: %s \n' % (ctime()))
+ sleep(2)
+ barr.wait()
+
+def run():
+ randomapi()
+ sleep(2)
+ barr.wait()
+ print('All reached the barrier at: %s \n' % (ctime()))
+
+### In[]:
+
+def main():
+ threads = []
+ print('START!')
+ for i in range(kali):
+ threads.append(Thread(target=run))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Over!')
+ return True
+
+#if __name__ == "__main__":
+# main()
\ No newline at end of file
diff --git a/Chapter02/ZanwarTiga1184050.py b/Chapter02/ZanwarTiga1184050.py
new file mode 100644
index 0000000..b80f119
--- /dev/null
+++ b/Chapter02/ZanwarTiga1184050.py
@@ -0,0 +1,86 @@
+import threading
+import requests
+import os
+
+event = threading.Event()
+
+class zanwarRewrite (threading.Thread):
+ def __init__(self,name, thread_number, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.thread_number = thread_number
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.threadLock.acquire()
+ print('A: Pls give me some motivation :(')
+ print('B: Okay, pls wait~')
+ event.wait()
+ print('rewrite : '+self.filename)
+ self.readfile()
+ print("Read after rewrite")
+ self.rewritefile()
+ event.clear()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "end")
+
+ def readfile(self):
+ f = open(self.filename, "r")
+ print("File's Content is : \n "+f.read())
+
+ def rewritefile(self):
+ f = open(self.filename, "r")
+ fd = open(self.filename+'.txt', "w")
+ for line in f:
+ fd.write(line.replace('Quotes', 'Kata Kata Bijak'))
+ fd.close()
+ fr = open(self.filename+'.txt', "r")
+ print(fr.read())
+ fr.close()
+
+
+class zanwarTiga1184050 (threading.Thread):
+ def __init__(self, name, thread_number, a ,filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+ self.thread_number = thread_number
+ self.a=a
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.count()
+ print("Event Set!")
+ event.set()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "finish.")
+
+ def randomapi(self):
+ with self.rlock:
+ print('Inside rlock apipangkat, akses web service...')
+ apiurl='https://goquotes-api.herokuapp.com/api/v1/random?'
+ eq='count='+str(self.a)
+ response = requests.get(apiurl+eq)
+ html=response.json()
+ string = "Quotes : "
+ for i in range(len(html["quotes"])):
+ hasil = html["quotes"][i]["text"]
+ tulis = "\n"+str(i)+". "
+ string = string+tulis+str(hasil)
+ self.createfile(string)
+ x = open(self.filename, "r")
+ print(x.read())
+
+ def count(self):
+ with self.rlock:
+ self.randomapi()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
\ No newline at end of file
diff --git a/Chapter02/ZanwarTujuh1184050.py b/Chapter02/ZanwarTujuh1184050.py
new file mode 100644
index 0000000..de23afc
--- /dev/null
+++ b/Chapter02/ZanwarTujuh1184050.py
@@ -0,0 +1,100 @@
+import threading
+import requests
+from queue import Queue
+import os
+
+event = threading.Event()
+queue = Queue()
+
+class zanwarRewrite (threading.Thread):
+ def __init__(self, name, thread_number, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.thread_number = thread_number
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.threadLock.acquire()
+ print('A: Pls give me some motivation :(')
+ print('B: Okay, pls wait~')
+ event.wait()
+ print('rewrite : '+self.filename)
+ self.readfile()
+ print("Read after rewrite")
+ self.rewritefile()
+ event.clear()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "end")
+
+ def readfile(self):
+ tulis = queue.get()
+ while tulis > 0:
+ f = open(self.filename, "r")
+ baca = f.readlines()
+ print('\n'+str(tulis)+' Tulis Readed from queue by %s' % ( self.name))
+ print(baca[tulis])
+ f.close()
+ tulis = tulis - 1
+ queue.task_done()
+
+ def rewritefile(self):
+ f = open(self.filename, "r")
+ fd = open(self.filename+'.txt', "w")
+ for line in f:
+ fd.write(line.replace('Quotes', 'Kata Kata Bijak'))
+ fd.close()
+ fr = open(self.filename+'.txt', "r")
+ print(fr.read())
+ fr.close()
+
+
+class zanwarTujuh1184050 (threading.Thread):
+ def __init__(self, name, thread_number, a ,filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+ self.thread_number = thread_number
+ self.a=a
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "start.")
+ self.count()
+ print("Event Set!")
+ event.set()
+ print("\n"+str(self.thread_number)+". ---> " + threading.currentThread().getName() + "finish.")
+
+ def randomapi(self):
+ with self.rlock:
+ print('Akses web service...')
+ apiurl='https://goquotes-api.herokuapp.com/api/v1/random?'
+ eq='count='+str(self.a)
+ response = requests.get(apiurl+eq)
+ html=response.json()
+ string = "Quotes : "
+ global x
+ x = self.a
+ for i in range(len(html["quotes"])):
+ hasil = html["quotes"][i]["text"]
+ tulis = "\n"+str(i)+". "
+ string = string+tulis+str(hasil)
+ queue.put(x)
+ print('%d Writed from queue by %s' % (x, self.name))
+ x = x-1
+ self.createfile(string)
+ # x = open(self.filename, "r")
+ # print(x.read())
+
+ def count(self):
+ with self.rlock:
+ self.randomapi()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
\ No newline at end of file
diff --git a/Chapter02/__init__.py b/Chapter02/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/Chapter02/alifTiga1184068.py b/Chapter02/alifTiga1184068.py
new file mode 100644
index 0000000..bda3781
--- /dev/null
+++ b/Chapter02/alifTiga1184068.py
@@ -0,0 +1,80 @@
+from threading import Thread,currentThread, Lock, RLock, Event
+import requests
+import os
+
+event = Event()
+hasilperhitungan=0
+
+class alifEventDeleteFile (Thread):
+ def __init__(self,name,thread_number,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.event = event
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ print('class alifEventDeleteFile mau menjalankan event wait dilanjutkan clear untuk baca dan delete file')
+ self.event.wait()
+ self.threadLock.acquire()
+ print('melakukan baca file : '+self.namafile)
+ self.readfile()
+ print('melakukan rename file : '+self.namafile)
+ self.renamefile()
+ self.event.wait()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "Baca file dan delete file selesai")
+ print ("menjalankan event clear")
+ self.event.clear()
+
+ def readfile(self):
+ f = open(self.namafile, "r")
+ print("Isi Filenya : "+f.read())
+
+ def renamefile(self):
+ os.rename(self.namafile,self.namafile+'.txt')
+
+class alifTiga1184068 (Thread):
+ def __init__(self, name,thread_number,a,b ,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.event = event
+ self.rlock = RLock()
+ self.name = name
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.thread_number = thread_number
+ self.a=a
+ self.b=b
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ self.threadLock.acquire()
+ print("threeadlock acquire utama")
+ self.hitung()
+ self.event.set()
+ print('class alifTiga1184068 sudah selesai melakukan event set')
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def apipangkat(self):
+ with self.rlock:
+ print('didalam rlock apipangkat, akses web service...')
+ apiurl='http://www.emsifa.com/api-wilayah-indonesia/api/provinces.json'
+ response = requests.get(apiurl)
+ html=response.json()
+ self.createfile(html)
+
+ def hitung(self):
+ with self.rlock:
+ print('rlock hitung')
+ self.apipangkat()
+
+ def createfile(self,isi):
+ print('membuat file baru : '+ self.namafile)
+ f = open(self.namafile, "x")
+ f.write(str(isi))
+ f.close()
+ print('sudah membuat file baru, mau menjalankan event set')
+
diff --git a/Chapter02/baharandili1184002.py b/Chapter02/baharandili1184002.py
new file mode 100644
index 0000000..688a81f
--- /dev/null
+++ b/Chapter02/baharandili1184002.py
@@ -0,0 +1,81 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Tue Mar 16 11:01:27 2021
+
+@author: HP
+"""
+
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class baharandili1184002 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ a = 1
+ b = 4
+ n = 0
+ self.aritmatika(a,b,n)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+
+ def aritmatika(self, a, b, n):
+ for n in range (2500000):
+ n = n + 1
+ a = a + b
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = baharandili1184002("Thread#1 ", 1,randint(1,10))
+ thread2 = baharandili1184002("Thread#2 ", 2,randint(1,10))
+ thread3 = baharandili1184002("Thread#3 ", 3,randint(1,10))
+ thread4 = baharandili1184002("Thread#4 ", 4,randint(1,10))
+ thread5 = baharandili1184002("Thread#5 ", 5,randint(1,10))
+ thread6 = baharandili1184002("Thread#6 ", 6,randint(1,10))
+ thread7 = baharandili1184002("Thread#7 ", 7,randint(1,10))
+ thread8 = baharandili1184002("Thread#8 ", 8,randint(1,10))
+ thread9 = baharandili1184002("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/baharenam1184002.py b/Chapter02/baharenam1184002.py
new file mode 100644
index 0000000..64bcff3
--- /dev/null
+++ b/Chapter02/baharenam1184002.py
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu Apr 15 18:20:47 2021
+
+@author: HP
+"""
+
+from threading import Barrier, Thread
+import os
+import requests
+from time import ctime, sleep
+
+jika = 1
+br = Barrier(jika)
+file = "list film"
+filename = os.path.join(os.path.dirname(__file__), file)
+
+def webapi():
+ apiurl='https://bioskop-api-zahirr.herokuapp.com/api/now-playing'
+ response = requests.get(apiurl)
+ html=response.json()
+ buatfile(html)
+
+def buatfile(isi):
+ for i in range(1):
+ print('buat file dulu gan, namanya : '+file+'.txt \n')
+ f = open("Film.txt", "w")
+ print('File sudah berhasil dibuat gan \n')
+ f.write(str(isi))
+ f.close()
+ f = open("Film.txt", "r")
+ print("ini adalah isi filenya gan: \n" +f.read())
+ print('Create, Read this file: '+file+' mencapai barrier pada waktu: %s \n' % (ctime()))
+
+def jalan():
+ webapi()
+ sleep(2)
+ br.wait()
+ print('All reached the barrier at: %s \n' % (ctime()))
+
+### In[]:
+
+def main():
+ threads = []
+ print('Bentar Gann')
+ for i in range(jika):
+ threads.append(Thread(target=jalan))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('Over!')
+ return True
+
+#if __name__ == "__main__":
+# main()
\ No newline at end of file
diff --git a/Chapter02/bahartujuh1184002.py b/Chapter02/bahartujuh1184002.py
new file mode 100644
index 0000000..f0a3c3c
--- /dev/null
+++ b/Chapter02/bahartujuh1184002.py
@@ -0,0 +1,89 @@
+import threading
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = threading.Event()
+
+class BaharQue(threading.Thread):
+ def __init__(self,name, filename):
+ threading.Thread.__init__(self)
+ self.name = name
+ self.threadLock = threading.Lock()
+ self.filename = os.path.join(os.path.dirname(__file__), filename)
+
+ def run(self):
+ print("\n"+ self.name + "mulai.")
+ self.threadLock.acquire()
+ print('kodepos')
+ event.wait()
+ print('ubah : '+self.filename)
+ self.baca()
+ print("kode sudah diubah")
+ self.buat()
+ event.clear()
+ self.threadLock.release()
+ print("\n" + threading.currentThread().getName() + "berhasil")
+
+ def baca(self):
+ f = open(self.filename, "r")
+ print("isi file : \n "+f.read())
+
+ def buat(self):
+ f = open(self.filename, "r")
+ bikin = open(self.filename+'.txt', "w")
+ for line in f:
+ bikin.write(line.replace('kodepos', 'kelurahan'))
+ bikin.close()
+ new = open(self.filename+'.txt', "r")
+ print(new.read())
+ new.close()
+
+
+class Bahartujuh1184002 (threading.Thread):
+ def __init__(self, name, filename):
+ threading.Thread.__init__(self)
+ self.threadLock = threading.Lock()
+ self.name = name
+ self.rlock = threading.RLock()
+ self.filename=os.path.join(os.path.dirname(__file__), filename)
+
+
+ def run(self):
+ print("\n"+ self.name + " mulai.")
+ self.main()
+ cb = queue.get()
+ print("\n queuenya")
+ print(cb)
+ queue.task_done()
+ event.set()
+ print("\n" + threading.currentThread().getName() + "selesai.")
+
+ def webapi(self):
+ with self.rlock:
+ print('Webservice nya')
+ apiurl='https://nbc.vanmason.web.id/service/kodepos/42173'
+ response = requests.get(apiurl)
+ html=response.json()
+ string = ""
+ for i in range(len(html["kodepos"])):
+ baru = html["kodepos"][i]["kelurahan"]
+ new = "\n"+str(i)+". "
+ string = string+new+str(baru)
+ queue.put(string)
+ self.createfile(string)
+
+
+ def main(self):
+ with self.rlock:
+ self.webapi()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.filename)
+ f = open(self.filename, "w")
+ f.write(str(isi))
+ f.close()
+
+
+
diff --git a/Chapter02/ida.txt b/Chapter02/ida.txt
new file mode 100644
index 0000000..9a4b06b
--- /dev/null
+++ b/Chapter02/ida.txt
@@ -0,0 +1,6 @@
+Anime:
+0. One Piece
+1. One Piece Movie 1
+2. One Piece Film: Gold
+3. One Piece Film: Z
+4. One Piece 3D: Mugiwara Chase
\ No newline at end of file
diff --git a/Chapter02/idanilai.txt b/Chapter02/idanilai.txt
new file mode 100644
index 0000000..f6c1b22
--- /dev/null
+++ b/Chapter02/idanilai.txt
@@ -0,0 +1,19 @@
+how are you?
+
+how are you? 2
+
+how are you? 3
+
+how are you? 4
+
+how are you? 5
+
+how are you? 6
+
+how are you? 7
+
+how are you? 8
+
+how are you? 9
+
+how are you? 10
\ No newline at end of file
diff --git a/Chapter02/josua_1184091.py b/Chapter02/josua_1184091.py
new file mode 100644
index 0000000..852a46b
--- /dev/null
+++ b/Chapter02/josua_1184091.py
@@ -0,0 +1,112 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Mon Mar 15 16:56:22 2021
+
+@author: Josuansef Pardede (1184091)
+"""
+
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class josua_1184091 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ d=randint(1,20)
+ e=randint(1,20)
+ self.josh(d, e, 3000000)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def josh(self, d, e, f):
+ while f>0:
+ d=d*e/40
+ f=f-2
+
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ #thread1 = MyThreadClass("Thread#1 ", randint(1,10))
+ thread1 = josua_1184091("Thread#1 ", 1,20)
+ thread2 = josua_1184091("Thread#2 ", 2,20)
+ thread3 = josua_1184091("Thread#3 ", 3,20)
+ thread4 = josua_1184091("Thread#4 ", 4,20)
+ thread5 = josua_1184091("Thread#5 ", 5,20)
+ thread6 = josua_1184091("Thread#6 ", 6,20)
+ thread7 = josua_1184091("Thread#7 ", 7,20)
+ thread8 = josua_1184091("Thread#8 ", 8,20)
+ thread9 = josua_1184091("Thread#9 ", 9,20)
+ thread10 = josua_1184091("Thread#10 ", 10,20)
+ thread11 = josua_1184091("Thread#11 ", 11,20)
+ thread12 = josua_1184091("Thread#12 ", 12,20)
+ thread13 = josua_1184091("Thread#13", 13,20)
+ thread14 = josua_1184091("Thread#14", 14,20)
+ thread15 = josua_1184091("Thread#15", 15,20)
+ thread16 = josua_1184091("Thread#16", 16,20)
+ thread17 = josua_1184091("Thread#17", 17,20)
+ thread18 = josua_1184091("Thread#18", 18,20)
+ thread19 = josua_1184091("Thread#19", 19,20)
+ thread20 = josua_1184091("Thread#20", 20,20)
+ # Thread Running
+ thread1.start()
+ thread2.start()
+ thread3.start()
+ thread4.start()
+ thread5.start()
+ thread6.start()
+ thread7.start()
+ thread8.start()
+ thread9.start()
+ thread10.start()
+ thread11.start()
+ thread12.start()
+ thread13.start()
+ thread14.start()
+ thread15.start()
+ thread16.start()
+ thread17.start()
+ thread18.start()
+ thread19.start()
+ thread20.start()
+
+ # Thread joining
+ thread1.join()
+ thread2.join()
+ thread3.join()
+ thread4.join()
+ thread5.join()
+ thread6.join()
+ thread7.join()
+ thread8.join()
+ thread9.join()
+ thread10.join()
+ thread11.join()
+ thread12.join()
+ thread13.join()
+ thread14.join()
+ thread15.join()
+ thread16.join()
+ thread17.join()
+ thread18.join()
+ thread19.join()
+ thread20.join()
+
+ # End
+ print("End")
+
+ #Execution Time
+ print("--- %s seconds ---" % (time.time() - start_time))
+ return True
+
+
+
+
diff --git a/Chapter02/kodepos b/Chapter02/kodepos
new file mode 100644
index 0000000..2cdb19f
--- /dev/null
+++ b/Chapter02/kodepos
@@ -0,0 +1,15 @@
+
+0. Serang
+1. Serang
+2. Serang
+3. Serang
+4. Serang
+5. Serang
+6. Serang
+7. Serang
+8. Serang
+9. Serang
+10. Serang
+11. Serang
+12. Serang
+13. Serang
\ No newline at end of file
diff --git a/Chapter02/kodepos.txt b/Chapter02/kodepos.txt
new file mode 100644
index 0000000..2cdb19f
--- /dev/null
+++ b/Chapter02/kodepos.txt
@@ -0,0 +1,15 @@
+
+0. Serang
+1. Serang
+2. Serang
+3. Serang
+4. Serang
+5. Serang
+6. Serang
+7. Serang
+8. Serang
+9. Serang
+10. Serang
+11. Serang
+12. Serang
+13. Serang
\ No newline at end of file
diff --git a/Chapter02/okky.txt b/Chapter02/okky.txt
new file mode 100644
index 0000000..bedfc70
--- /dev/null
+++ b/Chapter02/okky.txt
@@ -0,0 +1 @@
+{'results': [{'gender': 'male', 'name': {'title': 'Mr', 'first': 'Varg', 'last': 'Malmin'}, 'location': {'street': {'number': 1778, 'name': 'Laura Gundersens gate'}, 'city': 'Ålvik', 'state': 'Akershus', 'country': 'Norway', 'postcode': '0361', 'coordinates': {'latitude': '53.7606', 'longitude': '-34.2627'}, 'timezone': {'offset': '+9:00', 'description': 'Tokyo, Seoul, Osaka, Sapporo, Yakutsk'}}, 'email': 'varg.malmin@example.com', 'login': {'uuid': 'f8e27f04-ddc3-45f5-a7e2-9a187fae891c', 'username': 'tinydog618', 'password': 'juanita', 'salt': 'Z2Wmu9wx', 'md5': '38c24fb79f58b8e89566d9c4c24b9a98', 'sha1': '29705dbe702a1b95043622dc53be25178f5f939f', 'sha256': '7cf9018389351a43ed30069767a8727335c676dc76a196208dfebf7936e8e97d'}, 'dob': {'date': '1980-08-15T21:32:36.340Z', 'age': 41}, 'registered': {'date': '2004-01-21T23:27:26.047Z', 'age': 17}, 'phone': '59695507', 'cell': '96121541', 'id': {'name': 'FN', 'value': '15088035159'}, 'picture': {'large': 'https://randomuser.me/api/portraits/men/51.jpg', 'medium': 'https://randomuser.me/api/portraits/med/men/51.jpg', 'thumbnail': 'https://randomuser.me/api/portraits/thumb/men/51.jpg'}, 'nat': 'NO'}], 'info': {'seed': '5c473166cb77031a', 'results': 1, 'page': 1, 'version': '1.3'}}
\ No newline at end of file
diff --git a/Chapter02/okky1184087.py b/Chapter02/okky1184087.py
new file mode 100644
index 0000000..95cca0a
--- /dev/null
+++ b/Chapter02/okky1184087.py
@@ -0,0 +1,73 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class okky1184087 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ time.sleep(self.duration)
+ out_list = list()
+ self.do_something(randint(1,10),1000000)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+ def do_something(self,count,out_list):
+ out_list =1
+ count = 5
+ for i in range(2, count+ 1):
+ out_list *= i
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = okky1184087("Thread#1 ", 1,randint(1,10))
+ thread2 = okky1184087("Thread#2 ", 2,randint(1,10))
+ thread3 = okky1184087("Thread#3 ", 3,randint(1,10))
+ thread4 = okky1184087("Thread#4 ", 4,randint(1,10))
+ thread5 = okky1184087("Thread#5 ", 5,randint(1,10))
+ thread6 = okky1184087("Thread#6 ", 6,randint(1,10))
+ thread7 = okky1184087("Thread#7 ", 7,randint(1,10))
+ thread8 = okky1184087("Thread#8 ", 8,randint(1,10))
+ thread9 = okky1184087("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/Chapter02/parhanTiga1184042.py b/Chapter02/parhanTiga1184042.py
new file mode 100644
index 0000000..8cfd98f
--- /dev/null
+++ b/Chapter02/parhanTiga1184042.py
@@ -0,0 +1,80 @@
+from threading import Thread,currentThread, Lock, RLock, Event
+import requests
+import os
+
+event = Event()
+hasilperhitungan=0
+
+class parhanEventDeleteFile (Thread):
+ def __init__(self,name,thread_number,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.event = event
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ print('class parhanEventDeleteFile mau menjalankan event wait dilanjutkan clear untuk baca dan delete file')
+ self.event.wait()
+ self.threadLock.acquire()
+ print('melakukan baca file : '+self.namafile)
+ self.readfile()
+ print('melakukan rename file : '+self.namafile)
+ self.renamefile()
+ self.event.wait()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "Baca file dan delete file selesai")
+ print ("menjalankan event clear")
+ self.event.clear()
+
+ def readfile(self):
+ f = open(self.namafile, "r")
+ print("Isi Filenya : "+f.read())
+
+ def renamefile(self):
+ os.rename(self.namafile,self.namafile+'.txt')
+
+class parhanTiga1184042 (Thread):
+ def __init__(self, name,thread_number,a,b ,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.event = event
+ self.rlock = RLock()
+ self.name = name
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.thread_number = thread_number
+ self.a=a
+ self.b=b
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "jalan")
+ self.threadLock.acquire()
+ print("threeadlock acquire utama")
+ self.hitung()
+ self.event.set()
+ print('class parhanTiga1184042 sudah selesai melakukan event set')
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def apicorona(self):
+ with self.rlock:
+ print('didalam rlock apicorona, akses web service...')
+ apiurl='https://api.kawalcorona.com/indonesia'
+ response = requests.get(apiurl)
+ html=response.json()
+ self.createfile(html)
+
+ def hitung(self):
+ with self.rlock:
+ print('rlock hitung')
+ self.apicorona()
+
+ def createfile(self,isi):
+ print('membuat file baru : '+ self.namafile)
+ f = open(self.namafile, "x")
+ f.write(str(isi))
+ f.close()
+ print('sudah membuat file baru, mau menjalankan event set')
+
diff --git a/Chapter02/ravi.txt b/Chapter02/ravi.txt
new file mode 100644
index 0000000..5e7c2db
--- /dev/null
+++ b/Chapter02/ravi.txt
@@ -0,0 +1 @@
+[{'id': '1301', 'province_id': '13', 'name': 'KABUPATEN KEPULAUAN MENTAWAI'}, {'id': '1302', 'province_id': '13', 'name': 'KABUPATEN PESISIR SELATAN'}, {'id': '1303', 'province_id': '13', 'name': 'KABUPATEN SOLOK'}, {'id': '1304', 'province_id': '13', 'name': 'KABUPATEN SIJUNJUNG'}, {'id': '1305', 'province_id': '13', 'name': 'KABUPATEN TANAH DATAR'}, {'id': '1306', 'province_id': '13', 'name': 'KABUPATEN PADANG PARIAMAN'}, {'id': '1307', 'province_id': '13', 'name': 'KABUPATEN AGAM'}, {'id': '1308', 'province_id': '13', 'name': 'KABUPATEN LIMA PULUH KOTA'}, {'id': '1309', 'province_id': '13', 'name': 'KABUPATEN PASAMAN'}, {'id': '1310', 'province_id': '13', 'name': 'KABUPATEN SOLOK SELATAN'}, {'id': '1311', 'province_id': '13', 'name': 'KABUPATEN DHARMASRAYA'}, {'id': '1312', 'province_id': '13', 'name': 'KABUPATEN PASAMAN BARAT'}, {'id': '1371', 'province_id': '13', 'name': 'KOTA PADANG'}, {'id': '1372', 'province_id': '13', 'name': 'KOTA SOLOK'}, {'id': '1373', 'province_id': '13', 'name': 'KOTA SAWAH LUNTO'}, {'id': '1374', 'province_id': '13', 'name': 'KOTA PADANG PANJANG'}, {'id': '1375', 'province_id': '13', 'name': 'KOTA BUKITTINGGI'}, {'id': '1376', 'province_id': '13', 'name': 'KOTA PAYAKUMBUH'}, {'id': '1377', 'province_id': '13', 'name': 'KOTA PARIAMAN'}]
\ No newline at end of file
diff --git a/Chapter02/raviDua1184040.py b/Chapter02/raviDua1184040.py
new file mode 100644
index 0000000..66ae5ab
--- /dev/null
+++ b/Chapter02/raviDua1184040.py
@@ -0,0 +1,91 @@
+from threading import Thread,currentThread, Lock, RLock, Semaphore
+import requests
+import os
+
+semaphore = Semaphore(0)
+hasilperhitungan=0
+
+class raviSemaphorewriteFile (Thread):
+ def __init__(self,name,threadId,nfile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.threadId = threadId
+ self.nfile=os.path.join(os.path.dirname(__file__), nfile)
+ self.semaphore = semaphore
+
+ def run(self):
+ print("\n"+str(self.threadId)+". ---> " + self.name + "Mulai ya!")
+ print('mau menjalankan semaphore acquire untuk baca dan tulis, buat ulang file')
+ self.threadLock.acquire()
+ self.semaphore.acquire()
+ print('baca file dong : '+self.nfile)
+ self.readfile()
+ print('Tulis dan buat ulang file dong : '+self.nfile)
+ self.writefile()
+ self.threadLock.release()
+ print("\n"+str(self.threadId)+". ---> " + currentThread().getName() + "Finish")
+
+ def readfile(self):
+ f = open(self.nfile, "r+")
+ ##f.read(20) #Metode read(n) berfungsi untuk membaca sebanyak n karakter.
+ print("Tampilin angkanya dong biar kita tau : \n "+f.read())
+
+ def writefile(self):
+ f = open(self.nfile, "r+")
+ fc = open(self.nfile+'.html', "w")
+ for line in f:
+ fc.write(line.replace('Angka', 'Nomor'))
+ ff = open(self.nfile+'.html', "r+")
+ print(ff.read())
+
+
+class raviDua1184040(Thread):
+ def __init__(self, name,threadId,ravi,rahmatul ,nfile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.semaphore = semaphore
+ self.rlock = RLock()
+ self.name = name
+ self.nfile=os.path.join(os.path.dirname(__file__), nfile)
+ self.threadId = threadId
+ self.ravi=ravi
+ self.rahmatul=rahmatul
+
+ def run(self):
+ print("\n"+str(self.threadId)+". ---> " + self.name + "Mulai aja ya")
+ self.threadLock.acquire()
+ print("ini threeadlock acquire utama")
+ self.count()
+ self.threadLock.release()
+ print("\n"+str(self.threadId)+". ---> " + currentThread().getName() + "Finish")
+
+ def apicount(self):
+ with self.rlock:
+ print('Inside rlock apipangkat, akses web service...')
+ apiurl=' http://api.mathjs.org/v4/?expr=2%2B3*sqrt(4)'
+ eq=str(self.ravi)+'*'+str(self.rahmatul)
+ response = requests.get(apiurl+eq)
+ html=response.content.decode(response.encoding)
+ hasil = int(html)
+ string = "Angka : "
+ i = 1
+ for i in range(1, 11):
+ string = string+str(i)
+ i = i +1
+ self.createfile(string)
+ x = open(self.nfile, "r+")
+ print(x.read())
+
+ def count(self):
+ with self.rlock:
+ self.apicount()
+
+ def createfile(self,isi):
+ print('Membuat file baru nih : '+ self.nfile)
+ f = open(self.nfile, "w")
+ f.write(str(isi))
+ f.close()
+ print('Sudah di buat file baru-nya nih, SIAP-Siap ya, selanjutnya mau relese semaphore')
+ self.semaphore.release()
+ print('di dalam Semaphore release tadi, semaphore sudah di release')
\ No newline at end of file
diff --git a/Chapter02/raviEnam1184040.py b/Chapter02/raviEnam1184040.py
new file mode 100644
index 0000000..a518071
--- /dev/null
+++ b/Chapter02/raviEnam1184040.py
@@ -0,0 +1,57 @@
+from threading import Barrier, Thread
+import os
+import requests
+from time import ctime, sleep
+
+no = 1
+b = Barrier(no)
+r1 = "ravi"
+r2 = "newravi"
+filename = os.path.join(os.path.dirname(__file__), r1)
+filename2 = os.path.join(os.path.dirname(__file__), r2)
+
+def API():
+ apiurl='http://www.emsifa.com/api-wilayah-indonesia/api/regencies/13.json'
+ response = requests.get(apiurl)
+ html=response.json()
+ cfile(html)
+
+def cfile(isi):
+ print('File dibuat : '+r1+'.docx \n')
+ f = open(filename+".docx", "w")
+ print('File dibuat sudah \n')
+ f.write(str(isi))
+ f.close()
+ print('Ini isinya: '+r1+'.docx')
+ x = open(filename+".docx", "r")
+ print(x.read()+'\n')
+ x.close()
+ print('Ganti nama \n')
+ print('menjadi: '+r2+'.docx \n')
+ os.rename(filename+'.docx', filename2+'.docx')
+ sleep(2)
+ b.wait()
+ print('File:'+r1+'.docx'+' berhasil di ganti \n')
+ sleep(2)
+ b.wait()
+ print('Create, Read, and delete r1: '+r1+' barrier waktu: %s \n' % (ctime()))
+
+def run():
+ API()
+ sleep(2)
+ b.wait()
+ print('Semua mencapai barrier pada waktu: %s \n' % (ctime()))
+
+def main():
+ threads = []
+ print('goodluck')
+ for i in range(no):
+ threads.append(Thread(target=run))
+ threads[-1].start()
+ for thread in threads:
+ thread.join()
+ print('done')
+ return True
+
+#if __name__ == "__main__":
+# main()
\ No newline at end of file
diff --git a/Chapter02/raviTiga1184040.py b/Chapter02/raviTiga1184040.py
new file mode 100644
index 0000000..581e02e
--- /dev/null
+++ b/Chapter02/raviTiga1184040.py
@@ -0,0 +1,97 @@
+from threading import Thread,currentThread, Lock, RLock, Event
+import requests
+import os
+
+items = []
+event = Event()
+hasilperhitungan=0
+
+class raviMenulis (Thread):
+ def __init__(self,name,threadId,nfile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.threadId = threadId
+ self.nfile=os.path.join(os.path.dirname(__file__), nfile)
+ self.threadEvent = event
+
+ def run(self):
+ print("\n"+str(self.threadId)+". ---> " + self.name + "Go")
+ self.threadLock.acquire()
+ print('Joni: Saya mau Belajar berhitung 1 sampai 10, Jono kamu bisa buatkan saya Angka dan Nomor nya?')
+ print('Jono: Bisa dong, tunggu sebentar')
+ event.wait()
+ print('Menulis : '+self.nfile)
+ self.readfile()
+ print('Baca file setala ditulis : '+self.nfile)
+ self.writefile()
+ self.threadEvent.clear()
+ self.threadLock.release()
+ print("\n"+str(self.threadId)+". ---> " + currentThread().getName() + "Finish")
+
+ def readfile(self):
+ f = open(self.nfile, "r+")
+ ##f.read(20) #Metode read(n) berfungsi untuk membaca sebanyak n karakter.
+ print("Ini angkanya, selamat belajar Joni : \n "+f.read())
+
+ def writefile(self):
+ f = open(self.nfile, "r+")
+ fd = open(self.nfile+'.pdf', "w")
+ for line in f:
+ fd.write(line.replace('Ini Angkanya', 'dan Ini Nomor'))
+ fd.close()
+ fr = open(self.nfile+'.pdf', "r+")
+ print(fr.read())
+ fr.close()
+
+
+class raviTiga1184040(Thread):
+ def __init__(self, name,threadId,r,f ,nfile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.threadEvent = event
+ self.rlock = RLock()
+ self.name = name
+ self.nfile=os.path.join(os.path.dirname(__file__), nfile)
+ self.threadId = threadId
+ self.r=r
+ self.f=f
+
+ def run(self):
+ print("\n"+str(self.threadId)+". ---> " + self.name + "Mulai")
+ self.count()
+ print("Event di Set!")
+ event.set()
+ print("\n"+str(self.threadId)+". ---> " + currentThread().getName() + "finish.")
+
+ def Api(self):
+ with self.rlock:
+ print('Di dalam rlock APIpangkat, akses web service...')
+ apiurl=' https://api.mathjs.org/v4/?expr=4%2B2*6-8'
+ eq=str(self.r)+'*'+str(self.f)
+ response = requests.get(apiurl+eq)
+ html=response.content.decode(response.encoding)
+ hasil = int(html)
+ string = " Angka : "
+ i = 1
+ for i in range(1, 11):
+ isinya = (1, 11)
+ items.append(isinya)
+ string = string+str(i)
+ i = i +1
+ self.createfile(string)
+ x = open(self.nfile, "r+")
+ print(x.read())
+ isinya = (1, 11)
+ items.append(isinya)
+
+
+ def count(self):
+ with self.rlock:
+ self.Api()
+
+ def createfile(self,isi):
+ print('Create File : '+ self.nfile)
+ f = open(self.nfile, "w")
+ f.write(str(isi))
+ f.close()
\ No newline at end of file
diff --git a/Chapter02/raviTujuh1184040.py b/Chapter02/raviTujuh1184040.py
new file mode 100644
index 0000000..dce3fda
--- /dev/null
+++ b/Chapter02/raviTujuh1184040.py
@@ -0,0 +1,87 @@
+from threading import Thread,currentThread, Lock, RLock, Event
+import requests
+import os
+from queue import Queue
+
+queue = Queue()
+event = Event()
+
+class raviMenulis (Thread):
+ def __init__(self,name,thread_number,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.name = name
+ self.thread_number = thread_number
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.event = event
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "GO!!")
+ print('class raviMenulis mau menjalankan event wait, setelah itu clear untuk baca dan rename file')
+ self.event.wait()
+ self.threadLock.acquire()
+ print('Action read file : '+self.namafile)
+ self.readfile()
+ print('Action rename file : '+self.namafile)
+ self.unamofile()
+ self.event.wait()
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "Baca dan delete file done")
+ print ("menjalankan event clear")
+ self.event.clear()
+
+ def readfile(self):
+ q=queue.get()
+ f = open(self.namafile, "r")
+ print("Isi Filenya: "+f.read())
+ print("Done!!, jalanin queue task done \n")
+ queue.task_done()
+
+ def unamofile(self):
+ print('Ganti Nama File \n')
+ os.rename(self.namafile,self.namafile+'.txt')
+ print("nama file diganti ya \n")
+
+class raviTujuh1184040 (Thread):
+ def __init__(self, name,thread_number,r,f ,namafile):
+ Thread.__init__(self)
+ self.threadLock = Lock()
+ self.event = event
+ self.rlock = RLock()
+ self.name = name
+ self.namafile=os.path.join(os.path.dirname(__file__), namafile)
+ self.thread_number = thread_number
+ self.r=r
+ self.f=f
+
+ def run(self):
+ print("\n"+str(self.thread_number)+". ---> " + self.name + "GO!!")
+ self.threadLock.acquire()
+ print("threeadlock acquire utama")
+ self.hitung()
+ self.event.set()
+ print('class raviTujuh1184068 selesai untuk event set')
+ self.threadLock.release()
+ print("\n"+str(self.thread_number)+". ---> " + currentThread().getName() + "selesai")
+
+ def Aapi(self):
+ with self.rlock:
+ print('didalam rlock apipangkat, akses web service...')
+ apiurl='http://www.emsifa.com/api-wilayah-indonesia/api/regencies/13.json'
+ response = requests.get(apiurl)
+ html=response.json()
+ queue.put(html)
+ self.createfile(html)
+
+ def hitung(self):
+ with self.rlock:
+ print('rlock hitung')
+ self.Aapi()
+
+ def createfile(self,isi):
+ print('Buat file baru : '+ self.namafile)
+ f = open(self.namafile, "x")
+ f.write(str(isi))
+ f.close()
+ print('file baru sudah dibuat, mau menjalankan event set')
+
diff --git a/Chapter02/vicky b/Chapter02/vicky
new file mode 100644
index 0000000..c574199
--- /dev/null
+++ b/Chapter02/vicky
@@ -0,0 +1,101 @@
+nama kota:
+0. Light rum
+1. Applejack
+2. Gin
+3. Dark rum
+4. Sweet Vermouth
+5. Strawberry schnapps
+6. Scotch
+7. Apricot brandy
+8. Triple sec
+9. Southern Comfort
+10. Orange bitters
+11. Brandy
+12. Lemon vodka
+13. Blended whiskey
+14. Dry Vermouth
+15. Amaretto
+16. Tea
+17. Champagne
+18. Coffee liqueur
+19. Bourbon
+20. Tequila
+21. Vodka
+22. Aņejo rum
+23. Bitters
+24. Sugar
+25. Kahlua
+26. demerara Sugar
+27. Dubonnet Rouge
+28. Watermelon
+29. Lime juice
+30. Irish whiskey
+31. Apple brandy
+32. Carbonated water
+33. Cherry brandy
+34. Creme de Cacao
+35. Grenadine
+36. Port
+37. Coffee brandy
+38. Red wine
+39. Rum
+40. Grapefruit juice
+41. Ricard
+42. Sherry
+43. Cognac
+44. Sloe gin
+45. Apple juice
+46. Pineapple juice
+47. Lemon juice
+48. Sugar syrup
+49. Milk
+50. Strawberries
+51. Chocolate syrup
+52. Yoghurt
+53. Mango
+54. Ginger
+55. Lime
+56. Cantaloupe
+57. Berries
+58. Grapes
+59. Kiwi
+60. Tomato juice
+61. Cocoa powder
+62. Chocolate
+63. Heavy cream
+64. Galliano
+65. Peach Vodka
+66. Ouzo
+67. Coffee
+68. Spiced rum
+69. Water
+70. Espresso
+71. Angelica root
+72. Orange
+73. Cranberries
+74. Johnnie Walker
+75. Apple cider
+76. Everclear
+77. Cranberry juice
+78. Egg yolk
+79. Egg
+80. Grape juice
+81. Peach nectar
+82. Lemon
+83. Firewater
+84. Lemonade
+85. Lager
+86. Whiskey
+87. Absolut Citron
+88. Pisco
+89. Irish cream
+90. Ale
+91. Chocolate liqueur
+92. Midori melon liqueur
+93. Sambuca
+94. Cider
+95. Sprite
+96. 7-Up
+97. Blackberry brandy
+98. Peppermint schnapps
+99. Creme de Cassis
\ No newline at end of file
diff --git a/Chapter02/vicky1184037.py b/Chapter02/vicky1184037.py
new file mode 100644
index 0000000..3846402
--- /dev/null
+++ b/Chapter02/vicky1184037.py
@@ -0,0 +1,84 @@
+import time
+import os
+from random import randint
+from threading import Thread,currentThread
+
+
+class vicky1184037 (Thread):
+ def __init__(self, name,thread_number, duration):
+ Thread.__init__(self)
+ self.name = name
+ self.duration = duration
+ self.thread_number = thread_number
+ def run(self):
+ print ("\n"+str(self.thread_number)+". ---> " + self.name + " running, belonging to process ID "+ str(os.getpid()) + "\n")
+ time.sleep(self.duration)
+ out_list = list()
+ self.do_something(1000000,out_list)
+ print (str(self.thread_number)+". ---> " + self.name + " over, sleep duration : " +str(self.duration) +" second")
+ print (", Realname of Thread : " + currentThread().getName())
+
+import time
+from do_something import
+
+if __name__ == "__main__" :
+ start_time = time.time()
+ size = 10000000
+ n_exec = 10
+ for i in range(0, n_exec) :
+ out_list = list()
+ do_something(size, out_list)
+
+ print ("List processing complate.")
+ end_time = time.time()
+ print("serial time-", end_time - start_time)
+
+
+
+def main():
+ start_time = time.time()
+
+ # Thread Creation
+ thread1 = vicky1184037("Thread#1 ", 1,randint(1,10))
+ thread2 = vicky1184037("Thread#2 ", 2,randint(1,10))
+ thread3 = vicky1184037("Thread#3 ", 3,randint(1,10))
+ thread4 = vicky1184037("Thread#4 ", 4,randint(1,10))
+ thread5 = vicky1184037("Thread#5 ", 5,randint(1,10))
+ thread6 = vicky1184037("Thread#6 ", 6,randint(1,10))
+ thread7 = vicky1184037("Thread#7 ", 7,randint(1,10))
+ thread8 = vicky1184037("Thread#8 ", 8,randint(1,10))
+ thread9 = vicky1184037("Thread#9 ", 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))
+ return True
+
+
+
+
+
diff --git a/README.md b/README.md
index 5a125d7..f2016f1 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Python Parallel Programming Cookbook - Second Edition
-
+
This is the code repository for [Python Parallel Programming Cookbook - Second Edition ](https://www.packtpub.com/programming/python-parallel-programming-cookbook?utm_source=github&utm_medium=repository&utm_campaign=9781789533736), published by Packt.
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..55f9a52
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,2 @@
+requests
+pytest-cov
\ No newline at end of file
diff --git a/test_app.py b/test_app.py
new file mode 100644
index 0000000..cd08d1d
--- /dev/null
+++ b/test_app.py
@@ -0,0 +1,491 @@
+import unittest
+
+
+
+#from Chapter01 import serial_test,multiprocessing_test,multithreading_test
+
+
+class TestApp(unittest.TestCase):
+
+ def setUp(self):
+ pass
+
+ def tearDown(self):
+ pass
+
+# def test_00_serial(self):
+# response = serial_test.serial_test()
+# self.assertEqual(response, True)
+#
+# def test_01_multithreading(self):
+# response = multithreading_test.multithreading_test()
+# self.assertEqual(response, True)
+#
+# def test_02_multiprocessing(self):
+# response = multiprocessing_test.multiprocessing_test()
+# self.assertEqual(response, True)
+
+ # def test_02_kaisar_1184093(self):
+ # from Chapter02.Kaisar1184093 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+ # def test_02_rizaluardi_1184102(self):
+ # from Chapter02.Rizaluardi1184102 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+ def readfile(self,filename):
+ f = open(filename, "r")
+ return str(f.read())
+ f.close()
+
+
+ # def test_07_zanwarTujuh1184050(self):
+ # from Chapter02.ZanwarTujuh1184050 import zanwarTujuh1184050, zanwarRewrite
+ # thread1 = zanwarRewrite("Thread Rewrite File ",1 , 'nilai')
+ # thread2 = zanwarTujuh1184050("Thread Utama ", 2, 5, 'nilai')
+ # thread2.start()
+ # thread1.start()
+ # thread2.join()
+ # thread1.join()
+ # respon=self.readfile('./Chapter02/nilai.txt')
+ # self.assertNotRegex(respon, "Kosong")
+
+# def test_03_WahyuKurniaSariDua1184001(self):
+ # from Chapter02.WahyuKurniasariDua1184001 import WahyuKurniaSariSemaphoreDeleteFile, WahyuKurniaSariDua1184001
+ # delete = WahyuKurniaSariSemaphoreDeleteFile("Thread delete", 1,"pikachu")
+ # main = WahyuKurniaSariDua1184001("Thread utama", 2,"pikachu", "pikachu")
+ # delete.start()
+ # main.start()
+ # delete.join()
+ # main.join()
+ #self.assertGreaterEqual(main.getFileContent(),0 )
+
+
+
+ # def test_03_FerdyTiga1184112(self):
+ # from Chapter02.FerdyTiga1184112 import FerdyGITiga1184112,FerdyEventGI
+ # threadrewrite= FerdyEventGI("Thread Lain ", 1,'minuman')
+ # threadutama = FerdyGITiga1184112("Thread inti ", 2,'minuman')
+ # threadrewrite.start()
+ # threadutama.start()
+ # threadrewrite.join()
+ # threadutama.join()
+ # respon=self.readfile('./Chapter02/minuman.txt')
+ # self.assertNotRegex(respon, "Gak Boleh Kosong")
+
+
+ # def test_03_hanifTiga1184058(self):
+ # from Chapter02.HanifTiga1184058 import hanifTiga1184058, hanifRename
+ # threadrename= hanifRename("Thread rename file ",1 , 'nilai')
+ # threadutama = hanifTiga1184058("Thread utama ", 2, 1, 'nilai')
+ # threadrename.start()
+ # threadutama.start()
+ # threadrename.join()
+ # threadutama.join()
+ # respon=self.readfile('./nilai.txt')
+ # self.assertNotRegex(respon, "Kosong")
+ # def readfile(self,nfile):
+ # f = open(nfile, "r+")
+ # return str(f.read())
+
+
+ # def test_03_raviTiga1184040(self):
+ # from Chapter02.raviTiga1184040 import raviTiga1184040, raviMenulis
+ # threadwrite = raviMenulis ("Thread Pro ",1 , 'value')
+ # threadutama = raviTiga1184040("Thread Utama ", 2,2,5, 'value')
+ # threadwrite.start()
+
+ # def test_03_FerdyTiga1184112(self):
+ # from Chapter02.FerdyTiga1184112 import FerdyGITiga1184112,FerdyEventGI
+ # threadrewrite= FerdyEventGI("Thread Lain ", 1,'minuman')
+ # threadutama = FerdyGITiga1184112("Thread inti ", 2,'minuman')
+ # threadrewrite.start()
+
+ # threadutama.start()
+ # threadwrite.join()
+ # threadutama.join()
+
+ # respon=self.readfile('./Chapter02/value.pdf')
+ # self.assertNotRegex(respon, "Nomor : 12345678910")
+
+ # respon=self.readfile('./Chapter02/minuman.txt')
+ # self.assertNotRegex(respon, "Gak Boleh Kosong")
+
+ # def test_03_hanifTiga1184058(self):
+ # from Chapter02.HanifTiga1184058 import hanifTiga1184058, hanifRename
+ # threadrename= hanifRename("Thread rename file ",1 , 'nilai')
+ # threadutama = hanifTiga1184058("Thread utama ", 2, 1, 'nilai')
+ # threadrename.start()
+ # threadutama.start()
+ # threadrename.join()
+ # threadutama.join()
+ # respon=self.readfile('./nilai.txt')
+ # self.assertNotRegex(respon, "Kosong")
+
+ # def test_03_raviTiga1184040(self):
+ # from Chapter02.raviTiga1184040 import raviTiga1184040, raviMenulis
+ # threadwrite = raviMenulis ("Thread Pro ",1 , 'value')
+ # threadutama = raviTiga1184040("Thread Utama ", 2,2,5, 'value')
+ # threadwrite.start()
+ # threadutama.start()
+ # threadwrite.join()
+ # threadutama.join()
+ # respon=self.readfile('./Chapter02/value.pdf')
+ # self.assertNotRegex(respon, "Nomor : 12345678910")
+
+
+# def test_03_parhanTiga1184042(self):
+# from Chapter02.parhanTiga1184042 import parhanTiga1184042,parhanEventDeleteFile
+# threadutama = parhanTiga1184042("Thread Utama ", 2,5,5,'parhan')
+# threaddelete= parhanEventDeleteFile("Thread Delete File ", 1,'parhan')
+# threaddelete.start()
+# threadutama.start()
+# threaddelete.join()
+# threadutama.join()
+# respon=self.readfile('./Chapter02/parhan.txt')
+# self.assertNotRegex(respon, "kosong")
+#
+# def test_03_okkyTiga1184087(self):
+# from chapter02.okkyTiga1184087 import okky1184087Write_Trier, okky1184087
+# threadwrite = okky1184087Write_Trier ("Thread satu ",1 , 'okky1')
+# threadutama = okky1184087("Thread dua ", 2,2, 'okky2')
+# threadwrite.start()
+# threadutama.start()
+# threadwrite.join()
+# threadutama.join()
+# respon=self.readfile('./Chapter02/value.pdf')
+# self.assertNotRegex(respon, "Nomor : 12345678910")
+
+ # def test_03_parhanTiga1184042(self):
+ # from Chapter02.parhanTiga1184042 import parhanTiga1184042,parhanEventDeleteFile
+ # threadutama = parhanTiga1184042("Thread Utama ", 2,5,5,'parhan')
+ # threaddelete= parhanEventDeleteFile("Thread Delete File ", 1,'parhan')
+ # threaddelete.start()
+ # threadutama.start()
+ # threaddelete.join()
+ # threadutama.join()
+ # respon=self.readfile('./Chapter02/parhan.txt')
+ # self.assertNotRegex(respon, "kosong")
+
+
+ # def test_06_Ferdy_1184112(self):
+ # from Chapter02.FerdyEnam1184112 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+ # def test_06_zanwar_1184050(self):
+ # from Chapter02.ZanwarEnam1184050 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+ #def test_06_alif_1184068(self):
+ # from Chapter02.AlifEnam1184068 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+
+ # def test_06_parhan_1184042(self):
+ # from Chapter02.ParhanEnam1184042 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+ # def test_06_parhan_1184042(self):
+ #from Chapter02.ParhanEnam1184042 import main
+ #response = main()
+ #self.assertEqual(response, True)
+
+ #def test_06_ira_1184024(self):
+ #from Chapter02.IraEnam1184024 import main
+ #response = main()
+ #self.assertEqual(response, True)
+
+
+ #def test_06_parhan_1184042(self):
+ # from Chapter02.ParhanEnam1184042 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+ # def test_06_hanif_1184058(self):
+ # from Chapter02.HanifEnam1184058 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+ #def test_06_WahyuKurniaSari_1184001(self):
+
+
+ # def test_06_WahyuKurniaSari_1184001(self):
+ # from Chapter02.WahyuKurniaSariEnam1184001 import main
+ # result=main()
+ # self.assertEqual(result, True)
+
+
+ #def test_06_WahyuKurniaSari_1184001(self):
+
+ #from Chapter02.WahyuKurniaSariEnam1184001 import main
+ #result=main()
+ #self.assertEqual(result, True)
+
+ # def test_06_ira_1184024(self):
+ # from Chapter02.IraEnam1184024 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+
+ # from Chapter02.WahyuKurniaSariEnam1184001 import main
+ # result=main()
+ # self.assertEqual(result, True)
+
+ #def test_06_ira_1184024(self):
+ # from Chapter02.IraEnam1184024 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+ # def test_06_rizaluardi_1184102(self):
+ # from Chapter02.RizaluardiEnam1184102 import main
+ # result=main()
+ # self.assertEqual(result, True)
+
+ #def test_06_rizaluardi_1184102(self):
+ # from Chapter02.RizaluardiEnam1184102 import main
+ # result=main()
+ # self.assertEqual(result, True)
+
+
+ # def test_06_nandez_1184014(self):
+ # from Chapter02.IrfanHernandezEnam1184014 import main
+ # def test_06_josuansef_1184091(self):
+ # from Chapter02.JosuansefEnam_1184091 import main
+
+ # response = main()
+ # self.assertEqual(response, True)
+
+# def test_03_FerdyEvent1184112(self):
+# from Chapter02.FerdyTujuh1184112 import FerdyQueue,Ferdy
+# threadjuga= FerdyQueue("Thread Lain ",'queue')
+# threadaja = Ferdy("Thread inti",'queue')
+# threadjuga.start()
+# threadaja.start()
+# threadjuga.join()
+# threadaja.join()
+# respon=self.readfile('./Chapter02/queue')
+# self.assertNotRegex(respon, "Gak Boleh Kosong")
+
+# def test_07_HernandezTujuh1184014(self):
+# from Chapter02.IrfanHernandezTujuh1184014 import IrfanPut
+# thread1 = IrfanPut("Thread Put", 1, "miramas")
+# thread1.start()
+# thread1.join()
+# respon=self.readfile('./Chapter02/miramas.txt')
+# self.assertNotRegex(respon, "Kosong")
+#
+#
+# def test_07_alifTujuh1184068(self):
+# from Chapter02.AlifTujuh1184068 import alifTujuh1184068,alifHandlingFile
+# threadutama = alifTujuh1184068("Thread Utama ", 2,5,5,'alip')
+# threadhandling= alifHandlingFile("Thread handling File ", 1,'alip')
+# threadhandling.start()
+# threadutama.start()
+# threadhandling.join()
+# threadutama.join()
+# respon=self.readfile('./Chapter02/alip.txt')
+# self.assertNotRegex(respon, "kosong")
+
+ # def test_07_hanifTujuh1184058(self):
+ # from Chapter02.HanifTujuh1184058 import HanifTujuh1184058, HanifCopy
+ # thread1 = HanifCopy("Thread Copy File ",1 , 'pokemon.txt')
+ # thread2 = HanifTujuh1184058("Thread Utama ", 2, 5, 'pokemon.txt')
+ # thread2.start()
+ # thread1.start()
+ # thread2.join()
+ # thread1.join()
+ # respon=self.readfile('./Chapter02/pokemon.txt')
+ # self.assertNotRegex(respon, "Kosong")
+
+ # def test_06_bahar_1184002(self):
+ # from Chapter02.baharenam1184002 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+ #def test_06_josuansef_1184091(self):
+ # from Chapter02.JosuansefEnam_1184091 import main
+ # response = main()
+ # self.assertEqual(response, True)
+
+
+# def test_07_raviTujuh1184040(self):
+# from Chapter02.raviTujuh1184040 import raviTujuh1184040,raviMenulis
+# threadutama = raviTujuh1184040("Thread Utama ", 2,5,5,'ravi')
+# threadravi2= raviMenulis("Thread raviMenulis File ", 1,'ravi')
+# threadravi2.start()
+# threadutama.start()
+# threadravi2.join()
+# threadutama.join()
+# respon=self.readfile('./Chapter02/ravi.txt')
+# self.assertNotRegex(respon, "kosong")
+
+ # def test_07_OkkyTujuh1184087(self):
+ # from Chapter02.OkkyTujuh1184087 import Person,okky
+ # t1 = Person("Thread Utama ", 1,'okky.txt')
+ # t2 = okky("Thread kedua File ", 2,5,'okky.txt')
+ # t2.start()
+ # t1.start()
+ # t2.join()
+ # t1.join()
+ # respon=self.readfile('./Chapter02/okky.txt')
+ # self.assertNotRegex(respon, "kosong")
+
+ # def test_07_raviTujuh1184040(self):
+ # from Chapter02.raviTujuh1184040 import raviTujuh1184040,raviMenulis
+ # threadutama = raviTujuh1184040("Thread Utama ", 2,5,5,'ravi')
+ # threadravi2= raviMenulis("Thread raviMenulis File ", 1,'ravi')
+ # threadravi2.start()
+ # threadutama.start()
+ # threadravi2.join()
+ # threadutama.join()
+ # respon=self.readfile('./Chapter02/ravi.txt')
+ # self.assertNotRegex(respon, "kosong")
+
+# def test_07_AnisaTujuh1184016(self):
+# from Chapter02.AnisaRosalinaTujuh1184016 import Anisa
+# thread1 = Anisa("Thread Put", 1, "Cocktile")
+# thread1.start()
+# thread1.join()
+# respon=self.readfile('./Chapter02/Cocktile.txt')
+# self.assertNotRegex(respon, "Kosong")
+
+ # def test_07_AnisaTujuh1184016(self):
+ # from Chapter02.AnisaRosalinaTujuh1184016 import Anisa
+ # thread1 = Anisa("Thread Put", 1, "Cocktile")
+ # thread1.start()
+ # thread1.join()
+ # respon=self.readfile('./Chapter02/Cocktile.txt')
+ # self.assertNotRegex(respon, "Kosong")
+
+
+
+ # def test_07_AriyoTujuh1184056(self):
+ # from Chapter02.AriyoTujuh1184056 import ayomaju
+ # thread1 = ayomaju("Thread Put", "AriyoTujuh")
+ # thread1.start()
+ # thread1.join()
+ # respon=self.readfile('./Chapter02/AriyoTujuh')
+ # self.assertNotRegex(respon, "Kosong")
+
+ # def test_07_bahartujuh1184002(self):
+ # from Chapter02.bahartujuh1184002 import BaharQue,Bahartujuh1184002
+ # ti= BaharQue("Thread 1 ",'kodepos')
+ # tl = Bahartujuh1184002("Thread 2",'kodepos')
+ # ti.start()
+ # tl.start()
+ # ti.join()
+ # tl.join()
+ # respon=self.readfile('./Chapter02/kodepos')
+ # self.assertNotRegex(respon, "Gak Boleh Kosong")
+
+ # def test_07_parhanTujuh1184042(self):
+ # from Chapter02.ParhanTujuh1184042 import parhanTujuh1184042,parhanmanajemenFile
+ # threadutama = parhanTujuh1184042("Thread Utama ", 2,5,5,'apigempa')
+ # threadmanajemen= parhanmanajemenFile("Thread manajemen File ", 1,'apigempa')
+ # threadmanajemen.start()
+ # threadutama.start()
+ # threadmanajemen.join()
+ # threadutama.join()
+ # respon=self.readfile('./Chapter02/apigempa.txt')
+ # self.assertNotRegex(respon, "kosong")
+
+ # def test_07_idaTujuh1184113(self):
+ # from Chapter02.IdaTujuh1184113 import idaTujuh1184113,Director
+ # threadutama = idaTujuh1184113("Thread Utama ", 2,5,5,'ida')
+ # threaddirector= Director("Thread Director ", 1,'ida')
+ # threaddirector.start()
+ # threadutama.start()
+ # threaddirector.join()
+ # threadutama.join()
+ # respon=self.readfile('./Chapter02/ida.txt')
+ # self.assertNotRegex(respon, "kosong")
+
+
+ #def test_07_DiarTujuh1184083(self):
+ # from Chapter02.DiarTujuh1184083 import DiarQue,DiarTujuh1184083
+ # ti = DiarQue("Thread 1 ",'kodepos')
+ # tl = DiarTujuh1184083("Thread 2",'kodepos')
+ # ti.start()
+ # tl.start()
+ # ti.join()
+ # tl.join()
+ # respon=self.readfile('./Chapter02/kodepos')
+ # self.assertNotRegex(respon, "Gak Boleh Kosong")
+
+ #def test_07_idaTujuh1184113(self):
+ # from Chapter02.IdaTujuh1184113 import idaTujuh1184113,Director
+ # threadutama = idaTujuh1184113("Thread Utama ", 2,5,5,'ida')
+ # threaddirector= Director("Thread Director ", 1,'ida')
+ # threaddirector.start()
+ # threadutama.start()
+ # threaddirector.join()
+ # threadutama.join()
+ # respon=self.readfile('./Chapter02/ida.txt')
+ # self.assertNotRegex(respon, "kosong")
+
+ #def test_07_rizaluarditujuh_1184102(self):
+ # from Chapter02.RizaluardiTujuh1184102 import RizaluardiIms,RizaluardiTujuh1184102
+ # nganu1 = RizaluardiIms("Thread 1", 'provinsi')
+ # nganu2 = RizaluardiTujuh1184102("Thread 2",'provinsi')
+ # nganu1.start()
+ # nganu2.start()
+ # nganu1.join()
+ # nganu2.join()
+ # respon=self.readfile('./Chapter02/provinsi')
+ # self.assertNotRegex(respon, "Jangan kosong datanya")
+
+ #def test_07_josuanseftujuh1184091(self):
+ # from Chapter02.JosuansefTujuh_1184091 import Josuansef1184091Tujuh_Producer,Josuansef1184091Tujuh_Consumer
+ # threadjosuaproducer = Josuansef1184091Tujuh_Producer("Thread utama ", 2,5,5,'joss')
+ # threadjosuaconsumer = Josuansef1184091Tujuh_Consumer("Thread baca file ", 1,'joss')
+ # threadjosuaconsumer.start()
+ # threadjosuaproducer.start()
+ # threadjosuaconsumer.join()
+ # threadjosuaproducer.join()
+ # respon=self.readfile('./Chapter02/joss')
+ # self.assertNotRegex(respon, "kosong")
+
+ # def test_07_josuanseftujuh1184091(self):
+ # from Chapter02.JosuansefTujuh_1184091 import Josuansef1184091Tujuh_Producer,Josuansef1184091Tujuh_Consumer
+ # threadjosuaproducer = Josuansef1184091Tujuh_Producer("Thread utama ", 2,5,5,'joss')
+ # threadjosuaconsumer = Josuansef1184091Tujuh_Consumer("Thread baca file ", 1,'joss')
+ # threadjosuaconsumer.start()
+ # threadjosuaproducer.start()
+ # threadjosuaconsumer.join()
+ # threadjosuaproducer.join()
+ # respon=self.readfile('./Chapter02/joss')
+ # self.assertNotRegex(respon, "kosong")
+
+
+ #def test_07_iraTujuh1184024(self):
+ # from Chapter02.IraTujuh1184024 import iraTujuh1184024,iraHandlingFile
+ # threadutama = iraTujuh1184024("Thread Utama ", 2,5,5,'ira')
+ # threadhandling= iraHandlingFile("Thread handling File ", 1,'ira')
+ # threadhandling.start()
+ # threadutama.start()
+ # threadhandling.join()
+ # threadutama.join()
+ # respon=self.readfile('./Chapter02/ira.txt')
+ # self.assertNotRegex(respon, "kosong")
+
+ def test_07_vickyTujuh1184037(self):
+ from Chapter02.VickyTujuh1184037 import vickysaf
+ thread1 = vickysaf("Thread Put", "vicky")
+ thread1.start()
+ thread1.join()
+ respon=self.readfile('./Chapter02/vicky')
+ self.assertNotRegex(respon, "Kosong")
\ No newline at end of file