문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
프로그램:java:ㄴhomework:book_source:ch17 [2022/01/04 11:31] clayeryan@gmail.com 만듦 |
프로그램:java:ㄴhomework:book_source:ch17 [2023/07/13 17:29] (현재) clayeryan@gmail.com ↷ 문서가 programmer:java:ㄴhomework:book_source:ch17에서 프로그램:java:ㄴhomework:book_source:ch17(으)로 이동되었습니다 |
||
---|---|---|---|
줄 1: | 줄 1: | ||
- | ====== | + | ====== |
+ | =====프로세스와 스레드===== | ||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ProcessEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | try { | ||
+ | // 메모장 실행 | ||
+ | Process p1 = Runtime.getRuntime().exec(" | ||
+ | // 그림판 실행 | ||
+ | Process p2 = Runtime.getRuntime().exec(" | ||
+ | |||
+ | p1.waitFor(); | ||
+ | p2.destroy(); | ||
+ | |||
+ | } catch(Exception e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | =====스레드 생성과 실행===== | ||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // Thread 상속받는 방법 | ||
+ | ThreadExtend t1 = new ThreadExtend(); | ||
+ | |||
+ | // Runnable 구현하는 방법 | ||
+ | Runnable r = new RunnableImple(); | ||
+ | // Thread 생성자의 매개변수로 전달 | ||
+ | Thread t2 = new Thread(r); | ||
+ | |||
+ | t1.start(); | ||
+ | t2.start(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class ThreadExtend extends Thread { | ||
+ | public void run() { | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<50; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class RunnableImple implements Runnable { | ||
+ | public void run() { | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<50; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Thread t1 = new Thread(new Runnable() { | ||
+ | @Override | ||
+ | public void run() { | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<50; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | Thread t2 = new Thread(new Runnable() { | ||
+ | @Override | ||
+ | public void run() { | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<50; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | t1.start(); | ||
+ | t2.start(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Thread t1 = new Thread(() -> { | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<50; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | Thread t2 = new Thread(() -> { | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<50; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | t1.start(); | ||
+ | t2.start(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | =====스레드 우선순위===== | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx4 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Thread t1 = new Thread(() -> { | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<50; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | Thread t2 = new Thread(() -> { | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<50; i++) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | // 우선순위 지정 | ||
+ | t1.setPriority(7); | ||
+ | t2.setPriority(3); | ||
+ | |||
+ | t1.start(); | ||
+ | t2.start(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | =====스레드 상태 제어===== | ||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx7 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Thread t1 = new Thread(new Runnable() { | ||
+ | @Override | ||
+ | public void run() { | ||
+ | for (int i=0; i<10; i++) { | ||
+ | System.out.println(" | ||
+ | try { | ||
+ | Thread.sleep(1000); | ||
+ | } catch (InterruptedException e) {} | ||
+ | } | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | |||
+ | t1.start(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx5 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Thread t1 = new Thread(new Runnable() { | ||
+ | @Override | ||
+ | public void run() { | ||
+ | try { | ||
+ | int i=1; | ||
+ | while (true) { | ||
+ | System.out.println(" | ||
+ | // sleep을 넣지 않으면 interrupt되지 않음 | ||
+ | Thread.sleep(1000); | ||
+ | i++; | ||
+ | } | ||
+ | } catch (InterruptedException e) { | ||
+ | |||
+ | } | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | |||
+ | t1.start(); | ||
+ | try { | ||
+ | Thread.sleep(10000); | ||
+ | } catch (InterruptedException e) { | ||
+ | |||
+ | } | ||
+ | t1.interrupt(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx8 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Sum t1 = new Sum(); | ||
+ | Sum t2 = new Sum(); | ||
+ | |||
+ | t1.start(); | ||
+ | t2.start(); | ||
+ | try { | ||
+ | t1.join(); | ||
+ | t2.join(); | ||
+ | } catch (InterruptedException e) { | ||
+ | |||
+ | } | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class Sum extends Thread { | ||
+ | int sum = 0; | ||
+ | @Override | ||
+ | public void run() { | ||
+ | for (int i=1; i<=100; i++) { | ||
+ | sum += i; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx9 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | YieldThread t1 = new YieldThread(); | ||
+ | YieldThread t2 = new YieldThread(); | ||
+ | |||
+ | t1.start(); | ||
+ | t2.start(); | ||
+ | try { | ||
+ | Thread.sleep(1000); | ||
+ | } catch (InterruptedException e) {} | ||
+ | |||
+ | t1.isContinue = false; // t1 양보 | ||
+ | |||
+ | try { | ||
+ | Thread.sleep(1000); | ||
+ | } catch (InterruptedException e) {} | ||
+ | |||
+ | t1.isContinue = true; // t1 다시 실행 | ||
+ | |||
+ | try { | ||
+ | Thread.sleep(2000); | ||
+ | } catch (InterruptedException e) {} | ||
+ | |||
+ | // 스레드 종료 | ||
+ | t1.isBreak = true; | ||
+ | t2.isBreak = true; | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class YieldThread extends Thread { | ||
+ | boolean isBreak = false; | ||
+ | boolean isContinue = true; | ||
+ | |||
+ | @Override | ||
+ | public void run() { | ||
+ | while(!isBreak) { | ||
+ | if (isContinue) { | ||
+ | System.out.println(getName()+" | ||
+ | } | ||
+ | } | ||
+ | System.out.println(getName()+" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | =====스레드 동기화===== | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx10 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 게임 객체 생성 | ||
+ | SmartPhoneGame game = new SmartPhoneGame(); | ||
+ | |||
+ | // 플레이어1 객체 생성 후 스레드 실행 | ||
+ | Player1 p1 = new Player1(); | ||
+ | p1.setSmartPhoneGame(game); | ||
+ | p1.start(); | ||
+ | |||
+ | // 플레이어2 객체 생성 후 스레드 실행 | ||
+ | Player2 p2 = new Player2(); | ||
+ | p2.setSmartPhoneGame(game); | ||
+ | p2.start(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | // 스마트폰게임 클래스 | ||
+ | class SmartPhoneGame { | ||
+ | private int level; // 레벨 | ||
+ | |||
+ | public int getLevel() { | ||
+ | return this.level; | ||
+ | } | ||
+ | |||
+ | |||
+ | public synchronized void increaseLevel() { | ||
+ | while (true) { | ||
+ | this.level++; | ||
+ | try { | ||
+ | Thread.sleep(1000); | ||
+ | } catch (InterruptedException e) {} | ||
+ | // 현재 스레드의 이름과 레벌 출력 | ||
+ | System.out.println(Thread.currentThread().getName()+" | ||
+ | if (this.level == 5) { | ||
+ | notifyAll(); | ||
+ | try { | ||
+ | wait(); | ||
+ | } catch (InterruptedException e) {} | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | |||
+ | // 레벨이 10의 배수가 되면 종료 | ||
+ | if (this.level % 10 == 0) break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | // 플레이어1 스레드 | ||
+ | class Player1 extends Thread { | ||
+ | private SmartPhoneGame game; | ||
+ | |||
+ | public void setSmartPhoneGame(SmartPhoneGame game) { | ||
+ | this.setName(" | ||
+ | this.game = game; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void run() { | ||
+ | game.increaseLevel(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // 플레이어2 스레드 | ||
+ | class Player2 extends Thread { | ||
+ | private SmartPhoneGame game; | ||
+ | |||
+ | public void setSmartPhoneGame(SmartPhoneGame game) { | ||
+ | this.setName(" | ||
+ | this.game = game; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void run() { | ||
+ | game.increaseLevel(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx14 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 통장 객체 생성 | ||
+ | Account acc = new Account(); | ||
+ | |||
+ | // 엄마스레드 객체 생성 | ||
+ | Mother mother = new Mother(acc); | ||
+ | // 아들스레드 객체 생성 | ||
+ | Son son = new Son(acc); | ||
+ | |||
+ | |||
+ | mother.start(); | ||
+ | son.start(); | ||
+ | |||
+ | try { | ||
+ | Thread.sleep(10000); | ||
+ | } catch (InterruptedException e) {} | ||
+ | |||
+ | // 스레드 중지 | ||
+ | son.interrupt(); | ||
+ | mother.interrupt(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | // 통장 클래스 | ||
+ | class Account { | ||
+ | int money; | ||
+ | synchronized void withdraw() { | ||
+ | while(money == 0) { | ||
+ | try { | ||
+ | wait(); | ||
+ | }catch(InterruptedException e) { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | notifyAll(); | ||
+ | System.out.println(Thread.currentThread().getName() + money + "원 출금" | ||
+ | money = 0; | ||
+ | } | ||
+ | synchronized void deposit() { | ||
+ | while(money > 0) { | ||
+ | try { | ||
+ | wait(); | ||
+ | } catch(InterruptedException e) { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | // 랜덤 입금 1~5만원 | ||
+ | money = (int)((Math.random()*5)+1)*10000; | ||
+ | notifyAll(); | ||
+ | System.out.println(); | ||
+ | System.out.println(Thread.currentThread().getName() + money + "원 입금" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // 엄마 스레드 | ||
+ | class Mother extends Thread { | ||
+ | Account account; | ||
+ | |||
+ | Mother(Account account) { | ||
+ | super(" | ||
+ | this.account = account; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void run() { | ||
+ | while(true) { | ||
+ | try { | ||
+ | account.deposit(); | ||
+ | sleep((int)(Math.random()*2000)); | ||
+ | } catch (InterruptedException e) { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | //아들 스레드 | ||
+ | class Son extends Thread { | ||
+ | Account account; | ||
+ | |||
+ | Son(Account account) { | ||
+ | super(" | ||
+ | this.account = account; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void run() { | ||
+ | while(true) { | ||
+ | try { | ||
+ | account.withdraw(); | ||
+ | sleep((int)(Math.random()*300)); | ||
+ | } catch (InterruptedException e) { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | =====데몬 스레드===== | ||
+ | |||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx12 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 스레드 객체 생성 | ||
+ | DaemonThread dt1 = new DaemonThread(); | ||
+ | DaemonThread dt2 = new DaemonThread(); | ||
+ | |||
+ | // 데몬스레드로 설정 | ||
+ | dt1.setDaemon(true); | ||
+ | dt2.setDaemon(true); | ||
+ | |||
+ | dt1.start(); | ||
+ | dt2.start(); | ||
+ | |||
+ | try { | ||
+ | Thread.sleep(5000); | ||
+ | } catch (InterruptedException e) {} | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class DaemonThread extends Thread { | ||
+ | public void run() { | ||
+ | while(true) { | ||
+ | System.out.println(getName()); | ||
+ | try { | ||
+ | sleep(500); | ||
+ | } catch (InterruptedException e) {} | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | =====스레드 그룹===== | ||
+ | <code java> | ||
+ | package chapter17; | ||
+ | |||
+ | public class ThreadEx13 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 그룹 객체 생성 | ||
+ | ThreadGroup group = new ThreadGroup(" | ||
+ | MyThread t1 = new MyThread(group, | ||
+ | MyThread t2 = new MyThread(group, | ||
+ | MyThread t3 = new MyThread(group, | ||
+ | |||
+ | |||
+ | t1.start(); | ||
+ | t2.start(); | ||
+ | t3.start(); | ||
+ | |||
+ | try { | ||
+ | Thread.sleep(3000); | ||
+ | } catch (InterruptedException e) {} | ||
+ | |||
+ | // 모든 스레드 중지 | ||
+ | group.interrupt(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class MyThread extends Thread { | ||
+ | MyThread(ThreadGroup group, String name) { | ||
+ | super(group, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void run() { | ||
+ | while(true) { | ||
+ | System.out.println(getName()); | ||
+ | try { | ||
+ | sleep(500); | ||
+ | } catch (InterruptedException e) { | ||
+ | System.out.println(getName() + " interrupted 발생" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | System.out.println(getName()+" | ||
+ | } | ||
+ | } | ||
+ | </ | ||