CodeGym /Java Course /์ž๋ฐ” ์ปฌ๋ ‰์…˜ /ํŒฌํ…€ ์ฐธ์กฐ

ํŒฌํ…€ ์ฐธ์กฐ

์ž๋ฐ” ์ปฌ๋ ‰์…˜
๋ ˆ๋ฒจ 4 , ๋ ˆ์Šจ 7
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"์•ˆ๋…•, ์•„๋ฏธ๊ณ !"

"์•ˆ๋…•, ๋ฆฌ์‹œ!"

"๊ทธ๋Ÿผ ์˜ค๋Š˜ ํ•˜๋ฃจ ์–ด๋• ์–ด?"

"ํ›Œ๋ฅญํ•ฉ๋‹ˆ๋‹ค! ์˜ค๋Š˜ Bilaabo๋Š” ์žฌ๊ท€์— ๋Œ€ํ•ด ๋งํ–ˆ๊ณ  Ellie๋Š” ์•ฝํ•˜๊ณ  ๋ถ€๋“œ๋Ÿฌ์šด ์ฐธ์กฐ์— ๋Œ€ํ•ด ๋งํ–ˆ์Šต๋‹ˆ๋‹ค."

"๊ทธ๋…€๊ฐ€ ์œ ๋ น ์ฐธ์กฐ์— ๋Œ€ํ•ด ๋งํ–ˆ์Šต๋‹ˆ๊นŒ?"

"PhantomReference์— ๋Œ€ํ•ด ๋ง์”€ํ•˜์‹œ๋Š” ๊ฑด๊ฐ€์š”? ๊ทธ๋…€๋Š” ๊ทธ๊ฒƒ์„ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."

"์ข‹์•„, ๊ทธ๋Ÿผ ๋‚ด๊ฐ€ ์ด ๊ณต๋ฐฑ์„ ๋ฉ”์›Œ๋„ ์ƒ๊ด€์—†๊ธธ ๋ฐ”๋ž˜."

"๋ฌผ๋ก ์ด์ฃ ! ์ฆ๊ฒ๊ฒŒ ๋“ฃ๊ฒ ์Šต๋‹ˆ๋‹ค, ๋ฆฌ์‹œ!"

"์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."

"ํŒฌํ…€ ์ฐธ์กฐ๋Š” ๊ฐ€์žฅ ์•ฝํ•œ ์ฐธ์กฐ์ž…๋‹ˆ๋‹ค. ๊ฐœ์ฒด์— ํŒฌํ…€ ์ฐธ์กฐ ์ด์™ธ์˜ ์ฐธ์กฐ๊ฐ€ ์ „ํ˜€ ์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค."

PhantomReference - 1

"A PhantomReference๋Š” ๋ณต์žกํ•œ ๊ฐœ์ฒด ์‚ญ์ œ ์ ˆ์ฐจ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.  ๊ฐœ์ฒด๊ฐ€ Java ์‹œ์Šคํ…œ ์™ธ๋ถ€์—์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•˜์œ„ ์ˆ˜์ค€ OS ๊ธฐ๋Šฅ์„ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ์ƒํƒœ๋ฅผ ํŒŒ์ผ์— ๊ธฐ๋กํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ๋งค์šฐ ์ค‘์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค."

"์‚ฌ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค."

ํŒฌํ…€ ์ฐธ์กฐ ์ƒ์„ฑ ์˜ˆ
// Special queue for phantom objects
ReferenceQueue<Integer> queue = new ReferenceQueue<Integer>();

// List of phantom references
ArrayList<PhantomReference<Integer>> list = new ArrayList<PhantomReference<Integer>>();

// Create 10 objects and add them to the list using phantom references
for ( int i = 0; i < 10; i++)
{
 Integer x = new Integer(i);
 list.add(new PhantomReference<Integer>(x, queue));
}

"๋งˆ์ง€๋ง‰ ์ค„์— ๋‹ค์‹œ ์ฃผ๋ชฉํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๊ฐœ์ฒด x๊ฐ€ PhantomReference๋กœ ์ „๋‹ฌ๋  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํŒฌํ…€ ์ฐธ์กฐ์˜ ํŠน์ˆ˜ ๋Œ€๊ธฐ์—ด๋„ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค."

"์ด ๋Œ€๊ธฐ์—ด์ด ํ•„์š”ํ•œ ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?"

"๊ทธ๊ฒŒ ๋‚ด๊ฐ€ ์ง€๊ธˆ ๋‹น์‹ ์—๊ฒŒ ๋งํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค."

"ํŒฌํ…€ ์ฐธ์กฐ๊ฐ€ ๋ณด์œ ํ•œ ๊ฐœ์ฒด๋ฅผ ํŒŒ๊ดดํ•˜๋ฉด ํŒŒ๊ดด๋˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ญ์ œ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค! ๊ทธ๊ฒƒ์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์„ธ์š”?!"

"๊ทธ๋ž˜์„œ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๊นŒ?"

"์—ฌ๊ธฐ์—๋Š” ์•ฝ๊ฐ„์˜ ๋‰˜์•™์Šค๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."

