[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item7. ๋‹ค ์“ด ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ผ.

2023. 1. 21. 19:14ใ†JAVA/Effective JAVA

728x90
item07. ๋‹ค ์“ด ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ผ. 

์ž๋ฐ”์—์„œ๋Š” ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ๋•๋ถ„์— ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ C๋‚˜ C++์— ๋น„ํ•ด์„œ๋Š” ํŽธํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ๋ ‡๋‹ค๊ณ  ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ์•„์˜ˆ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. 

 

๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ง์ ‘ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๊ณ , ์ด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋Š” ๊ฒ‰์œผ๋กœ๋Š” ์ž˜ ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์Šคํ…œ์— ์ˆ˜๋…„๊ฐ„ ์ž ๋ณตํ•ด ์žˆ๋‹ค๊ฐ€.. ์‹ฌํ•œ ๊ฒฝ์šฐ์—๋Š” ๋””์Šคํฌ ํŽ˜์ด์ง€์ด๋‚˜ OutoutMemoryError๋ฅผ ์ผ์œผ์ผœ์„œ ํ”„๋กœ๊ทธ๋žจ์ด ์˜ˆ๊ธฐ์น˜ ์•Š๊ฒŒ ์ข…๋ฃŒ๋  ์ˆ˜๋„ ์žˆ๋‹ค.. 

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ ์–ด๋–ค ๊ฒฝ์šฐ์— ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•ด๊ฒฐ? ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•ด์„œ item07์—์„œ ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. 

 

 

๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ๐Ÿ‘พ

: ์–ด๋–ค ๊ฐ์ฒด์— ๋Œ€ํ•œ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๋Š” ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์˜ ๋Œ€์ƒ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค. 

 

- stack

package chapter01.item07.stack;

import java.util.Arrays;
import java.util.EmptyStackException;

/**
 * item07. ๋‹ค ์“ด ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ผ.
 */
public class Stack
{
	private Object[] elements;
	private int size = 0;
	private static final int DEFAULT_INITIAL_CAPACITY = 16;
	
	public Stack() {
		elements = new Object[DEFAULT_INITIAL_CAPACITY];
	}
	
	public void push(Object e)
	{
		ensureCapacity();
		elements[size++] = e;
	}
	
	// ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋Š” ์ฝ”๋“œ
	public Object pop()
	{
		if (size == 0) {
			throw new EmptyStackException();
		}
		return elements[--size];
	}
	
	/**
	 * ์›์†Œ๋ฅผ ์œ„ํ•œ ๊ณต๊ฐ„์„ ์ ์–ด๋„ ํ•˜๋‚˜ ์ด์ƒ ํ™•๋ณดํ•œ๋‹ค.
	 * ๋ฐฐ์—ด ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ค์•ผ ํ•  ๋•Œ๋งˆ๋‹ค ๋Œ€๋žต 2๋ฐฐ์”ฉ ๋Š˜๋ฆฐ๋‹ค.
	 */
	private void ensureCapacity()
	{
		if (elements.length == size) {
			elements = Arrays.copyOf(elements, 2 * size + 1);
		}
	}
	
	
	public static void main(String[] args)
	{
		Stack stack = new Stack();
		for (String arg : args)
		{
			stack.push(arg);
		}
		
		while (true)
		{
			System.out.println("stack.pop() = " + stack.pop());
		}
	}
}

์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”์—์„œ ์˜ˆ์ œ๋กœ ์ œ๊ณตํ•œ Stack ์ƒ˜ํ”Œ ์ฝ”๋“œ์ธ๋ฐ ์ด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋ฌธ์ œ๊ฐ€ ์—†์–ด๋ณด์ธ๋‹ค.. 

์‹ค์ œ๋กœ๋„ ๊ฐ์ข… ํ…Œ์ŠคํŠธ๋ฅผ ๋‹ค ํ•˜๋”๋ผ๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. 

 

ํ•˜์ง€๋งŒ '๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜'๋ผ๋Š” ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•œ๋‹ค. 

์ด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์Šคํƒ์ด ์ปค์กŒ๋‹ค๊ฐ€ ์ค„์–ด๋“ค์—ˆ์„ ๋•Œ ์Šคํƒ์—์„œ ๊บผ๋‚ด์ง„ ๊ฐ์ฒด๋“ค์„ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๊ฐ€ ํšŒ์ˆ˜ํ•˜์ง€ ์•Š๋Š”๋‹ค!

๊ฐ์ฒด๋“ค์„ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋ฐ๋„ ํšŒ์ˆ˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. 

์™œ๋ƒํ•˜๋ฉด Stack์ด ๊ฐ์ฒด๋“ค์˜ ๋‹ค ์“ด ์ฐธ์กฐ๋ฅผ ์—ฌ์ „ํžˆ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์—์„œ๋Š” ๊ฐ์ฒด ์ฐธ์กฐ ํ•˜๋‚˜๋ฅผ ์‚ด๋ ค๋‘๋ฉด ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๋Š” ๊ทธ ๊ฐ์ฒด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ทธ ๊ฐ์ฒด๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ํšŒ์ˆ˜ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž ์žฌ์ ์œผ๋กœ๋Š” ์„ฑ๋Šฅ์— ์•…์˜ํ–ฅ์„ ๋ผ์น  ์ˆ˜ ์žˆ๋‹ค. 

 

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•1) ํ•ด๋‹น ์ฐธ์กฐ๋ฅผ ๋‹ค ์ผ์„ ๋•Œ null ์ฒ˜๋ฆฌ (์ฐธ์กฐ ํ•ด์ œ) ํ•˜๋ฉด ๋œ๋‹ค. 

