-
[Java] μλ° μ°λ λμ λκΈ°νAndroid 2021. 2. 7. 16:57λ°μν
- μ΄ κΈμ "μλ° μ¨λΌμΈ μ€ν°λ" 곡λΆνμ¬ μμ±ν κΈμ λλ€.
μ°λ λ λκΈ°νλ?
λ©ν° μ°λ λ νλ‘κ·Έλλ°μμ λκΈ°ν μμ μ΄ νμμ λλ€. μ°λ λ λκΈ°νλ μ¬λ¬ μ°λ λκ° λμΌν 리μμ€λ₯Ό 곡μ νμ¬ μ¬μ©νκ² λλ©΄ μλ‘μ κ²°κ³Όμ μν₯μ μ£ΌκΈ° λλ¬Έμ λ°©μ§νλ κΈ°λ²μ λλ€.
μ°λ λ λκΈ°νλ₯Ό νκΈ° μν΄μλ μκ³μμ(critical section)κ³Ό λ½(lock)μ μ¬μ©ν©λλ€. μκ³μμμΌλ‘ μ€μ ν ꡬμμ λμμ 리μμ€λ₯Ό μ¬μ©ν μ μλ ꡬμμ΄κ³ , λ½μ νλν μ°λ λμ λν΄μλ§ λ¦¬μμ€λ₯Ό μ¬μ©νλλ‘ νλ λ°©μμ λλ€.synchronizedλ₯Ό μ΄μ©ν λκΈ°ν
μλ°μμλ synchronized ν€μλλ₯Ό μ¬μ©νμ¬ μκ³μμμ μ§μ νμ¬ λμμ 곡μ μμμ μ°¨μ§νμ§ μλλ‘ κ°μ ν©λλ€. synchronizedλ₯Ό μ¬μ©νλ λ°©λ²μ λ©μλ μ체μ μ μ©νκ±°λ, μ½λ λΈλ‘μ μ μ©ν μ μμ΅λλ€.
-
λ©μλ μ 체λ₯Ό μκ³μμμ μ§μ : λ©μλ μ μΈ μμ synchronized ν€μλ μΆκ°, μ΄ λ©μλκ° νΈμΆλλ©΄ λ©μλλ₯Ό κ°μ§κ³ μλ κ°μ²΄μ λν λ½μ νλνμ¬ μμ μ μννλ€κ° λ©μλ μ’ λ£λλ©΄ λ½μ λ°λ©ν©λλ€.
-
νΉμ μμλ§ μκ³μμμ μ§μ : synchronized ν€μλλ‘ μμνλ λΈλ‘μΌλ‘ μ§μ , νΉμ κ°μ²΄λ₯Ό μ§μ νμ¬ λΈλ‘ λ΄μμ κ°μ²΄μ λν λ½μ νλνκ³ λΈλ‘μ λ²μ΄λλ©΄ λ½μ λ°λ©ν©λλ€.
synchronized μ€μ΅ μ½λ
- μ°Έμ‘°(https://kyun2.tistory.com/12)
public class Student { int bookCount = 5; // 곡μ μμ public int getBookCount() { return bookCount; } public void setBookCount(int bookCount) { this.bookCount = bookCount; } public void borrowBook() throws InterruptedException { int m = bookCount; Thread.sleep(2000); bookCount = m+1; System.out.println("λμΆμλ£"); } public void returnBook() throws InterruptedException { int m = bookCount; Thread.sleep(3000); bookCount = m-1; System.out.println("λ°λ©μλ£"); } }
public class Library { public static Student student = new Student(); }
public class BorrowThread extends Thread{ @Override public void run() { try { Library.student.borrowBook(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("νμμ΄ λΉλ¦° μ΄ μ± μ κ°―μ : "+Library.student.getBookCount()); } }
public class ReturnThread extends Thread{ @Override public void run() { try { Library.student.returnBook(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("νμμ΄ λΉλ¦° μ΄ μ± μ κ°―μ : "+Library.student.getBookCount()); } }
public class Main { public static void main(String[] args) { System.out.println("νμ¬ λμΆν μ± μ κ°―μ : "+Library.student.getBookCount()); BorrowThread bt = new BorrowThread(); ReturnThread rt = new ReturnThread(); bt.setPriority(10); bt.start(); rt.start(); try{ bt.join(); rt.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }
λ€μ μμ λ λκΈ°νλ₯Ό νμ§ μμμ λ¬Έμ κ° λλ μ½λμ λλ€. μ½λλ₯Ό λ³΄κ³ μ΄λ μ§μ μμ λ¬Έμ κ° μκΈΈμ§ μλ€λ©΄ λκΈ°νμ λν΄ μ΄λ―Έ μ΄λμ λ μκ³ κ³μ κ²λλ€.
μ€ν κ²°κ³Όλ₯Ό 보면 νμμ΄ νμ¬ λμΆν μ± μ μ΄ 5κΆμ΄μμ΅λλ€. κ·Έλ¦¬κ³ ν κΆμ λμΆν ν λ°λ©μ νμ¬ νμ¬ νμμ΄ λΉλ¦° μ± μ κ°―μλ μ²μκ³Ό λμΌν΄μΌ ν©λλ€. νμ§λ§ λμΆνκ³ λ ν μ± μ 6κΆμ΄ λμμ§λ§, λ°λ©μ νλ 5κΆμ΄ μλ 4κΆμ΄ λμμ΅λλ€.
public void borrowBook() throws InterruptedException { int m = bookCount; Thread.sleep(2000); bookCount = m+1; System.out.println("λμΆμλ£"); } public void returnBook() throws InterruptedException { int m = bookCount; Thread.sleep(3000); bookCount = m-1; System.out.println("λ°λ©μλ£"); }
μμΈμ λΆμν΄ λ³΄μλ©΄, BorrowThreadλ borrowBook λ©μλλ₯Ό νΈμΆνκ³ ReturnThreadλ returnBook λ©μλλ₯Ό λμμ νΈμΆν©λλ€. κ·Έμ λ°λΌ, μ§μλ³μ mμλ νμ¬ bookCountμΈ 5κ° λͺ¨λ λμ μ΄ λκ³ , λͺ μ΄κ°μ λλ μ΄ ν bookCountμ κ°μ λμ νκΈ° λλ¬Έμ +1μΌ λλ 6μ΄λκ³ -1μΌ λλ 4κ° λλ κ²μ λλ€.
μ΄λ₯Ό λ°©μ§νκΈ° μν΄μλ synchronized ν€μλλ‘ μκ³μμμ μ§μ νμ¬ λκΈ°νλ₯Ό ν΄μΌ ν©λλ€.public synchronized void borrowBook() throws InterruptedException { int m = bookCount; Thread.sleep(2000); bookCount = m+1; System.out.println("λμΆμλ£"); } public synchronized void returnBook() throws InterruptedException { int m = bookCount; Thread.sleep(3000); bookCount = m-1; System.out.println("λ°λ©μλ£"); }
Studentμ λ©μλμΈ boroowBookκ³Ό returnBookμ synchronized ν€μλλ₯Ό μΆκ°νμμ΅λλ€. μ΄ λ©μλλ₯Ό νΈμΆν κ²½μ°μλ Studentκ°μ²΄μ λν λ½μ μ»κ³ λ½μ μ»μ§ λͺ»ν κ°μ²΄λ λ½μ λ°λ©ν λκΉμ§ κΈ°λ€λ €μΌ ν©λλ€.
λ°λΌμ, borrowThreadκ° λ¨Όμ Student κ°μ²΄ λ½μ νλνκ³ borrowBook λ©μλλ₯Ό μ€νν©λλ€. λ©μλκ° μ’ λ£λμ΄ λ½μ΄ λ°λ©λ μ΄ν ReturnThreadμ returnBook λ©μλκ° νΈμΆλ©λλ€.μ€ν κ²°κ³Ό μνλ κ²°κ³Όλ₯Ό μ»μ μ μμ΅λλ€.
public void borrowBook() throws InterruptedException { synchronized (this){ int m = bookCount; Thread.sleep(2000); bookCount = m+1; System.out.println("λμΆμλ£"); } } public void returnBook() throws InterruptedException { synchronized (this){ int m = bookCount; Thread.sleep(3000); bookCount = m-1; System.out.println("λ°λ©μλ£"); } }
λ©ν° μ°λ λ νλ‘κ·Έλλ°μμ μκ³ μμμ μ±λ₯μ μ’μ°νκΈ° λλ¬Έμ κ°λ₯νλ©΄ λ©μλ μ 체 λ½μ κ±°λ κ²λ³΄λ€λ synchronized λΈλμΌλ‘ μκ³μμμ μ΅μνν΄μ ν¨μ¨μ μΈ νλ‘κ·Έλ¨μ μμ±νλ κ²μ΄ μ’μ΅λλ€.
wait(), notify() λκΈ°ν
- μ°Έμ‘°(https://kyun2.tistory.com/12)
public class Student { int bookCount = 0; // 곡μ μμ public int getBookCount() { return bookCount; } public void setBookCount(int bookCount) { this.bookCount = bookCount; } public synchronized void borrowBook() throws InterruptedException { if(bookCount>=10){ try{ System.out.println("10κΆ μ΄κ³Όλ‘ λμΆν μ μμ΅λλ€"); }catch (Exception e){ } } int m = bookCount; bookCount = m+1; System.out.println("μ± λμΆ μ±κ³΅. λμΆν μ± μ κ°―μ : "+bookCount); } public synchronized void returnBook() throws InterruptedException { if(bookCount<=0){ try{ System.out.println("λ°λ©ν μ± μ΄ μμ΅λλ€"); }catch (Exception e){ } } int m = bookCount; bookCount = m-1; System.out.println("μ± λ°λ© μ±κ³΅. λμΆν μ± μ κ°―μ : "+bookCount); } }
public class Library { public static Student student = new Student(); }
public class BorrowThread extends Thread{ @Override public void run() { for(int i=0;i<30;i++){ try { Library.student.borrowBook(); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public class ReturnThread extends Thread{ @Override public void run() { for(int i=0;i<30;i++){ try { Library.student.returnBook(); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public class Main { public static void main(String[] args) { System.out.println("νμ¬ λμΆν μ± μ κ°―μ : "+Library.student.getBookCount()); BorrowThread bt = new BorrowThread(); ReturnThread rt = new ReturnThread(); bt.setPriority(10); bt.start(); rt.start(); try{ bt.join(); rt.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }
λ€μ μ½λλ synchronized ν€μλλ‘ λκΈ°νκ° λμ΄ μκΈ° λλ¬Έμ λμΆν μ± μ μκ° 10κΆμ΄ λλλΌλ BorrowThreadκ° μ’ λ£λ νμ ReturnThreadκ° μ€νλλ κ²μ νμΈν μ μμμ΅λλ€.
μ κ° μνλ κ²°κ³Όλ μ΅λ λΉλ¦΄ μ μλ μλ 10κΆμ΄κ³ 10κΆμ μ΄λ―Έ λΉλ Έμ κ²½μ° λ°λ©λΆν° μ§ννλλ‘ νκ³ μΆμμ΅λλ€. κ·Έλ κ² νκΈ° μν΄μλ λΉλ¦° μ± μ μκ° 10κΆμ΄ λλ©΄ λ½μ λ°λ©νμ¬ ReturnThreadκ° μ€νλλλ‘ νλ λ°©λ²μ λλ€. λν, λ°λ©ν μ± μ μκ° μλ€λ©΄ λμΆμ΄ κ°λ₯νλλ‘ BorrowThreadμκ² λ½μ μ£Όλ λ°©λ²μ λλ€.public synchronized void borrowBook() throws InterruptedException { if(bookCount>=10){ try{ System.out.println("10κΆ μ΄κ³Όλ‘ λμΆν μ μμ΅λλ€"); wait(); }catch (Exception e){ } } int m = bookCount; bookCount = m+1; System.out.println("μ± λμΆ μ±κ³΅. λμΆν μ± μ κ°―μ : "+bookCount); notify(); } public synchronized void returnBook() throws InterruptedException { if(bookCount<=0){ try{ System.out.println("λ°λ©ν μ± μ΄ μμ΅λλ€"); wait(); }catch (Exception e){ } } int m = bookCount; bookCount = m-1; System.out.println("μ± λ°λ© μ±κ³΅. λμΆν μ± μ κ°―μ : "+bookCount); notify(); }
λ€μκ³Ό κ°μ΄ μ½λλ₯Ό μμ νκ² λλ©΄ 10κΆ μ΄κ³Όλ‘ λμΆν μ μκ³ 0κΆμ΄ λλ©΄ λ°λ©ν μ μλλ‘ νμμ΅λλ€.
λ°μν'Android' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Android] Robelctric Testλ (0) 2021.02.17 [Android] Unit Test, JUnit4 (0) 2021.02.16 [Android] μλλ‘μ΄λ μ€νλμ€ NDK μ¬μ©λ² (0) 2021.02.12 [Android] Broadcast? Broadcast Receiverλ? (0) 2021.01.08 [Android] Recycler View μ¬μ©λ² (0) 2021.01.04