"๊ฐ์ฒด์— ๋Œ€ํ•œ ํŒฌํ…€ ์ฐธ์กฐ๋งŒ ๋‚จ์•„ ์žˆ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค."

" 1๋‹จ๊ณ„ . ๋‹ค์Œ ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘ ์ค‘์— ๊ฐœ์ฒด์—์„œ finalize() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ finalize() ๋ฉ”์„œ๋“œ๊ฐ€ ์žฌ์ •์˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ด ๋‹จ๊ณ„๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  2๋‹จ๊ณ„๊ฐ€ ์ฆ‰์‹œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค."

" 2๋‹จ๊ณ„ . ๋‹ค์Œ ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘ ์ค‘์— ๊ฐ์ฒด๋Š” ํŒฌํ…€ ๊ฐ์ฒด์˜ ํŠน์ˆ˜ ๋Œ€๊ธฐ์—ด์— ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค. PhantomReference์—์„œ clear() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์ด ๋Œ€๊ธฐ์—ด์—์„œ ๊ฐ์ฒด๊ฐ€ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค."

"๋ˆ„๊ฐ€ ๋ถˆ๋ €์–ด? ๊ฐœ์ฒด๊ฐ€ ์‚ญ์ œ๋˜์—ˆ์ง€?"

"๊ธ€์Ž„, ๊ฐ์ฒด๋Š” ์‹ค์ œ๋กœ ์šฐ๋ฆฌ ์„ธ๊ณ„(Java ์„ธ๊ณ„)์—์„œ ์ฃฝ์—ˆ์ง€๋งŒ ์‚ฌ๋ผ์ง€์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ์œ ๋ น์œผ๋กœ ๋‚จ์•„ ์žˆ์Šต๋‹ˆ๋‹ค . PhantomReference ์ƒ์„ฑ์ž ์— ์ „๋‹ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ."

"๊ทธ๋ž˜์„œ ์ด ReferenceQueue๋Š” ์‚ฌํ›„ ์„ธ๊ณ„์™€ ๊ฐ™์€ ๊ฑด๊ฐ€์š”?"

"ํ™˜์ƒ ์„ธ๊ณ„์— ๊ฐ€๊น์Šต๋‹ˆ๋‹ค."

"๊ทธ๋ฆฌ๊ณ  ํŒฌํ…€ ๊ฐ์ฒด๋Š” ํŒฌํ…€ ์ฐธ์กฐ์—์„œ clear()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ๋งŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

"์ด์ „ ์˜ˆ์ œ๋ฅผ ๊ณ„์†ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค."

ํŒฌํ…€ ์ฐธ์กฐ ์ƒ์„ฑ ์˜ˆ
// Special queue for phantom objects
ReferenceQueue<Integer> queue = new ReferenceQueue<Integer>();

// List of phantom references
ArrayList<PhantomReference<Integer>> list = new ArrayList<PhantomReference<Integer>>();

// Create 10 objects and add them to the list using phantom references
for ( int i = 0; i < 10; i++)
{
 Integer x = new Integer(i);
 list.add(new PhantomReference<Integer>(x, queue));
}

// Call the garbage collector and hope it will listen to us :)
// It should destroy all phantom reachable objects and put them in the queue
// of phantoms
System.gc();

// Get all objects from the queue
Reference<? extends Integer>referenceFromQueue;
while ((referenceFromQueue = queue.poll()) != null)
{
 // Display the object on the screen
 System.out.println(referenceFromQueue.get());

 // Clear the reference
 referenceFromQueue.clear();
}

"๋‚˜๋Š” ์—ฌ๊ธฐ์„œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๊ณ  ์žˆ๋Š”์ง€ ๊ฑฐ์˜ ์ •ํ™•ํžˆ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค."

"ํ•˜์ง€๋งŒ ์ด๊ฒƒ์„ ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๊นŒ?"

"์—ฌ๊ธฐ์— ๋” ๋‚˜์€ ์˜ˆ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค."

ํŒฌํ…€ ์ฐธ์กฐ ์ƒ์„ฑ ์˜ˆ
// Special queue for phantom objects
ReferenceQueue<Integer> queue = new ReferenceQueue<Integer>();

// List of phantom references
ArrayList<PhantomInteger> list = new ArrayList<PhantomInteger>();

// Create 10 objects and add them to the list using phantom references
for ( int i = 0; i < 10; i++)
{
 Integer x = new Integer(i);
 list.add(new PhantomInteger (x, queue));
}
์ด ์Šค๋ ˆ๋“œ๋Š” ํŒฌํ…€ ํ๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ณ  ์—ฌ๊ธฐ์—์„œ ๊ฐœ์ฒด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
Thread referenceThread = new Thread()
{
 public void run()
 {
  while (true)
  {
   try
   {
    // Get the new object from the queue. If there is no object, then we wait!
    PhantomInteger ref = (PhantomInteger)queue.remove();
    // Call the close method on it
    ref.close();
    ref.clear();
   }
   catch (Exception ex)
   {
    // Write errors to a log
   }
  }
 }
};
// Run the thread as a daemon
referenceThread.setDaemon(true);
referenceThread.start();
PhantomReference๋ฅผ ์ƒ์†ํ•˜๊ณ  close() ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
static class PhantomInteger extends PhantomReference<Integer>
{
 PhantomInteger(Integer referent, ReferenceQueue<? super Integer> queue)
 {
  super(referent, queue);
 }

