Skip to content

Instantly share code, notes, and snippets.

@ch7895
Last active March 23, 2016 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ch7895/ee5fe84b44e950c96893 to your computer and use it in GitHub Desktop.
Save ch7895/ee5fe84b44e950c96893 to your computer and use it in GitHub Desktop.
스레드 동기화 예제1 - syncronized
public class A {
private static int a ;
A(){
a=15;
}
public synchronized void work(){
a--;
}
public int getA(){
return a;
}
}
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());
}
}
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();
}
}
}
@ch7895
Copy link
Author

ch7895 commented Mar 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment