public class Synchronized implements Runnable{
public void run() {
synchronized (Synchronized.class) {
try {
System.out.println("start");
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end");
}
}
public void test(){
synchronized (Synchronized.class) {
System.out.println("again start!");
}
}
public static void main(String[] args) {
Synchronized s = new Synchronized();
Thread t1 = new Thread(s);
Thread t2 = new Thread(s);
t1.start();
t2.start();
int count = 100;
while(count > 0) {
s.test();
count--;
}
}
}