synchronized修饰静态方法示例
- synchronized修饰静态方法,其作用对象是这个类的所有对象;
- 2个不同的对象在不同的线程中访问静态方法,按顺序一个一个线程的执行,不会多个线程同时执行;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class SynchronizedStaticMethod {
public synchronized static void test(int j) {
for (int i = 0; i < 10; i++) {
log.info("synchronized static method object {} - {}", j, i);
}
}
public static void main(String[] args) {
SynchronizedStaticMethod example1 = new SynchronizedStaticMethod();
SynchronizedStaticMethod example2 = new SynchronizedStaticMethod();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(() -> {
example1.test(1);
});
service.execute(() -> {
example2.test(2);
});
}
}
输出:
- 两个线程排队执行;
18:05:02.200 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 0
18:05:02.205 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 1
18:05:02.205 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 2
18:05:02.205 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 3
18:05:02.205 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 4
18:05:02.205 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 5
18:05:02.205 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 6
18:05:02.205 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 7
18:05:02.205 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 8
18:05:02.206 [pool-1-thread-1] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 1 - 9
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 0
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 1
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 2
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 3
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 4
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 5
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 6
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 7
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 8
18:05:02.206 [pool-1-thread-2] INFO com.example.concurrency.example.sync.SynchronizedStaticMethod - synchronized static method object 2 - 9