¼Ò¼³ java.lang ¼Ò½ºÄڵ堠Thread  synchronized  

synchronized ÀÇ »ç¿ë¹ý

public class Sync implements Runnable {
    public void a(String who) {
       System.out.println(who+" will sleep 5 sec");
       try
       {
       	  Thread.sleep(5000);
       }
       catch (InterruptedException e)
       {
          System.out.println(e);
       }
       System.out.println(who+" am happy");
    }
    public void run()
    {
       a("thread");
    }
    public static void main(String[] arg)
    {
       Sync sync = new Sync();
       Thread t = new Thread(sync);
       sync.a("main");
       t.start();
       System.out.println("Á¾·á");
    }
}
C:\blackbox\java.lang\sync>java Sync
main will sleep 5 sec
main am happy
Á¾·á
thread will sleep 5 sec
thread am happy

runnable thread¸¦ startÇÏ´Â °ÍÀº main threadÀÌ´Ù. 
main thread°¡ a()¿¡¼­ returnµÇÁö ¾Ê´Â´Ù¸é runnable thread´Â startµÇÁö ¸øÇÑ´Ù. 


public class Sync implements Runnable { public void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public void run() { a("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec thread will sleep 5 sec main am happy Á¾·á thread am happy
public class Sync implements Runnable { public synchronized void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public void run() { a("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); sync.a("main"); t.start(); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec main am happy Á¾·á thread will sleep 5 sec thread am happy synchronized methodÀÇ È¿°ú°¡ ¾Æ´Ï´Ù. ´ÜÁö start¸¦ ÇØÁÖ±â À§ÇÑ main thread°¡ ÀâÇôÀֱ⠶§¹®ÀÌ´Ù.
public class Sync implements Runnable { public synchronized void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public void run() { a("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec main am happy Á¾·á thread will sleep 5 sec thread am happy ÇϳªÀÇ thread°¡ synchronizer method¿¡ µé¾î°¡ ÀÖÀ¸¸é ´Ù¸¥ thread´Â °°Àº synchronized method¸¦ ½ÇÇàÇÏÁö ¸øÇÏ°í ±â´Ù¸°´Ù. main thread°¡ synchronized method¿¡¼­ returnµÇ¸é main thread´Â "Á¾·á"¸¦ È­¸é¿¡ Ãâ·ÂÇÏ°í »ý¸íÀ» ³¡¸¶Ä£´Ù. ±×·¯³ª JVMÀº Á¾·áµÇÁö ¾Ê´Â´Ù. runnable thread°¡ ¾ÆÁ÷ »ì¾ÆÀֱ⶧¹®ÀÌ´Ù.
public class Sync implements Runnable { public synchronized void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public void b(String who) { System.out.println("I am b(String) : "+who+" ´Â ÀÚÀ¯·Ó´Ù"); } public void run() { b("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec I am b(String) : thread ´Â ÀÚÀ¯·Ó´Ù main am happy Á¾·á synchronized °¡ µÇÁö ¾ÊÀº public void b(String)Àº ÀÚÀ¯·Ó°Ô multithread°¡ ½ÇÇàÇÒ ¼ö ÀÖ´Ù.
public class Sync implements Runnable { public synchronized void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public synchronized void b(String who) { System.out.println("I am sync b(String) : "+who+" a()°¡ ³¡³ª¾ß ÇÒÅÙµ¥..."); } public void run() { b("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec main am happy Á¾·á I am sync b(String) : thread a()°¡ ³¡³ª¾ß ÇÒÅÙµ¥... synchronized methodÀÎ a(String)°¡ returnµÇÁö ¾Ê´Â´Ù¸é ´Ù¸¥ multithread´Â °°Àº method°¡ ¾Æ´Ò Áö¶óµµ synchronized methodÀÎ b(String)¸¦ ½ÇÇàÇÒ ¼ö ¾ø´Ù.
public class Sync implements Runnable { public synchronized void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public static void b(String who) { System.out.println("I am static b(String) : "+who+" ´Â ÀÚÀ¯·Ó´Ù"); } public void run() { b("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec I am static b(String) : thread ´Â ÀÚÀ¯·Ó´Ù main am happy Á¾·á
public class Sync implements Runnable { public synchronized void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public static synchronized void b(String who) { System.out.println("I am static synchronized b(String) : "+who+" static À̱⿡ ÀÚÀ¯·Ó´Ù"); } public void run() { b("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec I am static synchronized b(String) : thread static À̱⿡ ÀÚÀ¯·Ó´Ù main am happy Á¾·á synchronized method°¡ ½ÇÇàµÇ°í ÀÖ´õ¶óµµ static synchronized´Â ÀÚÀ¯·Ó´Ù.
public class Sync implements Runnable { public static synchronized void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public static synchronized void b(String who) { System.out.println("I am static synchronized b(String) : "+who+" sync static a()°¡ ³¡³ª¾ß ÇÒÅÙµ¥.."); } public void run() { b("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec main am happy Á¾·á I am static synchronized b(String) : thread sync static a()°¡ ³¡³ª¾ß ÇÒÅÙµ¥.. ´Ù¸¥ ÇÔ¼öÀÎ static synchronized method°¡ 2°³ ÀÖÀ» ¶§ µ¿½Ã¿¡ °¢°¢ÀÇ thread°¡ method¸¦ ½ÇÇàÇÒ ¼ö ¾ø´Ù.

ÇϳªÀÇ thread¿¡¼­ synchronized method´Â ¹«ÀÇ¹Ì ÇÏ´Ù.

public class Sync { public static synchronized void a(String who) { System.out.println(who+" try call sync b()"); b(who); } public static synchronized void b(String who) { System.out.println("I am b() dead lock"); } public static void main(String[] arg) { Sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main try call sync b() I am b() dead lock Á¾·á public class Sync { public synchronized void a(String who) { System.out.println(who+" try call sync b()"); b(who); } public synchronized void b(String who) { System.out.println("I am b() dead lock"); } public static void main(String[] arg) { Sync sync = new Sync(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main try call sync b() I am b() dead lock Á¾·á

synchronized method´Â ÇϳªÀÇ class¿¡¼­¸¸ È¿°ú¸¦ ¹ßÈÖÇÑ´Ù.

public class Call { public static synchronized void say() { System.out.println("Call:: say : ³»°¡ ³½µ¥ ¶³ºä ? "); } } public class Sync implements Runnable { public static synchronized void a(String who) { System.out.println(who+" will sleep 5 sec"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } System.out.println(who+" am happy"); } public void run() { Call.say(); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } C:\blackbox\java.lang\sync>java Sync main will sleep 5 sec Call:: say : ³»°¡ ³½µ¥ ¶³ºä ? main am happy Á¾·á ´Ù¸¥ classÀÇ synchronized method´Â »ó°üÇÏÁö ¾Ê´Â´Ù. synchronized method´Â ´ÜÀÏ class¿¡¼­ ÇϳªÀÇ thread°¡ ¾Æ´Ñ 2°³ ÀÌ»óÀÇ multithread°¡ synchronized method¸¦ Á¢±ÙÇϰíÀÚ ÇÒ ¶§ Á¢±ÙÀÌ ¹Ì·ïÁø´Ù.
public class Sync implements Runnable { public static synchronized void a(String who) { System.out.println(who+" try call sync b()"); try { Thread.sleep(200); } catch (InterruptedException e) { System.out.println(e); } b(who); } public static synchronized void b(String who) { System.out.println(who+" try call sync a()"); try { Thread.sleep(200); } catch (InterruptedException e) { System.out.println(e); } a(who); } public void run() { b("thread"); } public static void main(String[] arg) { Sync sync = new Sync(); Thread t = new Thread(sync); t.start(); sync.a("main"); System.out.println("Á¾·á"); } } c:\>java Sync main try call sync b() main try call sync a() main try call sync b() main try call sync a() main try call sync b() main try call sync a() main try call sync b() main try call sync a() main try call sync b() main try call sync a() main try call sync b() main try call sync a() main thread°¡ ¸ÕÀú ¸ð´ÏÅÍ·Î µé¾î°¡¼­ ¼±Á¡ÇØ ¹ö¸°´Ù. sync a()°¡ returnµÇ±â Àü¿¡ main thread°¡ b()·Î µé¾î°¥ ¼ö ÀÖ´Â °ÍÀº µ¿ÀÏÇÑ threadÀ̱⠶§¹®ÀÌ´Ù.


jabookÀúÀÚ¸íÇÔ
Á¦¸ñ:¼Ò¼³°°Àº¹Ì´ÏÄÚµå
ÀÛ¼ºÀÚ:Àںϸâ¹ö ÇÑâÇå