package chapter01.item07.stack;

import java.util.Arrays;
import java.util.EmptyStackException;

/**
 * item07. ๋‹ค ์“ด ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ผ.
 */
public class Stack
{
   private Object[] elements;
   private int size = 0;
   private static final int DEFAULT_INITIAL_CAPACITY = 16;
   
   public Stack() {
      elements = new Object[DEFAULT_INITIAL_CAPACITY];
   }
   
   public void push(Object e)
   {
      ensureCapacity();
      elements[size++] = e;
   }
   
   // ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋Š” ์ฝ”๋“œ
// public Object pop()
// {
//    if (size == 0) {
//       throw new EmptyStackException();
//    }
//    return elements[--size];
// }
   
   /**
    * ์›์†Œ๋ฅผ ์œ„ํ•œ ๊ณต๊ฐ„์„ ์ ์–ด๋„ ํ•˜๋‚˜ ์ด์ƒ ํ™•๋ณดํ•œ๋‹ค.
    * ๋ฐฐ์—ด ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ค์•ผ ํ•  ๋•Œ๋งˆ๋‹ค ๋Œ€๋žต 2๋ฐฐ์”ฉ ๋Š˜๋ฆฐ๋‹ค.
    */
   private void ensureCapacity()
   {
      if (elements.length == size) {
         elements = Arrays.copyOf(elements, 2 * size + 1);
      }
   }
   
   // ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ์—†์ด ์ œ๋Œ€๋กœ ๊ตฌํ˜„ํ•œ pop ๋ฉ”์„œ๋“œ
   public Object pop()
   {
      if (size == 0) {
         throw new EmptyStackException();
      }
      Object result = elements[--size];
      elements[size] = null; // ๋ฐฉ๋ฒ•1) ๋‹ค ์“ด ์ฐธ์กฐ ํ•ด์ œ -> ๋ฐ”๋žŒ์งํ•œ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋‹ค -> ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ null ์ฒ˜๋ฆฌํ•˜๋Š” ์ผ์€ ์˜ˆ์™ธ์ ์ด์–ด์•ผ ํ•œ๋‹ค.
      return result;
   }
   
   public static void main(String[] args)
   {
      Stack stack = new Stack();
      for (String arg : args)
      {
         stack.push(arg);
      }
      
      while (true)
      {
         System.out.println("stack.pop() = " + stack.pop());
      }
   }
}

-> elements[size] = null  ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ๋‹ค ์“ด ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•ด์คฌ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ null ์ฒ˜๋ฆฌํ•˜๋Š” ์ผ์€ ์˜ˆ์™ธ์ ์ธ ๊ฒฝ์šฐ์—ฌ์•ผ ํ•œ๋‹ค!! 

 

 

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• 2) ๊ทธ ์ฐธ์กฐ๋ฅผ ๋‹ด์€ ๋ณ€์ˆ˜๋ฅผ ์œ ํšจ ๋ฒ”์œ„(scope) ๋ฐ–์œผ๋กœ ๋ฐ€์–ด๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. 

๋ณ€์ˆ˜์˜ ๋ฒ”์œ„๋ฅผ ์ตœ์†Œ๊ฐ€ ๋˜๊ฒŒ ์ •์˜ํ•ด์„œ ๋” ์ด์ƒ ์ฐธ์กฐํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ์œ ํšจ ๋ฒ”์œ„ ๋ฐ–์œผ๋กœ ๋ฐ€์–ด๋‚ด์„œ ๋‹ค ์“ด ์ฐธ์กฐ ๊ฐ์ฒด๋ฅผ ํ•ด์ œํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ผ๋ฐ˜์ ์œผ๋กœ ์ž๊ธฐ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ง์ ‘ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค๋ผ๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ํ•ญ์‹œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. 

 

