Skip to content

Commit

Permalink
更新SpscLinkableListTest
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Nov 27, 2023
1 parent 8cdbf17 commit 19f2acf
Showing 1 changed file with 9 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
*/
package org.lealone.test.misc;

import java.util.concurrent.CountDownLatch;

public class SpscLinkableListTest {

public static void main(String[] args) throws Exception {
// for (int i = 0; i < 100; i++)
new SpscLinkableListTest().run();
for (int i = 0; i < 10000; i++)
new SpscLinkableListTest().run();
}

// LinkableList是一个无锁且不需要CAS的普通链表,满足单生产者单消费者的应用场景
private final LinkableList<PendingTask> pendingTasks = new LinkableList<>();
private final long pendingTaskCount = 100 * 10000; // 待处理任务总数
private final long pendingTaskCount = 1000 * 10000; // 待处理任务总数
private long completedTaskCount; // 已经完成的任务数

private long result; // 存放计算结果
Expand All @@ -30,11 +28,11 @@ private void run() throws Exception {
submitTask(task);
}
});

// 消费者不断从pendingTasks中取出AsyncTask执行
Thread consumer = new Thread(() -> {
while (completedTaskCount < pendingTaskCount) {
runPendingTasks();
Thread.yield(); // 去掉这一行性能会变慢
}
});
long t = System.currentTimeMillis();
Expand All @@ -54,24 +52,10 @@ private void run() throws Exception {
}

private void submitTask(AsyncTask task) {
if (pendingTasks.size() > 1000) {
CountDownLatch latch = new CountDownLatch(1);
task.latch = latch;
PendingTask pt = new PendingTask(task);
pendingTasks.add(pt);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (pendingTasks.size() > 1)
removeCompletedTasks();
} else {
PendingTask pt = new PendingTask(task);
pendingTasks.add(pt);
if (pendingTasks.size() > 1)
removeCompletedTasks();
}
PendingTask pt = new PendingTask(task);
pendingTasks.add(pt);
if (pendingTasks.size() > 1)
removeCompletedTasks();
}

private void removeCompletedTasks() {
Expand All @@ -98,17 +82,14 @@ private void runPendingTasks() {
}

public class AsyncTask {
int value;
CountDownLatch latch;
final int value;

AsyncTask(int value) {
this.value = value;
}

void compute() {
result += value;
if (latch != null)
latch.countDown();
}
}

Expand Down Expand Up @@ -171,18 +152,10 @@ public void setHead(E head) {
this.head = head;
}

public E getTail() {
return tail;
}

public void setTail(E tail) {
this.tail = tail;
}

public boolean isEmpty() {
return head == null;
}

public int size() {
return size;
}
Expand All @@ -200,27 +173,5 @@ public void add(E e) {
tail = e;
}
}

public void remove(E e) {
size--;
if (head == e) { // 删除头
head = e.getNext();
if (head == null)
tail = null;
} else {
E n = head;
E last = n;
while (n != null) {
if (e == n) {
last.setNext(n.getNext());
break;
}
last = n;
n = n.getNext();
}
if (tail == e) // 删除尾
tail = last;
}
}
}
}

0 comments on commit 19f2acf

Please sign in to comment.