10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void...
-
Upload
sophia-carson -
Category
Documents
-
view
235 -
download
1
Transcript of 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void...
![Page 1: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/1.jpg)
10. Multithreading
1
public class SumTest { public static void main(String a1[]) { int a, b, sum; a = Integer.parseInt(a1[0]); b = Integer.parseInt(a1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System.out.println(" 두수의 합은 " + sum + " 입니다 "); }}
JAVA
![Page 2: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/2.jpg)
2
Multiple threads on multiple CPUs
Multiple threads sharing a single CPU
Thread 3
Thread 2
Thread 1
Thread 3
Thread 2
Thread 1
• Threads Concept
![Page 3: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/3.jpg)
10 장 . 다중 스레드 3
Thread Class- java.lang Package
Thread()Thread(String s)Thread(Runnable r)Thread(Runnable r, String s)
![Page 4: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/4.jpg)
10 장 . 다중 스레드
4
MethodMethod Description
static void sleep(long msec)throws InterruptedException
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
static void sleep(long msec, int nsec) throws InterruptedException
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.
String getName() Returns this thread's name.
void setName(String s) Changes the name of this thread to be equal to the argument name.
void start()Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
final int getPriority() Returns this thread's priority.
final void setPriority(int p) Changes the priority of this thread.
void join() throws InterruptedException Waits for this thread to die.
void run()If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
void suspend() This method has been deprecated, as it is inherently deadlock-prone
void resume()This method exists solely for use with suspend(), which has been deprecated be-cause it is deadlock-prone
![Page 5: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/5.jpg)
10 장 . 다중 스레드
5
StateMachine diagram
![Page 6: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/6.jpg)
10 장 . 다중 스레드
6
Thread states- New
- Runnable(ready)
- Running(executing)
- Waiting
- Dead(finished)
![Page 7: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/7.jpg)
10 장 . 다중 스레드 7
Thread Class
class ThreadA extends Thread { ......... public void run() { .... } .......}
ThreadA ta = new ThreadA();ta.start();
![Page 8: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/8.jpg)
10 장 . 다중 스레드 8
Runnable Interface
class RunnableB extends Applet implements Runnable { ......... public void run() { ........ } ........}
public interface Runnable { public void run();}
![Page 9: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/9.jpg)
10 장 . 다중 스레드 9
Example
RunnableB rb = new RunnableB(); Thread tb = new Thread(rb);tb.start();
RunnableB rb = new RunnableB();new Thread(rb).start();
![Page 10: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/10.jpg)
10 장 . 다중 스레드 10
Exercises
ThreadFromThread.java
01020304050607080910111213
class ThreadTest extends Thread { public void run() { for (int i=1 ; i<=10 ; i++) { System.out.println(“Java :" + i); } }}public class ThreadFromThread { public static void main(String args[]) { ThreadTest t = new ThreadTest(); t.start(); }}
![Page 11: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/11.jpg)
10 장 . 다중 스레드 11
Java : 1
Java : 2
Java : 3
Java : 4
Java : 5
Java : 6
Java : 7
Java : 8
Java : 9
Java : 10
![Page 12: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/12.jpg)
10 장 . 다중 스레드 12
Exercises
ThreadFromRunnable.java
0102030405060708091011121314
class RunnableTest implements Runnable { public void run() { for (int i=1 ; i<=10 ; i++) { System.out.println(“Java :" + i); } } }public class ThreadFromRunnable { public static void main(String args[]) { RunnableTest r = new RunnableTest(); Thread t = new Thread(r); t.start(); }}
![Page 13: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/13.jpg)
10 장 . 다중 스레드 13
Java : 1
Java : 2
Java : 3
Java : 4
Java : 5
Java : 6
Java : 7
Java : 8
Java : 9
Java : 10
![Page 14: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/14.jpg)
10 장 . 다중 스레드 14
ExercisesDoubleThread.java
010203040506070809101112131415161718192021
class ThreadTest1 extends Thread { public ThreadTest1(String str) { setName(str); } public void run() { for (int i=1 ; i<=10 ; i++) { System.out.println(i + getName()); } System.out.println(“End" + getName()); }}public class DoubleThread { public static void main(String args[]) { ThreadTest1 t1 = new ThreadTest1 (" : Thread1"); ThreadTest1 t2 = new ThreadTest1 (" : Thread2"); t1.start(); t2.start(); }}
![Page 15: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/15.jpg)
10 장 . 다중 스레드 15
1: Thread11: Thread22: Thread12: Thread23: Thread13: Thread24: Thread14: Thread25: Thread15: Thread26: Thread16: Thread27: Thread17: Thread28: Thread18: Thread29: Thread19: Thread210: Thread110: Thread2 End: Thread1End: Thread2
![Page 16: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/16.jpg)
10 장 . 다중 스레드 16
1: Thread21: Thread12: Thread22: Thread13: Thread23: Thread14: Thread24: Thread15: Thread25: Thread16: Thread26: Thread17: Thread27: Thread18: Thread28: Thread19: Thread29: Thread110: Thread210: Thread1 End: Thread2End: Thread1
![Page 17: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/17.jpg)
10 장 . 다중 스레드
17
setPriority()- Changes the priority of this thread.
static final int MAX_PRIORITY ← priority : 10static final int MIN_PRIORITY ← priority : 1static final int NORM_PRIORITY ← priority : 5
![Page 18: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/18.jpg)
10 장 . 다중 스레드
18
ExcercisesThreadPriority.java
01
02
03
04
05
06
07
08
09
10
11
class PriorityTest extends Thread{
public PriorityTest(String str){
setName(str);
}
public void run(){
for(int i=1; i<=5; i++){
System.out.println( i + getName() + " Priority : "
+ getPriority() );
}
}
}
![Page 19: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/19.jpg)
10 장 . 다중 스레드
19
ExcercisesThreadPriority.java
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class ThreadPriority {
public static void main(String args[]){
PriorityTest t1 = new PriorityTest(" : First Thread");
PriorityTest t2 = new PriorityTest(" : Second Thread");
PriorityTest t3 = new PriorityTest(" : Third Thread");
int priority_t1 = Integer.parseInt(args[0]);
int priority_t2 = Integer.parseInt(args[1]);
t1.setPriority(priority_t1);
t2.setPriority(priority_t2);
t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
![Page 20: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/20.jpg)
10 장 . 다중 스레드
20
1 : Third Thread : 11 : First Thread : 51 : Second Thread : 102 : Second Thread : 103 : Second Thread : 104 : Second Thread : 102 : First Thread : 53 : First Thread : 54 : First Thread : 55 : First Thread : 52 : Third Thread : 15 : Second Thread : 103 : Third Thread : 14 : Third Thread : 15 : Third Thread : 1
![Page 21: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/21.jpg)
10 장 . 다중 스레드
21
![Page 22: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/22.jpg)
10 장 . 다중 스레드
22
실습예제DoubleThread1.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
class DoubleThreadTest1 extends Thread {
public DoubleThreadTest1(String str) {
setName(str);
}
public void run() {
for (int i=1 ; i<=3 ; i++) {
System.out.println(i + getName());
}
System.out.println(“End" + getName());
}
}
public class DoubleThread1 {
public static void main(String args[]) {
DoubleThreadTest1 t1 =
new DoubleThreadTest1(" : Thread1");
DoubleThreadTest1 t2 =
![Page 23: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/23.jpg)
10 장 . 다중 스레드
23
실습예제DoubleThread1.java
17
18
19
20
21
22
23
new DoubleThreadTest1(" : Thread2");
System.out.println("***** Start *****");
t1.start();
t2.start();
System.out.println("***** End *****");
}
}
![Page 24: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/24.jpg)
10 장 . 다중 스레드
24
***** Start ********** End *****1 : Thread21 : Thread12 : Thread22 : Thread13 : Thread23 : Thread1End : Thread2End : Thread1
![Page 25: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/25.jpg)
10 장 . 다중 스레드
5 스레드의 시작과 종료
25
실습예제DoubleThread2.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
class DoubleThreadTest2 extends Thread {
public DoubleThreadTest2(String str) {
setName(str);
}
public void run() {
for (int i=1 ; i<=3 ; i++) {
System.out.println(i + getName());
}
System.out.println(“End" + getName());
}
}
public class DoubleThread2 {
public static void main(String args[])throws Exception {
DoubleThreadTest2 t1 = new DoubleThreadTest2
(" : Thread1");
DoubleThreadTest2 t2 = new DoubleThreadTest2
![Page 26: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/26.jpg)
10 장 . 다중 스레드
26
실습예제DoubleThread1.java
17
18
19
20
21
22
23
24
25
(" : Thread2");
System.out.println("***** Start *****");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("***** End *****");
}
}
![Page 27: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/27.jpg)
10 장 . 다중 스레드
27
***** Start *****1 : Thread11 : Thread22 : Thread12 : Thread23 : Thread13 : Thread2End : Thread1End : Thread2***** End *****
![Page 28: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/28.jpg)
10 장 . 다중 스레드
28
synchronized Method
329 page
Critical section
Critical section
unlock
![Page 29: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/29.jpg)
10 장 . 다중 스레드
29
330 page
![Page 30: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/30.jpg)
10 장 . 다중 스레드
30
ExcercisesTVContribution.java
010203040506070809101112131415161718
class Account { private int total = 0; synchronized void deposit() { total = total + 1000; } int gettotal() { return total; }}class Customer extends Thread { Account same_a; Customer(Account a, String s) { same_a = a; setName(s); } public void run() { for(int i = 0; i < 200 ; i++) { System.out.println(getName() + " : " + i);
![Page 31: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/31.jpg)
10 장 . 다중 스레드
31
실습예제TVContribution.java
192021222324252627282930313233343536
same_a.deposit(); if (same_a.gettotal() >= 500000) break; } }}public class TVContribution { public static void main(String args[])throws Exception { Account same_account = new Account(); Customer donator1 = new Customer(same_account,“Donator1"); Customer donator2 = new Customer(same_account,"Donator2"); Customer donator3 = new Customer(same_account,"Donator3"); Customer donator4 = new Customer(same_account,"Donator4"); Customer donator5 = new Customer(same_account,"Donator5");
![Page 32: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/32.jpg)
10 장 . 다중 스레드
32
실습예제TVContribution.java
3738394041424344454647484950
donator1.start(); donator2.start(); donator3.start(); donator4.start(); donator5.start(); donator1.join(); donator2.join(); donator3.join(); donator4.join(); donator5.join(); System.out.println(“Total : " + same_account.gettotal()); }}
![Page 33: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/33.jpg)
10 장 . 다중 스레드
33
Donator3 : 0Donator3 : 1Donator1 : 0Donator4 : 0 .........Donator5 : 62Donator3 : 124Donator1 : 126Donator4 : 125Donator2 : 62Total : 504000
![Page 34: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/34.jpg)
10 장 . 다중 스레드
34
Donator1 : 0Donator1 : 1Donator1 : 2Donator3 : 0 .........Donator2 : 42Donator3 : 102Donator5 : 92Donator1 : 62Donator2 : 43Total : 503000
![Page 35: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/35.jpg)
10 장 . 다중 스레드
35
java.lang.Object Class
- wait(), notify(), notifyAll()
void wait() throws InterruptedExceptionvoid wait(long msec) throws InterruptedExceptionvoid wait(long msec, int nsec) throws InterruptedException
void notify()void notifyAll()
![Page 36: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/36.jpg)
10 장 . 다중 스레드
36
wait( ), notify( ), notifyAll( ) Method
335 page
![Page 37: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/37.jpg)
10 장 . 다중 스레드
37
ExcercisesProducerConsumer.java
0102030405060708091011121314151617181920
class Buffer { private int contents; private boolean available = false; public synchronized void put(int value) { while (available == true ) { try{ wait(); } catch (InterruptedException e) {} } contents = value; System.out.println (“Producer########## : Produce " + contents); notify(); available = true; }public synchronized int get() { while (available == false ) { try { wait();
![Page 38: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/38.jpg)
10 장 . 다중 스레드
38
ExcercisesProducerConsumer.java
2122232425262728293031323334353637383940
} catch (InterruptedException e) {} } System.out.println (“Consumer########## : Consume " + contents); notify(); available = false; return contents; } }class Producer extends Thread { private Buffer b; public Producer(Buffer blank) { b = blank ; } public void run() { for (int i = 1; i <= 10;i++) b.put(i); }}
![Page 39: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/39.jpg)
10 장 . 다중 스레드
39
ExcercisesProducerConsumer.java
4142434445464748495051525354555657585960
class Consumer extends Thread { private Buffer b; public Consumer(Buffer blank) { b = blank; } public void run() { int value = 0; for (int i = 1 ; i <= 10 ; i++ ) value = b.get(); }}public class ProducerConsumer { public static void main(String args[]) { Buffer buff = new Buffer(); Producer p1 = new Producer(buff); Consumer c1 = new Consumer(buff); p1.start() ; c1.start() ; }}
![Page 40: 10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.](https://reader033.fdocument.pub/reader033/viewer/2022061618/56649d0a5503460f949dd2f1/html5/thumbnails/40.jpg)
10 장 . 다중 스레드
40
Producer ########## : Produce 0Consumer########## : Consume 0Producer ########## : Produce 1
............................
Consumer########## : Consume 7 Producer ########## : Produce 8 Consumer########## : Consume 8 Producer ########## : Produce 9 Consumer########## : Consume 9