- cache

์œ„์— ์˜ˆ์‹œ์—์„œ๋Š” ์Šคํƒ์„ ์˜ˆ๋กœ ๋“ค์—ˆ๋Š”๋ฐ ์บ์‹œ ์—ญ์‹œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ์ผ์œผํ‚ค๋Š” ์ฃผ๋ฒ”์ด๋‹ค. 

์บ์‹œ ์—ญ์‹œ ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ์บ์‹œ์— ๋„ฃ๊ณ  ๋‚˜์„œ ์ด ์‚ฌ์‹ค์„ ๊นŒ๋จน๊ณ  ์žˆ๋‹ค๊ฐ€ ๊ทธ ๊ฐ์ฒด๋ฅผ ๋‹ค ์“ด ๋’ค๋กœ๋„ ํ•œ์ฐธ ๊ทธ๋Œ€๋กœ ๋†”๋‘๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. 

 

package chapter01.item07.cache;

import java.time.LocalDateTime;

public class CacheKey
{
   private Integer value;
   private LocalDateTime created;
   
   public CacheKey(Integer value)
   {
      this.value = value;
      this.created = LocalDateTime.now();
   }
   
   @Override
   public boolean equals(Object obj)
   {
      return this.value.equals(obj);
   }
   
   @Override
   public int hashCode()
   {
      return this.value.hashCode();
   }
   
   public LocalDateTime getCreated()
   {
      return created;
   }
   
   @Override
   public String toString()
   {
      return "CacheKey{" +
            "value=" + value +
            ", created=" + created +
            '}';
   }
}

 

package chapter01.item07.cache;

import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.cglib.core.WeakCacheKey;

/**
 * item07. ๋‹ค ์“ด ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ผ.
 */
public class PostRepository
{
   private Map<CacheKey, Post> cache;
   
   public PostRepository()
   {
      this.cache = new HashMap<>();
   }
   
   public Post getPostById(Integer id)
   {
      CacheKey key = new CacheKey(id);
      
      if (cache.containsKey(key)) {
         return cache.get(key);
      }else {
         Post post = new Post(); // TODO DB์—์„œ ์ฝ์–ด์˜ค๊ฑฐ๋‚˜ REST API๋ฅผ ํ†ตํ•ด ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
         cache.put(key, post);
         return post;
      }
   }
   
   public Map<CacheKey, Post> getCache()
   {
      return cache;
   }
}
@Test
void cache() throws InterruptedException
{
   PostRepository postRepository = new PostRepository();
   Integer p1 = 1;
   postRepository.getPostById(p1);
   
   assertFalse(postRepository.getCache().isEmpty());
   
   System.out.println("run gc");
   System.gc();
   System.out.println("wait");
   Thread.sleep(3000L);
   
   assertTrue(postRepository.getCache().isEmpty());
}

์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•œ ํ›„์— ์บ์‹œ๋ฅผ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๋ฉด ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์—์„œ ์บ์‹œ๋Š” ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๊ณณ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— GC์˜ ๋Œ€์ƒ์ด ๋˜์ง€ ์•Š์•„์„œ assertTrue์—์„œ ํ…Œ์ŠคํŠธ ์‹คํŒจํ•œ๋‹ค. (false ์ด๊ธฐ ๋•Œ๋ฌธ์—) 

 

 

package chapter01.item07.cache;

import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.cglib.core.WeakCacheKey;

/**
 * item07. ๋‹ค ์“ด ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ผ.
 */
public class PostRepository
{
   private Map<CacheKey, Post> cache;
   
   public PostRepository()
   {
      this.cache = new WeakHashMap<>();
   }
   
   public Post getPostById(Integer id)
   {
      CacheKey key = new CacheKey(id);
      
      if (cache.containsKey(key)) {
         return cache.get(key);
      }else {
         Post post = new Post(); // TODO DB์—์„œ ์ฝ์–ด์˜ค๊ฑฐ๋‚˜ REST API๋ฅผ ํ†ตํ•ด ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
         cache.put(key, post);
         return post;
      }
   }
   
   public Map<CacheKey, Post> getCache()
   {
      return cache;
   }
}

 ์ฑ…์— ๋ณด๋ฉด 'ํ‚ค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋™์•ˆ๋งŒ ์—”ํŠธ๋ฆฌ๊ฐ€ ์‚ด์ด ์žˆ๋Š” ์บ์‹œ๊ฐ€ ํ•„์š”ํ•œ ์ƒํ™ฉ์ด๋ผ๋ฉด WeakHashMap์„ ์‚ฌ์šฉํ•ด ์บ์‹œ๋ฅผ ๋งŒ๋“ค์ž'

