先說簡單的,Java實作線程互斥:
無線程互斥的情況:
/**
* @desc: 沒有進行互斥的情況
* @author: YanMingXin
* @create: 2021/12/19-18:02
**/
public class Method0 {
private int value = 10;
private void reduce() {
try {
while (value == 0) {
System.out.println("stop...");
return;
}
System.out.println(Thread.currentThread().getName() + ":" + value);
value--;
} catch (Exception e) {
e.printStackTrace();
}
}
private void make() {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
for (int i = 0; i < 20; i++) {
poolExecutor.execute(() -> {
reduce();
});
}
}
public static void main(String[] args) {
Method0 methodA = new Method0();
methodA.make();
}
}
方式一:互斥鎖synchronized
/**
* @desc: 互斥方式一
* @author: YanMingXin
* @create: 2021/12/19-17:48
**/
public class MethodA {
private int value = 10;
private synchronized void reduce() {
try {
while (value == 0) {
System.out.println("stop...");
return;
}
System.out.println(Thread.currentThread().getName() + ":" + value);
value--;
} catch (Exception e) {
e.printStackTrace();
}
}
private void make() {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
for (int i = 0; i < 20; i++) {
poolExecutor.execute(() -> {
reduce();
});
}
}
public static void main(String[] args) {
MethodA methodA = new MethodA();
methodA.make();
}
}
方式二:可重入互斥鎖ReentrantLock
/**
* @desc: 互斥方式二
* @author: YanMingXin
* @create: 2021/12/19-17:50
**/
public class MethodB {
private int value = 10;
private ReentrantLock lock=new ReentrantLock();
private void reduce(){
lock.lock();
try {
while (value == 0) {
System.out.println("stop...");
return;
}
System.out.println(Thread.currentThread().getName() + ":" + value);
value--;
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
private void make() {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
for (int i = 0; i < 20; i++) {
poolExecutor.execute(() -> {
reduce();
});
}
}
public static void main(String[] args) {
MethodB methodA = new MethodB();
methodA.make();
}
}
然後,Java實作線程同步:
無線程同步的情況:
/**
* @desc: 無線程同步狀态
* @author: YanMingXin
* @create: 2021/12/19-17:47
**/
public class Method0 {
private int value = 0;
private static List list = new ArrayList<String>();
static {
list = Arrays.asList("1-Insert", "2-Delete", "3-Update", "4-Select");
}
private void reduce() {
try {
System.out.println(list.get(value));
value++;
while (value > 3) {
System.out.println("stop...");
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void make() {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
4,
4,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>());
for (int i = 0; i < 4; i++) {
poolExecutor.execute(() -> {
reduce();
});
}
poolExecutor.shutdown();
}
public static void main(String[] args) {
Method0 methodA = new Method0();
methodA.make();
}
}
/**
* @desc: 線程同步方式一
* @author: YanMingXin
* @create: 2021/12/19-17:47
**/
public class MethodA {
private int value = 0;
private static List list = new ArrayList<String>();
static {
list = Arrays.asList("1-Insert", "2-Delete", "3-Update", "4-Select");
}
private synchronized void reduce() {
try {
System.out.println(list.get(value));
value++;
while (value > 3) {
System.out.println("stop...");
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void make() {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
4,
4,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>());
for (int i = 0; i < 4; i++) {
poolExecutor.execute(() -> {
reduce();
});
}
poolExecutor.shutdown();
}
public static void main(String[] args) {
MethodA methodA = new MethodA();
methodA.make();
}
}
/**
* @desc: 線程同步方式二
* @author: YanMingXin
* @create: 2021/12/19-17:47
**/
public class MethodA {
private int value = 0;
private ReentrantLock lock = new ReentrantLock();
private static List list = new ArrayList<String>();
static {
list = Arrays.asList("1-Insert", "2-Delete", "3-Update", "4-Select");
}
private void reduce() {
lock.lock();
try {
System.out.println(list.get(value));
value++;
while (value > 3) {
System.out.println("stop...");
return;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
private void make() {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
4,
4,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>());
for (int i = 0; i < 4; i++) {
poolExecutor.execute(() -> {
reduce();
});
}
poolExecutor.shutdown();
}
public static void main(String[] args) {
MethodA methodA = new MethodA();
methodA.make();
}
}
/**
* @desc: 線程同步方式三
* @author: YanMingXin
* @create: 2021/12/19-17:47
**/
public class MethodA {
private int value = 0;
private static List list = new ArrayList<String>();
static {
list = Arrays.asList("1-Insert", "2-Delete", "3-Update", "4-Select");
}
private void reduce() {
synchronized (this) {
try {
System.out.println(list.get(value));
value++;
while (value > 3) {
System.out.println("stop...");
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void make() {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
4,
4,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>());
for (int i = 0; i < 4; i++) {
poolExecutor.execute(() -> {
reduce();
});
}
poolExecutor.shutdown();
}
public static void main(String[] args) {
MethodA methodA = new MethodA();
methodA.make();
}
}