 private void close()
 {
  System.out.println("Bad Integer totally destroyed!");
 }
}

"์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์„œ ์„ธ ๊ฐ€์ง€ ์ผ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค."

"๋จผ์ € PhantomReference < Integer > ๋ฅผ ์ƒ์†ํ•˜๋Š” PhantomInteger ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค ."

"๋‘˜์งธ, ์ด ํด๋ž˜์Šค์—๋Š” ํŠน์ˆ˜ํ•œ close () ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•  ํ•„์š”์„ฑ์ด ์ด ๋ชจ๋“  ์ž‘์—…์„ ์‹œ์ž‘ํ•˜๊ฒŒ ํ–ˆ์Šต๋‹ˆ๋‹ค.

"์…‹์งธ, ์šฐ๋ฆฌ๋Š” ํŠน์ˆ˜ ์Šค๋ ˆ๋“œ์ธ referenceThread๋ฅผ ์„ ์–ธํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์Šค๋ ˆ๋“œ๋Š” ํŒฌํ…€ ํ์— ๋‹ค๋ฅธ ๊ฐœ์ฒด๊ฐ€ ๋‚˜ํƒ€๋‚  ๋•Œ๊นŒ์ง€ ๋ฃจํ”„์—์„œ ๋Œ€๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ผ์ด ๋ฐœ์ƒํ•˜๋Š” ์ฆ‰์‹œ ์Šค๋ ˆ๋“œ๋Š” ํŒฌํ…€ ํ์—์„œ ๊ฐœ์ฒด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ํ•ด๋‹น close () ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ clear() ๋ฐฉ๋ฒ•. ๊ทธ๊ฒŒ ๋‹ค์•ผ. ํŒฌํ…€์€ ๋” ๋‚˜์€ ์„ธ์ƒ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ์ด์ƒ ์šฐ๋ฆฌ๋ฅผ ๊ดด๋กญํžˆ์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค."

"ํฅ๋ฏธ๋กญ์ง€๋งŒ ๋ชจ๋“  ๊ฒƒ์ด ํ•ด๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."

"์šฐ๋ฆฌ๋Š” ์‹ค์ œ๋กœ ์ฃฝ์–ด๊ฐ€๋Š” ๊ฐœ์ฒด์˜ ๋Œ€๊ธฐ์—ด์„ ์ถ”์ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

"ํ•˜์ง€๋งŒ ๊ธฐ์–ตํ•˜์„ธ์š”. ๊ฐœ์ฒด ์ž์ฒด์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ฐœ์ฒด  ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์–ป์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค! PhantomReference์˜ get() ๋ฉ”์„œ๋“œ๋Š” ํ•ญ์ƒ null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. "

"ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” PhantomReference๋ฅผ ๋ฌผ๋ ค๋ฐ›์•˜์Šต๋‹ˆ๋‹ค!"

"PhantomReference์˜ ํ•˜์œ„ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋„ get() ๋ฉ”์„œ๋“œ๋Š” null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค."

"๊ทธ๋ž˜์„œ ์ƒ์„ฑ์ž์— ๊ฐœ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค."

"์•„. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿฐ ์ฐธ์กฐ๋Š” StrongReference๊ฐ€ ๋  ๊ฒƒ์ด๊ณ  ๊ฐœ์ฒด๋Š” ํŒฌํ…€ ๋Œ€๊ธฐ์—ด์— ์ ˆ๋Œ€ ๋“ค์–ด๊ฐ€์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค!"

"๋Œ•. ์•Œ์•˜์–ด, ํฌ๊ธฐํ•ด. ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด ๋ถˆ๊ฐ€๋Šฅ."

"์ข‹์•„, ์ข‹์•„. ์˜ค๋Š˜ ์ˆ˜์—…์—์„œ ๊ท€์ค‘ํ•œ ๊ฒƒ์„ ๋ฐฐ์› ๊ธฐ๋ฅผ ๋ฐ”๋ž€๋‹ค."

"์˜ˆ, ์ƒˆ๋กœ์šด ์ž๋ฃŒ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ ๋ชจ๋“  ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ•์˜ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค, Rishi."

"์ฒœ๋งŒ์—์š”. ๊ทธ๊ฒŒ ๋‹ค์˜ˆ์š”. ๊ธด์žฅ์„ ํ‘ธ์„ธ์š”. ํ•˜์ง€๋งŒ ์žŠ์ง€ ๋งˆ์„ธ์š”. ์˜ค๋Š˜ ์ €๋…์— ๋˜ ๋‹ค๋ฅธ ์ˆ˜์—…์ด ์žˆ์Šต๋‹ˆ๋‹ค."

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION