synchronized修饰方法 - 正例
- 同一个对象在不同线程中访问同步方法,同步方法对这些线程是能起到同步作用的;
- 如果SynchronizedMethod是个父类,那么子类中的test2()是带不上synchronized的;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class SynchronizedMethod {
public synchronized void test2(int j) {
for (int i = 0; i < 10; i++) {
log.info("synchronized method object {} - {}", j, i);
}
}
public static void main(String[] args) {
testMethodSync();
}
private static void testMethodSync() {
SynchronizedMethod example1 = new SynchronizedMethod();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(() -> {
example1.test2(1);
});
service.execute(() -> {
example1.test2(1);
});
}
}
输出:
- 同一个对象所在的2个线程按顺序先后执行;
17:14:54.487 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 0
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 1
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 2
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 3
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 4
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 5
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 6
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 7
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 8
17:14:54.492 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 9
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 0
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 1
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 2
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 3
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 4
17:14:54.492 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 5
17:14:54.493 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 6
17:14:54.493 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 7
17:14:54.493 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 8
17:14:54.493 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 9
synchronized修饰方法 - 反例
- 不同对象在不同线程中访问同步方法,同步方法对不同调用对象是不能起到同步作用的;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class SynchronizedMethod {
public synchronized void test2(int j) {
for (int i = 0; i < 10; i++) {
log.info("synchronized method object {} - {}", j, i);
}
}
public static void main(String[] args) {
testMethodNotSync();
}
private static void testMethodNotSync() {
SynchronizedMethod example1 = new SynchronizedMethod();
SynchronizedMethod example2 = new SynchronizedMethod();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(() -> {
example1.test2(1);
});
service.execute(() -> {
example2.test2(2);
});
}
}
输出:
- 乱序的,两个对象所在的线程同时执行;
17:19:00.185 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 0
17:19:00.185 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 0
17:19:00.190 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 1
17:19:00.190 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 1
17:19:00.190 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 2
17:19:00.190 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 2
17:19:00.190 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 3
17:19:00.190 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 3
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 4
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 4
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 5
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 5
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 6
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 6
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 7
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 7
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 8
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 8
17:19:00.191 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 1 - 9
17:19:00.191 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedMethod - synchronized method object 2 - 9