Skip to content

Commit

Permalink
update test demo
Browse files Browse the repository at this point in the history
  • Loading branch information
guanyang committed Apr 1, 2024
1 parent 6464aa6 commit 4685070
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 5 deletions.
208 changes: 208 additions & 0 deletions webflux-sample/src/test/java/org/gy/demo/webflux/PrintTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package org.gy.demo.webflux;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintTest {

public static class PrintABCUsingLockCondition {

private final int times;

private int state;


public PrintABCUsingLockCondition(int times) {
this.times = times;
}

public void print(String name, int targetState, Lock lock, Condition cur, Condition next) {
for (int i = 0; i < times; i++) {
lock.lock();
try {
while (state % 3 != targetState) {
try {
cur.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name);
state++;
next.signal();
} finally {
lock.unlock();
}
}
}

public static void main(String[] args) {
PrintABCUsingLockCondition printABCUsingLockCondition = new PrintABCUsingLockCondition(2);
Lock lock = new ReentrantLock();
Condition conditionA = lock.newCondition();
Condition conditionB = lock.newCondition();
Condition conditionC = lock.newCondition();
new Thread(() -> printABCUsingLockCondition.print("A", 0, lock, conditionA, conditionB)).start();
new Thread(() -> printABCUsingLockCondition.print("B", 1, lock, conditionB, conditionC)).start();
new Thread(() -> printABCUsingLockCondition.print("C", 2, lock, conditionC, conditionA)).start();
}

}


public static class NumAndLetterPrinter {

private final int times;

private int state;

private final Object lock = new Object();

private static final char c = 'A';

private static final String NUM_KEY = "NUM";
private static final String LETTER_KEY = "LETTER";

public NumAndLetterPrinter(int times) {
this.times = times;
}

public void print(String name, int targetState) {
for (int i = 0; i < times; i++) {
synchronized (lock) {
while (state % 2 != targetState) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (NUM_KEY.equals(name)) {
System.out.print(i + 1);
} else {
System.out.print((char) (c + i));
}
state++;
lock.notifyAll();
}
}
}

public static void main(String[] args) {
NumAndLetterPrinter numAndLetterPrinter = new NumAndLetterPrinter(26);
new Thread(() -> numAndLetterPrinter.print(NUM_KEY, 0)).start();
new Thread(() -> numAndLetterPrinter.print(LETTER_KEY, 1)).start();
}
}

public static class OddEvenPrinter {

private final int times;
private volatile int state;
private final Object lock = new Object();

public OddEvenPrinter(int times, int state) {
this.times = times;
this.state = state;
}

public void print(String name, int targetState) {
for (int i = 0; i < times; i++) {
synchronized (lock) {
while (state % 2 != targetState) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(String.format("线程[%s]打印数字:%d", name, state++));
lock.notifyAll();
}
}
}

public static void main(String[] args) {
OddEvenPrinter oddEvenPrinter = new OddEvenPrinter(5, 0);
new Thread(() -> oddEvenPrinter.print("奇数", 1)).start();
new Thread(() -> oddEvenPrinter.print("偶数", 0)).start();
}

}


public static class PrintABCUsingWaitNotify {

private final int times;

private int state;

private final Object lock = new Object();

public PrintABCUsingWaitNotify(int times) {
this.times = times;
}

public void print(String name, int targetState) {
for (int i = 0; i < times; i++) {
synchronized (lock) {
while (state % 3 != targetState) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name);
state++;
lock.notifyAll();
}
}
}

public static void main(String[] args) {
PrintABCUsingWaitNotify printABCUsingWaitNotify = new PrintABCUsingWaitNotify(5);
new Thread(() -> printABCUsingWaitNotify.print("A", 0)).start();
new Thread(() -> printABCUsingWaitNotify.print("B", 1)).start();
new Thread(() -> printABCUsingWaitNotify.print("C", 2)).start();
}
}

public static class PrintABCUsingLock {

private final int times;

private int state;

private final Lock lock = new ReentrantLock();

public PrintABCUsingLock(int times) {
this.times = times;
}

public void print(String name, int targetState) {
int loop = 0;
while (loop < times) {
lock.lock();
try {
if (state % 3 == targetState) {
System.out.println(name);
state++;
loop++;
}
} finally {
lock.unlock();
}
}
}

public static void main(String[] args) {
PrintABCUsingLock printABCUsingLock = new PrintABCUsingLock(5);
new Thread(() -> printABCUsingLock.print("A", 0)).start();
new Thread(() -> printABCUsingLock.print("B", 1)).start();
new Thread(() -> printABCUsingLock.print("C", 2)).start();
}
}

}
63 changes: 58 additions & 5 deletions webflux-sample/src/test/java/org/gy/demo/webflux/Test002.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
public class Test002 {

public static void main(String[] args) {
// int[] nums = randomIntArray(10, 200);
// printArray(nums);
//// bubbleSort(nums);
//// quickSort(nums, 0, nums.length - 1);
int[] nums = randomIntArray(10, 200);
printArray(nums);
// bubbleSort(nums);
// quickSort(nums, 0, nums.length - 1);
// mergeSort(nums, 0, nums.length - 1);
// printArray(nums);
heapSort(nums);
printArray(nums);

// String version1 = "0.1";
// String version2 = "1.1";
Expand All @@ -19,6 +20,58 @@ public static void main(String[] args) {

}

/**
* 使用堆排序算法对数组进行排序。
*
* @param nums 待排序的整型数组。
*/
public static void heapSort(int[] nums) {
// 建立最大堆,从最后一个非叶子节点开始向上调整
for (int i = nums.length / 2 - 1; i >= 0; i--) {
siftDown(nums, nums.length, i);
}
// 依次从堆中取出最大元素,放至数组末尾,然后重新调整堆
for (int i = nums.length - 1; i > 0; i--) {
swap(nums, 0, i);
siftDown(nums, i, 0);
}
}


/**
* 自顶向下堆化处理 该方法通过比较父节点和其子节点的值,将较大的元素下沉,以维持堆的性质。
*
* @param nums 堆数组
* @param len 数组的有效长度
* @param i 当前需要堆化的节点索引
*/
public static void siftDown(int[] nums, int len, int i) {
while (true) {
int left = 2 * i + 1; // 左子节点索引
int right = 2 * i + 2; // 右子节点索引
int max = i; // 当前最大值节点索引,默认为父节点

// 检查左子节点,如果存在且大于父节点,则更新最大值节点索引
if (left < len && nums[left] > nums[max]) {
max = left;
}

// 检查右子节点,如果存在且大于当前最大值节点,则更新最大值节点索引
if (right < len && nums[right] > nums[max]) {
max = right;
}

// 如果当前节点已经是最大值,或者左右子节点均越界,则结束循环
if (max == i) {
break;
}

// 交换当前节点与其较大的子节点,继续下沉调整
swap(nums, i, max);
i = max;
}
}


public static int compareVersion(String version1, String version2) {
if (version1 == null && version2 == null) {
Expand Down

0 comments on commit 4685070

Please sign in to comment.