This commit is contained in:
hz21056617
2022-01-29 18:07:54 +08:00
parent 8f0cb0b321
commit 0255d7420d
8 changed files with 376 additions and 1 deletions
+28
View File
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>JavaBasiceDemo</artifactId>
<groupId>com.wyl.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>concurrent</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,52 @@
package concurrent.queue;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import java.util.concurrent.ArrayBlockingQueue;
@AllArgsConstructor
public class BlockingQueueTestWork extends Thread {
ArrayBlockingQueue<Integer> arrayBlockingQueue;
Integer methodNumber;
@SneakyThrows
@Override
public void run() {
switch (methodNumber) {
case 1:
offerTest();
break;
case 2:
addTest();
break;
case 3:
putTest();
break;
}
}
public void offerTest() {
boolean offer = arrayBlockingQueue.offer(1);
if (!offer) {
System.out.println("队列满了");
return;
}
System.out.println("添加成功");
}
public void addTest() {
boolean offer = arrayBlockingQueue.add(1);
if (!offer) {
System.out.println("队列满了");
return;
}
System.out.println("添加成功");
}
public void putTest() throws InterruptedException {
arrayBlockingQueue.put(1);
System.out.println("添加成功");
}
}
@@ -0,0 +1,92 @@
package concurrent.reentrantlock;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.SneakyThrows;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* ReentrantLock 公平锁运行
* @ClassName: ReentrantLockNonFairTest
* @Date: 2022/1/28 13:48
* @author hz21056617
* @version V1.0
*/
@Data
@AllArgsConstructor
public class ReentrantLockFairTest extends Thread {
static final Lock lock = new ReentrantLock(true);
private Integer number;
private CountDownLatch countDownLatch;
private Integer methodNumber;
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@SneakyThrows
@Override
public void run() {
if (0 == methodNumber) {
lockTest();
} else if (1 == methodNumber) {
tryLockTest();
}
}
/**
* lock方法测试
* @param
* @return void
* @Date 2022/1/28 15:14
* @Author hz21056617
* @Version V1.0
*/
@SneakyThrows
public void lockTest() {
try {
lock.lock();
System.out.println(number);
Thread.sleep(500);
}
finally {
countDownLatch.countDown();
lock.unlock();
}
}
/**
* tryLock方法测试
* @param
* @return void
* @Date 2022/1/28 15:13
* @Author hz21056617
* @Version V1.0
*/
@SneakyThrows
public void tryLockTest() {
if (lock.tryLock()) {
try {
System.out.println(number);
Thread.sleep(500);
}
finally {
countDownLatch.countDown();
lock.unlock();
}
} else {
countDownLatch.countDown();
System.out.println("上个线程还没有执行完呢");
}
}
}
@@ -0,0 +1,91 @@
package concurrent.reentrantlock;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.SneakyThrows;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* ReentrantLock 非公平锁依次运行
* @ClassName: ReentrantLockNonFairTest
* @Date: 2022/1/28 13:48
* @author hz21056617
* @version V1.0
*/
@Data
@AllArgsConstructor
public class ReentrantLockNonFairTest extends Thread {
static final Lock lock = new ReentrantLock();
private Integer number;
private CountDownLatch countDownLatch;
private Integer methodNumber;
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
if (0 == methodNumber) {
lockTest();
} else if (1 == methodNumber) {
tryLockTest();
}
}
/**
* lock方法测试
* @param
* @return void
* @Date 2022/1/28 15:14
* @Author hz21056617
* @Version V1.0
*/
@SneakyThrows
public void lockTest() {
try {
lock.lock();
System.out.println(number);
Thread.sleep(500);
}
finally {
countDownLatch.countDown();
lock.unlock();
}
}
/**
* tryLock方法测试
* @param
* @return void
* @Date 2022/1/28 15:13
* @Author hz21056617
* @Version V1.0
*/
@SneakyThrows
public void tryLockTest() {
if (lock.tryLock()) {
try {
System.out.println(number);
Thread.sleep(500);
}
finally {
countDownLatch.countDown();
lock.unlock();
}
} else {
countDownLatch.countDown();
System.out.println("上个线程还没有执行完呢");
}
}
}
@@ -0,0 +1,40 @@
package concurrent.queue;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ArrayBlockingQueue;
public class QueueTest {
@Test
public void ArrayBlockingQueueOffer() throws InterruptedException {
ArrayBlockingQueue<Integer> arrayBlockingQueue = new ArrayBlockingQueue<>(10);
for (Integer i = 0; i < 100; i++){
new BlockingQueueTestWork(arrayBlockingQueue, 1).start();
}
Thread.sleep(10000000);
}
@Test
public void ArrayBlockingQueueAdd() throws InterruptedException {
ArrayBlockingQueue<Integer> arrayBlockingQueue = new ArrayBlockingQueue<>(10);
for (Integer i = 0; i < 100; i++){
new BlockingQueueTestWork(arrayBlockingQueue, 2).start();
}
Thread.sleep(10000000);
}
@Test
public void ArrayBlockingQueuePut() throws InterruptedException {
ArrayBlockingQueue<Integer> arrayBlockingQueue = new ArrayBlockingQueue<>(10);
for (Integer i = 0; i < 100; i++){
new BlockingQueueTestWork(arrayBlockingQueue, 3).start();
}
Thread.sleep(10000000);
}
@Test
public void reentrantLockNonFairTestTryLock() throws InterruptedException {
}
}
@@ -0,0 +1,45 @@
package concurrent.reentrantlock;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
public class ReentrantLockTest {
@Test
public void reentrantLockFairTestLock() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(100);
for (Integer i = 0; i < 100; i++){
new ReentrantLockFairTest(i, countDownLatch, 0).start();
}
countDownLatch.await();
}
@Test
public void reentrantLockFairTestTryLock() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(100);
for (Integer i = 0; i < 100; i++){
new ReentrantLockFairTest(i, countDownLatch, 1).start();
}
countDownLatch.await();
}
@Test
public void reentrantLockNonFairTestLock() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(100);
for (Integer i = 0; i < 100; i++){
new ReentrantLockNonFairTest(i, countDownLatch, 0).start();
}
countDownLatch.await();
}
@Test
public void reentrantLockNonFairTestTryLock() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(100);
for (Integer i = 0; i < 100; i++){
new ReentrantLockNonFairTest(i, countDownLatch, 1).start();
}
countDownLatch.await();
}
}
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>JavaBasiceDemo</artifactId>
<groupId>com.wyl.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>elasticsearch</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
+9 -1
View File
@@ -9,6 +9,8 @@
<version>1.0-SNAPSHOT</version>
<modules>
<module>dynamic-proxy</module>
<module>elasticsearch</module>
<module>concurrent</module>
</modules>
<packaging>pom</packaging>
<properties>
@@ -24,5 +26,11 @@
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
</project>