Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 = Integer.parseInt(a1[0]); a = Integer.parseInt(a1[0]); b = Integer.parseInt(a1[1]); b = Integer.parseInt(a1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 sum = a + b ; // 두 수를 더하는 부분입니다 System.out.println(" 두수의 합은 " + sum + " 입니다 "); System.out.println(" 두수의 합은 " + sum + " 입니다 "); }}

2 2 Multiple threads on multiple CPUs Multiple threads sharing a single CPU Threads Concept

3 10 장. 다중 스레드 3 Thread Class  java.lang Package Thread() Thread(String s) Thread(Runnable r) Thread(Runnable r, String s)

4 10 장. 다중 스레드 4 Method Description static void sleep(long msec) throws InterruptedException Causes the currently executing thread to sleep (temporarily cease execution) for the specifie d number of milliseconds, subject to the precision and accuracy of system timers and schedul ers. static void sleep(long msec, int nsec) throws InterruptedException Causes the currently executing thread to sleep (temporarily cease execution) for the specifie d 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 t hread. final int getPriority()Returns this thread's priority. final void setPriority(int p)Changes the priority of this thread. void join() throws InterruptedExceptionWaits for this thread to die. void run() If this thread was constructed using a separate Runnable run object, then that Runnable obje ct'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 because it is de adlock-pronesuspend()

5 10 장. 다중 스레드 5 StateMachine diagram

6 10 장. 다중 스레드 6 Thread states  New  Runnable(ready)  Running(executing)  Waiting  Dead(finished)

7 10 장. 다중 스레드 7 Thread Class class ThreadA extends Thread {......... public void run() {.... }....... } ThreadA ta = new ThreadA(); ta.start();

8 10 장. 다중 스레드 8 Runnable Interface class RunnableB extends Applet implements Runnable {......... public void run() {........ }........ } public interface Runnable { public void run(); }

9 10 장. 다중 스레드 9 Example RunnableB rb = new RunnableB(); Thread tb = new Thread(rb); tb.start(); RunnableB rb = new RunnableB(); new Thread(rb).start();

10 10 장. 다중 스레드 10 Exercises ThreadFromThread.java 01 02 03 04 05 06 07 08 09 10 11 12 13 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(); }

11 10 장. 다중 스레드 11 Java : 1 Java : 2 Java : 3 Java : 4 Java : 5 Java : 6 Java : 7 Java : 8 Java : 9 Java : 10

12 10 장. 다중 스레드 12 Exercises ThreadFromRunnable.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 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(); }

13 10 장. 다중 스레드 13 Java : 1 Java : 2 Java : 3 Java : 4 Java : 5 Java : 6 Java : 7 Java : 8 Java : 9 Java : 10

14 10 장. 다중 스레드 14 Exercises DoubleThread.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 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(); }

15 10 장. 다중 스레드 15 1: Thread1 1: Thread2 2: Thread1 2: Thread2 3: Thread1 3: Thread2 4: Thread1 4: Thread2 5: Thread1 5: Thread2 6: Thread1 6: Thread2 7: Thread1 7: Thread2 8: Thread1 8: Thread2 9: Thread1 9: Thread2 10: Thread1 10: Thread2 End: Thread1 End: Thread2

16 10 장. 다중 스레드 16 1: Thread2 1: Thread1 2: Thread2 2: Thread1 3: Thread2 3: Thread1 4: Thread2 4: Thread1 5: Thread2 5: Thread1 6: Thread2 6: Thread1 7: Thread2 7: Thread1 8: Thread2 8: Thread1 9: Thread2 9: Thread1 10: Thread2 10: Thread1 End: Thread2 End: Thread1

17 10 장. 다중 스레드 17 setPriority()  Changes the priority of this thread. static final int MAX_PRIORITY ← priority : 10 static final int MIN_PRIORITY ← priority : 1 static final int NORM_PRIORITY ← priority : 5

18 10 장. 다중 스레드 18 Excercises ThreadPriority.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() ); }

19 10 장. 다중 스레드 19 Excercises ThreadPriority.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(); }

20 10 장. 다중 스레드 20 1 : Third Thread : 1 1 : First Thread : 5 1 : Second Thread : 10 2 : Second Thread : 10 3 : Second Thread : 10 4 : Second Thread : 10 2 : First Thread : 5 3 : First Thread : 5 4 : First Thread : 5 5 : First Thread : 5 2 : Third Thread : 1 5 : Second Thread : 10 3 : Third Thread : 1 4 : Third Thread : 1 5 : Third Thread : 1

21 10 장. 다중 스레드 21

22 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 =

23 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 *****"); }

24 10 장. 다중 스레드 24 ***** Start ***** ***** End ***** 1 : Thread2 1 : Thread1 2 : Thread2 2 : Thread1 3 : Thread2 3 : Thread1 End : Thread2 End : Thread1

25 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

26 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 *****"); }

27 10 장. 다중 스레드 27 ***** Start ***** 1 : Thread1 1 : Thread2 2 : Thread1 2 : Thread2 3 : Thread1 3 : Thread2 End : Thread1 End : Thread2 ***** End *****

28 10 장. 다중 스레드 28 synchronized Method 329 page Critical section unlock

29 10 장. 다중 스레드 29 330 page

30 10 장. 다중 스레드 30 Excercises TVContribution.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 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);

31 10 장. 다중 스레드 31 실습예제 TVContribution.java 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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");

32 10 장. 다중 스레드 32 실습예제 TVContribution.java 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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()); }

33 10 장. 다중 스레드 33 Donator3 : 0 Donator3 : 1 Donator1 : 0 Donator4 : 0......... Donator5 : 62 Donator3 : 124 Donator1 : 126 Donator4 : 125 Donator2 : 62 Total : 504000

34 10 장. 다중 스레드 34 Donator1 : 0 Donator1 : 1 Donator1 : 2 Donator3 : 0......... Donator2 : 42 Donator3 : 102 Donator5 : 92 Donator1 : 62 Donator2 : 43 Total : 503000

35 10 장. 다중 스레드 35 java.lang.Object Class - wait(), notify(), notifyAll() void wait() throws InterruptedException void wait(long msec) throws InterruptedException void wait(long msec, int nsec) throws InterruptedException void notify() void notifyAll()

36 10 장. 다중 스레드 36 wait( ), notify( ), notifyAll( ) Method 335 page

37 10 장. 다중 스레드 37 Excercises ProducerConsumer.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 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();

38 10 장. 다중 스레드 38 Excercises ProducerConsumer.java 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 } 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); }

39 10 장. 다중 스레드 39 Excercises ProducerConsumer.java 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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() ; }

40 10 장. 다중 스레드 40 Producer ########## : Produce 0 Consumer########## : Consume 0 Producer ########## : Produce 1............................ Consumer########## : Consume 7 Producer ########## : Produce 8 Consumer########## : Consume 8 Producer ########## : Produce 9 Consumer########## : Consume 9


Download ppt "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."

Similar presentations


Ads by Google