25일차 강의 정리
스레드 스케줄링
Thread.MIN_PRIORITY => 1
Thread.NORM_PRIORITY => 5
Thread.MAX_PRIORITY => 10
1~10 까지의 우선순위가 있어서, 높을 수록 실행 할 확률이 높아 진다고 보면된다.(먼저실행할 확률)
스케쥴링의 두번째로는 join()이 있어서, 우선순위로 지정한 메소드 먼저 끝낼수있게 한다.
소스1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | class Pm00 extends Thread{ Pm00 me; public Pm00(String name){ super(name); } void setMe(Pm00 me){ this.me = me; } @Override public void run() { try{ if(me!=null)me.join(); }catch(InterruptedException ee){ ee.printStackTrace(); } for (int i = 0; i < 5; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(this.getName()+" 스레드 순위 "+this.getPriority()); //디폴트로 5,항상동일 } } } public class Ex00 { public static void main(String[] args) { Pm00 me1 = new Pm00("1번"); Pm00 me2 = new Pm00("2번"); Pm00 me3 = new Pm00("3번"); me1.setPriority(Thread.MIN_PRIORITY);//1 //디폴트 순위는 무조건 5로시작 me2.setPriority(Thread.NORM_PRIORITY);//5 //1(낮은순위) ~ 10(높은 순위) me3.setPriority(Thread.MAX_PRIORITY);//10 //우선순위를 높게주면 실행기회가높아진다 me1.start(); me2.setMe(me1); //me1이끝날때까지 기다린다 me2.start(); me3.setMe(me2); //me2이끝날때까지 기다린다 me3.start(); System.out.println("main end."); } } | cs |
결과1
소스2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | class Am02 implements Runnable{ @Override public void run() { for (int i = 0; i < 6; i++) { Thread thr = Thread.currentThread(); thr.setPriority(Thread.MIN_PRIORITY+i); try { thr.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(thr.getName()+":"+thr.getPriority()); } } } public class Ex02 { public static void main(String[] args) { Am02 am2 = new Am02(); Thread thr1 = new Thread(am2,"th1"); Thread thr2 = new Thread(am2,"th2"); Thread thr3 = new Thread(am2,"th3"); try{ thr1.start(); thr1.join(100); //( )시간동안 thr1을 실행 thr2.start(); thr2.join(); thr3.start(); thr3.join(); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println("main end"); } } | cs |
결과2
동기화
소스
특정블록의 동기화
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | class Bank { int money; public Bank() { money = 0; } } class ATM extends Thread{ Bank bank; public ATM(Bank bank){ this.bank = bank; } //특정 블록의 동기화방법 @Override public void run() { add(1000); try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } del(500); try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } del(300); try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } del(200); } void add(int money){ synchronized (bank){ //임계영역코딩(동기화할 객체 또는 동기화할 클래스명) this.bank.money+=money; System.out.println("cd입금: "+money+"원, 은행잔고: "+this.bank.money+"원"); } }//add end synchronized void del(int money){ synchronized (bank) { if (this.bank.money >= 0 && this.bank.money >= money) { this.bank.money -= money; System.out.println("cd출금: " + money + "원, 은행잔고: " + this.bank.money + "원"); } else { System.out.println("잔고부족"); } } }//del end }//ATM class end public class Ex03 { public static void main(String[] args) { Bank bank = new Bank(); ATM atm1 = new ATM(bank); ATM atm2 = new ATM(bank); ATM atm3 = new ATM(bank); System.out.println("잔고 : "+ bank.money+"원"); atm1.start(); atm2.start(); atm3.start(); try { atm1.join(); atm2.join(); atm3.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("잔고 : "+ bank.money+"원"); } } | cs |
메서드 동기화
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | class Bank { int money; public Bank() { money = 0; } // 메서드 동기화방법 synchronized void add(int money) { this.money += money; System.out.println("cd입금: " + money + "원, 은행잔고: " + this.money + "원"); }// add end synchronized void del(int money) { if (this.money >= 0 && this.money >= money) { this.money -= money; System.out.println("cd출금: " + money + "원, 은행잔고: " + this.money + "원"); } else { System.out.println("잔고부족"); } }// del end } class ATM extends Thread{ Bank bank; public ATM(Bank bank){ this.bank = bank; } //메서드 동기화방법 @Override public void run() { bank.add(1000); try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } bank.del(500); try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } bank.del(300); try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } bank.del(200); } }//ATM class end public class Ex03 { public static void main(String[] args) { Bank bank = new Bank(); ATM atm1 = new ATM(bank); ATM atm2 = new ATM(bank); ATM atm3 = new ATM(bank); System.out.println("잔고 : "+ bank.money+"원"); atm1.start(); atm2.start(); atm3.start(); try { atm1.join(); atm2.join(); atm3.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("잔고 : "+ bank.money+"원"); } } | cs |
결과
3개의 스레드가 동시에돌아도 문제없다.
소스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | class ATM2 implements Runnable{ int money; public ATM2() {this.money=0;} @Override public void run() { add(1000); try { Thread.currentThread().sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } del(500); try { Thread.currentThread().sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } del(300); try { Thread.currentThread().sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } del(200); } void add(int money){ synchronized (this) { this.money+=money; System.out.println(Thread.currentThread().getName()); System.out.println("cd입금-"+money+"원, 은행잔고:"+this.money+"원"); } } void del(int money){ synchronized (this) { if(this.money>=0 && this.money>=money){ this.money -=money; System.out.println(Thread.currentThread().getName()); System.out.println("cd출금-"+money+"원, 은행잔고:"+this.money+"원"); }else{ System.out.println("잔고 부족"); } } } } public class Ex05 { public static void main(String[] args) { ATM2 bank = new ATM2(); System.out.println("잔고:"+bank.money+"원"); Thread atm1 = new Thread(bank,"ATM1"); Thread atm2 = new Thread(bank,"ATM2"); Thread atm3 = new Thread(bank,"ATM3"); atm1.start(); atm2.start(); atm3.start(); try { atm1.join(); atm2.join(); atm3.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("잔고:"+bank.money+"원"); } } | cs |
결과
*state...>?
'* Programming > JAVA' 카테고리의 다른 글
SMS 참고 사이트 (0) | 2018.03.08 |
---|---|
DAY25 IO (0) | 2016.08.11 |
DAY24 스레드 (0) | 2016.08.08 |
DAY24 제네릭2(메소드제네릭,와일드카드) (0) | 2016.08.08 |
DAY23 제네릭1 (0) | 2016.08.08 |