-
-
Save ch7895/ee5fe84b44e950c96893 to your computer and use it in GitHub Desktop.
스레드 동기화 예제1 - syncronized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class A { | |
private static int a ; | |
A(){ | |
a=15; | |
} | |
public synchronized void work(){ | |
a--; | |
} | |
public int getA(){ | |
return a; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class B extends Thread{ | |
private A aa ; | |
private String name; | |
public B(String Name, A Aa){ | |
name = Name; | |
aa = Aa; | |
} | |
public void run(){ | |
for(int i=0; i<3; i++){ | |
aa.work(); | |
System.out.println(name + "/" + aa.getA()); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
//System.out.println(name + "/" + aa.getA()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class C { | |
public static void main(String[] args){ | |
B[] brr = new B[5]; | |
for(int i=0; i<5; i++){ | |
A a = new A(); | |
brr[i] = new B("Thread"+i, a); | |
} | |
for(int i=0; i<5; i++){ | |
brr[i].start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://changpd.blogspot.kr/2016/03/syncronized.html