先说简单的,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();
}
}
方式一:synchronized进行线程同步
/**
* @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();
}
}
方式二:ReentrantLock进行线程同步
/**
* @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();
}
}
方式三:synchronized代码块
/**
* @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();
}
}
标签:poolExecutor,Java,浅谈,void,private,互斥,static,value,new
From: https://blog.51cto.com/u_15654567/5786101