lab7

Lab7_1 ~ Lab7_5 코드 분석 보고서 (장수제한없음)를 넣고 Lab7_학번_이름.zip 압축한 후 e-learning (http://lms.dankook.ac.kr/index.jsp)으로 제출 (Due by 12/3)

java2-lab7

Lab7_1 extends Thread vs implements Runnable
Lab7_2 Monitor vs Mutex vs Semaphore
Lab7_3 TicketSeller (Monitor vs Mutex vs Semaphore)
Lab7_4 Producer-Consumer Problem (Monitor vs Semaphore)
Lab7_5 WorkerThread (JavaFX + Task) & ThreadedImageGrayscale (Performance by # of threads)

Producer-Consumer Problem

Producer-Consumer Problem using Semaphore

Producer() {

put(item) {

P(empty); // for wait, Get an empty count, block if unavailable
P(mutex); // lock critical section: shared buffer
<<< critical section: Put item into shared buffer >>>
V(mutex); // unlock critical section
V(full); // for signal, increase number of full count

}

}

Consumer() {

take () {

P(full); // for wait, Get a full count, block if unavailable
P(mutex);
<<< critical section: Take item from shared buffer >>>
V(mutex);
V(empty); // for signal, increase number of empty count
return item;

}

}

Producer-Consumer Problem using Monitor

Producer() {

synchronized put(item) {

while(count >= buffer.length) { wait(); } // wait if buffer is full
count++
<<< critical section: Put iteminto shared buffer >>>
notifyAll(); // signal

}

}

Consumer() {

synchronized take () {

while(count <=0) { wait(); } // wait if buffer is empty
count- –
<<< critical section: Take itemfrom shared buffer >>>
notifyAll(); // signal
return item;

}

}

Java Thread yield() vs join() vs sleep()

https://stackoverflow.com/questions/9700871/what-is-difference-between-sleep-method-and-yield-method-of-multi-threading

We can prevent a thread from execution by using any of the 3 methods of Thread class:

  • yield()
  • join()
  • sleep()

yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent (i.e., yield() can only make a heuristic attempt to suspend the execution of the current thread with no guarantee of when will it be scheduled back).

join() The current thread can invoke join() on any other thread which makes the current thread wait for the other thread to die before proceeding. If any executing thread t1 calls join() on t2 (i.e, t2.join()) immediately t1 will enter into waiting state until t2 completes its execution.

sleep() method can force the scheduler to suspend the execution of the current thread for a specified period of time as its parameter.

Java Thread Synchronization

Thread Synchronization

https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

  • Mutex Semaphore (aka Mutex) controls only one thread at a time executing on the shared resource by lock & unlock.
  • Counting Semaphore (aka Semaphore – Java7) controls the number of threads executing on the shared resource by acquire & release.
  • Monitor controls only one thread at a time, and can execute in the monitor (shared object) by wait & notify/notifyAll.

lab6

Lab6_1 ~ Lab6_5 프로젝트 디렉토리 안에 모든 파일(src/*.java & bin/*.class)와 보고서 (장수제한없음)를 넣고 Lab6_학번_이름.zip 압축한 후 e-learning (http://lms.dankook.ac.kr/index.jsp)으로 제출 (Due by 11/12)
java2-lab6

Lab6_1 WOEIDLIST.csv
Lab6_2 forestfires.csv
Lab6_3 Earthquake1971~2011.csv (directory안에 있는 모든 지진데이타 로딩)
Lab6_4 your own data fileIO
Lab6_5 package 사용