๋ผ๋Š” ๋ง์ด ์žˆ๋Š”๋ฐ ์ด์™€ ๊ฐ™์ด ์›๋ž˜๋Š” HashMap์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” ํ…Œ์ŠคํŠธ ์‹คํŒจํ–ˆ์ง€๋งŒ,, WeakHashMap์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค ์“ด ์—”ํŠธ๋ฆฌ๋Š” ์ฆ‰์‹œ ์ž๋™์œผ๋กœ ์ œ๊ฑฐ๊ฐ€ ๋ผ์„œ ํ…Œ์ŠคํŠธ ์„ฑ๊ณตํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๐Ÿ’ก WeakHashMap์„ ์•„์ง ์ž˜ ๋ชฐ๋ผ์„œ ์‚ฌ์‹ค ์ด ๋ถ€๋ถ„์€ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค,, ๋‹ค์Œ ๊ฐ•์˜์— ์„ค๋ช…์ด ์žˆ์–ด์„œ ๋“ฃ๊ณ  ๋‚ด์šฉ์„ ๋” ์ถ”๊ฐ€ํ•ด์•ผ๊ฒ ๋‹ค. 

 

 

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•3) ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์Šค๋ ˆ๋“œ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

@Test
void backgroundThread() throws InterruptedException
{
   ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
   PostRepository postRepository = new PostRepository();
   CacheKey key1 = new CacheKey(1);
   postRepository.getPostById(1);
   
   Runnable removeOldCache = () -> {
      System.out.println("running removeOldCache task");
      Map<CacheKey, Post> cache = postRepository.getCache();
      Set<CacheKey> cacheKeys = cache.keySet();
      Optional<CacheKey> key = cacheKeys.stream()
            .min(Comparator.comparing(CacheKey::getCreated));
      key.ifPresent((k)-> {
         System.out.println("removing " + k);
         cache.remove(k);
      });
   };
      
      System.out.println("The time is : " + new Date());
      
      executor.scheduleAtFixedRate(removeOldCache, 1, 3,
                            TimeUnit.SECONDS);
      
      Thread.sleep(20000L);
      
      executor.shutdown();
   
}

ScheduledThreadPoolExecutor๋ฅผ ํ†ตํ•ด ์“ฐ์ง€ ์•Š์€ ์—”ํŠธ๋ฆฌ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ณผ์ •์„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์Šค๋ ˆ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

ํ…Œ์ŠคํŠธ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ ์ฒ˜์Œ์—๋Š” ์ œ๊ฑฐํ•  ์—”ํŠธ๋ฆฌ๊ฐ€ ์žˆ์–ด์„œ ์ œ๊ฑฐํ•œ ํ›„์—๋Š” ์ด์ œ ๋” ์ด์ƒ ์ œ๊ฑฐํ•  ๊ฒƒ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— 'running removeOldCache task' ๋งŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ๋ฆฌ์Šค๋„ˆ (listener) , ์ฝœ๋ฐฑ (callback) 

: ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์˜ ์„ธ๋ฒˆใ…‰?ใ… ์ฃผ๋ฒ”์€ ๋ฐ”๋กœ ๋ฆฌ์Šค๋„ˆ ํ˜น์€ ์ฝœ๋ฐฑ์ด๋ผ ๋ถ€๋ฅด๋Š” ๊ฒƒ์ด๋‹ค. 

ํด๋ผ์ด์–ธํŠธ์นด ์ฝœ๋ฐฑ์„ ๋“ฑ๋ก๋งŒ ํ•˜๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ํ•ด์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ญ”๊ฐ€ ์กฐ์น˜ํ•ด์ฃผ์ง€ ์•Š๋Š” ํ•œ ์ฝœ๋ฐฑ์€ ์ œ๊ฑฐ๋˜์ง€ ์•Š๊ณ  ๊ณ„์† ์Œ“์ด๊ฒŒ๋งŒ ๋œ๋‹ค.. 

 

-> ์ด๋Ÿด ๋•Œ๋Š” ์ฝœ๋ฐฑ์„ ์•ฝํ•œ ์ฐธ์กฐ (Weak reference)๋กœ ์ €์žฅํ•˜๋ฉด ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๊ฐ€ ์ฆ‰์‹œ ์ˆ˜๊ฑฐํ•ด๊ฐ„๋‹ค. (ex. WeakHashMap ์— ํ‚ค๋กœ ์ €์žฅ) 

 

 

 

 

 

 

 

 

 

 

 

 

728x90