我的无锁链表实现始终在toString()上返回空字符串

韩国工业联合会

我正在尝试无锁链表。这是我的第一枪,但我不知道为什么即使在调试器中可以看到值,toString方法也总是返回空字符串。

package com.linkedq;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;

public class LinkedQueue <E> {
    private static class Node <E> {
        final E item;
        final AtomicReference<Node<E>> next;

        Node(E item, Node<E> next) {
            this.item = item;
            this.next = new AtomicReference<>(next);
        }

        @Override
        public String toString() {
            return item.toString();
        }
    }

    private AtomicReference<Node<E>> head = new AtomicReference<>(new Node<E>(null, null));
    private AtomicReference<Node<E>> tail = head;

    public boolean put(E item) {
        Node<E> newNode = new Node<E>(item, null);
        while (true) {
            Node<E> curTail = tail.get();
            Node<E> residue = curTail.next.get();
            if (curTail == tail.get()) {
                if (residue == null) /* A */ {
                    if (curTail.next.compareAndSet(null, newNode)) /* C */ {
                        tail.compareAndSet(curTail, newNode) /* D */ ;
                        return true;
                    }
                } else {
                    tail.compareAndSet(curTail, residue) /* B */;
                }
            }
        }
    }

    public void remove(E item) {
        Node<E> current = this.head.get().next.get();
        Node<E> next = null;

        while (current != null) {
            next = current.next.get();

            if (next.equals(item)) {
                if (!current.next.compareAndSet(next, next.next.get())) {
                    // some other thread changed the list, do a retry
                    remove(item);
                }
            }

            current = next;
        }
    }

    @Override
    public String toString() {
        Node<E> current = head.get().next.get();
        StringBuilder sb = new StringBuilder();

        while (current != null) {
            sb.append(current).append(", ");
            current = current.next.get();
        }

        return sb.toString();
    }

    public static void main(String[] args) throws InterruptedException {
        final ExecutorService es = Executors.newFixedThreadPool(10);
        final LinkedQueue<Integer> q = new LinkedQueue<>();

        for (int i=0; i<10000; i++) {
            es.execute(new Inserter(q, i));
        }

        // es.shutdown();
        // es.awaitTermination(1L, TimeUnit.HOURS);

        System.out.println("FIN" + q);
    }

    private static class Inserter implements Runnable {
        private final LinkedQueue<Integer> q;
        private final int value;

        public Inserter(LinkedQueue<Integer> q, int value) {
            this.q = q;
            this.value = value;
        }

        @Override
        public void run() {
            q.put(value);
            System.out.println("q = " + q);
        }
    }
}
塔吉尔·瓦列夫(Tagir Valeev)

至少一个明显的错误是您headtail实际上是同一AtomicReference对象,因此,当您更新时tailhead也会被更新。采用

private AtomicReference<Node<E>> head = new AtomicReference<>(new Node<E>(null, null));
private AtomicReference<Node<E>> tail = new AtomicReference<>(head.get());

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

fs.readFileSync始终返回空字符串

来自分类Dev

$ {NSD_GetText}始终返回空字符串

来自分类Dev

在init方法中设置的字符串属性始终返回空字符串

来自分类Dev

卷曲返回空字符串

来自分类Dev

Summernote返回空字符串

来自分类Dev

fgets()返回空字符串

来自分类Dev

NSubstitute返回空字符串

来自分类Dev

车把返回空字符串

来自分类Dev

.html()返回空字符串

来自分类Dev

Edittext返回空字符串

来自分类Dev

为什么null语句ToString()返回空字符串?

来自分类Dev

为什么null语句ToString()返回空字符串?

来自分类Dev

GCMRegistrar.getRegistrationId(this)始终返回空字符串(使用GCM的Android Push通知)

来自分类Dev

GCMRegistrar.getRegistrationId(this)始终返回空字符串(使用GCM的Android Push通知)

来自分类Dev

返回空字符的字符串向量

来自分类Dev

为什么我的函数在python中返回空字符串?

来自分类Dev

用dig解析我的ip返回空字符串

来自分类Dev

html表上的xpath查询总是在python中返回空字符串

来自分类Dev

JSON stringify返回空字符串

来自分类Dev

Location.hash返回空字符串

来自分类Dev

jQuery为此返回空字符串作为ID

来自分类Dev

对.php文件的AJAX请求返回空字符串

来自分类Dev

GCM GetRegistrationId返回空字符串/空

来自分类Dev

Python urllib2返回空字符串

来自分类Dev

preg_replace返回空字符串

来自分类Dev

PHP-使用mysqli返回空字符串

来自分类Dev

WebClient请求返回空字符串

来自分类Dev

Vim函数展开总是返回空字符串

来自分类Dev

PHP中的SaveXml返回空字符串