gpt4 book ai didi

java - "Single LIFO Executor"/Swing worker

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:18:08 25 4
gpt4 key购买 nike

Consider a Swing application with a JList or JTable, when the selection changes a SwingWorker is started and loads related data from database and updates UI.这工作正常并且 UI 响应迅速。

但是如果用户正在快速更改所选行(按住向上/向下键),我想确保最后选择的行是最后加载的行,而且我也不想查询数据库徒劳的。所以我想要的是一个后进先出队列大小为 1 的单线程执行器。因此,向它提交任务会删除之前提交的所有任务,并使其一次最多执行 1 个任务,并且最多有 1 个任务等待执行。

我在 java.util.concurrent 中找不到类似的东西,所以我编写了自己的执行器。我这样做是对的还是我从并发包中遗漏了什么?解决方案是否可以接受,或者是否有更好的方法来实现我想要的?

public class SingleLIFOExecutor implements Executor
{
private final ThreadPoolExecutor executor;
private Runnable lastCommand;

public SingleLIFOExecutor()
{
executor = new ThreadPoolExecutor(0, 1, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
}

@Override
public void execute(Runnable command)
{
executor.remove(lastCommand);
lastCommand = command;
executor.execute(command);
}
}

这是一个展示如何使用它的例子:

final Executor executor = new SingleLIFOExecutor();
JList jList = createMyList();
jList.addListSelectionListener(new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting())
{
executor.execute(new MyWorker());
}
}
});

最佳答案

LinkedBlockingDeque 似乎仍然使用带有 ThreadPoolExecutor 的队列。

所以我使用了一个包装器并将其与 ThreadPoolExecutor 一起使用:

package util;

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
* LIFO BlockingQueue to be used with the ExecutorService.
* @author Daniel
* @param <T>
*/
public class LinkedBlockingStack<T> implements BlockingQueue<T>{
private final LinkedBlockingDeque<T> stack = new LinkedBlockingDeque<T>();

@Override
public T remove() {
return stack.remove();
}

@Override
public T poll() {
return stack.poll();
}

@Override
public T element() {
return stack.element();
}

@Override
public T peek() {
return stack.peek();
}

@Override
public int size() {
return stack.size();
}

@Override
public boolean isEmpty() {
return stack.isEmpty();
}

@Override
public Iterator<T> iterator() {
return stack.iterator();
}

@Override
public Object[] toArray() {
return stack.toArray();
}

@Override
public <S> S[] toArray(final S[] a) {
return stack.toArray(a);
}

@Override
public boolean containsAll(final Collection<?> c) {
return stack.containsAll(c);
}

@Override
public boolean addAll(final Collection<? extends T> c) {
return stack.addAll(c);
}

@Override
public boolean removeAll(final Collection<?> c) {
return stack.removeAll(c);
}

@Override
public boolean retainAll(final Collection<?> c) {
return stack.removeAll(c);
}

@Override
public void clear() {
stack.clear();
}

@Override
public boolean add(final T e) {
return stack.offerFirst(e); //Used offerFirst instead of add.
}

@Override
public boolean offer(final T e) {
return stack.offerFirst(e); //Used offerFirst instead of offer.
}

@Override
public void put(final T e) throws InterruptedException {
stack.put(e);
}

@Override
public boolean offer(final T e, final long timeout, final TimeUnit unit)
throws InterruptedException {
return stack.offerLast(e, timeout, unit);
}

@Override
public T take() throws InterruptedException {
return stack.take();
}

@Override
public T poll(final long timeout, final TimeUnit unit)
throws InterruptedException {
return stack.poll();
}

@Override
public int remainingCapacity() {
return stack.remainingCapacity();
}

@Override
public boolean remove(final Object o) {
return stack.remove(o);
}

@Override
public boolean contains(final Object o) {
return stack.contains(o);
}

@Override
public int drainTo(final Collection<? super T> c) {
return stack.drainTo(c);
}

@Override
public int drainTo(final Collection<? super T> c, final int maxElements) {
return stack.drainTo(c, maxElements);
}
}

关于java - "Single LIFO Executor"/Swing worker ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4173873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com