test1 j:1 — i:0
test1 j:1 — i:1
test1 j:1 — i:2
test1 j:1 — i:3
test1 j:1 — i:4
test1 j:1 — i:5
test1 j:1 — i:6
test1 j:1 — i:7
test1 j:1 — i:8
test1 j:1 — i:9
test1 j:2 — i:0
test1 j:2 — i:1
test1 j:2 — i:2
test1 j:2 — i:3
test1 j:2 — i:4
test1 j:2 — i:5
test1 j:2 — i:6
test1 j:2 — i:7
test1 j:2 — i:8
test1 j:2 — i:9
线程1和线程2按照各自顺序执行,线程一和线程二都能够按照自己的同步代码走下去,但是不一定能保证线程一执行完之后才到线程二执行,这就要看哪一个线程能够率先抢到资源。
修饰方法
package com.lyy.concurrency.sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SynchronizedExample1 {
//修饰一个方法
public synchronized void test2(int j){
for (int i = 0; i < 10; i++) {
System.out.println(“test2 j:”+j+" — i:"+i);
}
}
public static void main(String[] args) {
SynchronizedExample1 example1 = new SynchronizedExample1();
SynchronizedExample1 example2 = new SynchronizedExample1();
ExecutorService executorService = Executors.newCachedThreadPool();//声明一个线程池
//加上线程池相当于我们调用了两个线程
//两个线程调用了同一个对象
executorService.execute(() ->{
example1.test2(1);
});
executorService.execute(() ->{
example2.test2(2);
});
}
}
返回结果:
test2 j:1 — i:0
test2 j:1 — i:1
test2 j:1 — i:2
test2 j:1 — i:3
test2 j:1 — i:4
test2 j:1 — i:5
test2 j:1 — i:6
test2 j:1 — i:7
test2 j:2 — i:0
test2 j:2 — i:1
test2 j:2 — i:2
test2 j:2 — i:3
test2 j:2 — i:4
test2 j:2 — i:5
test2 j:2 — i:6
test2 j:2 — i:7
test2 j:2 — i:8
test2 j:1 — i:8
test2 j:2 — i:9
test2 j:1 — i:9
我们可以看到1 和2 是交替运行的,但是各自都是按照顺序在执行,这里是因为修饰代码块只能作用于当前调用的对象,我们这里是调用了两个方法所以,两个线程则互不干扰,都是各自执行各自的代码,同步是整个方法
注意:如果SynchronizedExample1 是个子类 那么实现test2的时候是不会携带synchronized关键字 ,因为synchronized是不属于方法声明的一部分,因此,synchronized关键字不能被继承,如果想要去实现这个子类继承synchronized,需要我们手动是实现这个功能
修饰类
package com.lyy.concurrency.sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SynchronizedExample2 {
// 修饰一个类
public static void test1(int j){
synchronized (SynchronizedExample2.class){
for (int i = 0; i < 10; i++) {
System.out.println(“test1 j:”+j+" — i:"+i);
}
}
}
public static void main(String[] args) {
SynchronizedExample2 example1 = new SynchronizedExample2();
SynchronizedExample2 example2 = new SynchronizedExample2();
ExecutorService executorService = Executors.newCachedThreadPool();//声明一个线程池
//加上线程池相当于我们调用了两个线程 l
//两个线程调用了同一个对象
executorService.execute(() ->{
example1.test1(1);
});
executorService.execute(() ->{
example2.test1(2);
});
}
}
返回结果:
test1 j:1 — i:0
test1 j:1 — i:1
test1 j:1 — i:2
test1 j:1 — i:3
test1 j:1 — i:4
test1 j:1 — i:5
test1 j:1 — i:6
test1 j:1 — i:7
test1 j:1 — i:8
test1 j:1 — i:9
test1 j:2 — i:0
test1 j:2 — i:1
test1 j:2 — i:2
test1 j:2 — i:3
test1 j:2 — i:4
test1 j:2 — i:5
test1 j:2 — i:6
test1 j:2 — i:7
test1 j:2 — i:8
test1 j:2 — i:9
修饰静态方法
package com.lyy.concurrency.sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SynchronizedExample2 {
//修饰一个静态方法
public static synchronized void test2(int j){
for (int i = 0; i < 10; i++) {
System.out.println(“test2 j:”+j+" — i:"+i);
}
}
public static void main(String[] args) {
SynchronizedExample2 example1 = new SynchronizedExample2();
SynchronizedExample2 example2 = new SynchronizedExample2();
ExecutorService executorService = Executors.newCachedThreadPool();//声明一个线程池
//加上线程池相当于我们调用了两个线程 l
//两个线程调用了同一个对象
executorService.execute(() ->{
example1.test2(1);
});
executorService.execute(() ->{
example2.test2(2);
});
}
}
返回结果:
test2 j:1 — i:0
test2 j:1 — i:1
test2 j:1 — i:2
test2 j:1 — i:3
test2 j:1 — i:4
test2 j:1 — i:5
test2 j:1 — i:6
test2 j:1 — i:7
test2 j:1 — i:8
test2 j:1 — i:9
test2 j:2 — i:0
test2 j:2 — i:1
test2 j:2 — i:2
test2 j:2 — i:3
test2 j:2 — i:4
test2 j:2 — i:5
test2 j:2 — i:6
test2 j:2 — i:7
test2 j:2 — i:8
test2 j:2 — i:9
案例:
线程不安全案例:
package com.lyy.concurrency.example.count;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class CountExample1 {
//请求总数
public static int clientTotal = 5000;
//同时并发执行的线程数
public static int threadTotal = 200;
//
public static int count = 0;
public static void main(String[] args) throws Exception{
ExecutorService executorService = Executors.newCachedThreadPool();//线程池
final Semaphore semaphore = new Semaphore(threadTotal);//允许并发的数量
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
最后
针对最近很多人都在面试,我这边也整理了相当多的面试专题资料,也有其他大厂的面经。希望可以帮助到大家。
下面的面试题答案都整理成文档笔记。也还整理了一些面试资料&最新2021收集的一些大厂的面试真题(都整理成文档,小部分截图)
最新整理电子书
static void main(String[] args) throws Exception{
ExecutorService executorService = Executors.newCachedThreadPool();//线程池
final Semaphore semaphore = new Semaphore(threadTotal);//允许并发的数量
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
最后
针对最近很多人都在面试,我这边也整理了相当多的面试专题资料,也有其他大厂的面经。希望可以帮助到大家。
下面的面试题答案都整理成文档笔记。也还整理了一些面试资料&最新2021收集的一些大厂的面试真题(都整理成文档,小部分截图)
[外链图片转存中…(img-jYlDpb6R-1720084559439)]
最新整理电子书
[外链图片转存中…(img-teJ29sq2-1720084559439)]