如何创建每次将新元素添加到ArrayList

时间:2019-06-09 14:03:53

标签: java multithreading

我想创建一个方法,每次将新消息添加到groupchat arraylist时都会启动。 伪代码:

public void listenForChange(){
   while(true){
        if(new message is added to the groupchat){
             System.out.println(print the last added message);
        }
   }
 }

我尝试过但无法使用的内容:

public class Groupe{
   ArrayList<String> groupechat;
   int liveChange;

 public void listenForChange() {
    while(true){
        if (groupchat.size() > liveChange){
            liveChange= gruppenchat.size();
            System.out.println(gruppenchat.get(liveChange-1));
        }
    }
}

测试类:

public class testGruppen extends Thread {
Gruppe gruppe;


public TestGroup(){
    gruppe= new Gruppe("Monday");
}

public void run() {
    System.out.println("listen");
    gruppe.listenForChange();
}
public static void main(String[] args) {
    testGruppen test = new testGruppen();
    test.start();
    test.gruppe.write("1"); // write just adds a new String to groupchat
    test.gruppe.write("2");
    test.gruppe.write("3");
    test.gruppe.write("4");

}

}

输出:4而不是1\n 2\n 3\n 4\n

3 个答案:

答案 0 :(得分:1)

使用装饰器怎么样?

public static void main(String... args) {
    List<Integer> group = new FireEventListDecorator<>(new ArrayList<>());
    for (int i = 0; i < 3; i++)
        group.add(i);
}

public static class FireEventListDecorator<E> extends AbstractList<E> {

    private final List<E> delegate;

    public FireEventListDecorator(List<E> delegate) {
        this.delegate = delegate;
    }

    @Override
    public void add(int index, E element) {
        delegate.add(index, element);
        fireEvent(element);
    }

    @Override
    public E get(int index) {
        return delegate.get(index);
    }

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

    private void fireEvent(E element) {
        System.out.println("add: " + element);
    }
}

答案 1 :(得分:0)

为避免使用轮询浪费CPU的while (true)循环,请通过观察者/侦听器模式使用回调方法。一种方法是给拥有ArrayList的类一个PropertyChangeSupport实例,允许它接受侦听器,然后在更改ArrayList的方法中通知侦听器。

例如

public class Group {
    // property listened to: ADD_TEXT
    public static final String ADD_TEXT = "add text";

    // the support object
    private PropertyChangeSupport support = new PropertyChangeSupport(this);
    private List<String> chatText = new ArrayList<>();

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        support.addPropertyChangeListener(ADD_TEXT, listener);
    }

    public void addText(String text) {
        String oldValue = "";
        String newValue = text;
        chatText.add(text + "\n");

        // notify listeners
        support.firePropertyChange(ADD_TEXT, oldValue, newValue);
    }
}

然后可以像这样使用它:

public class TestGroupChat {
    public static void main(String[] args) {
        final Group group = new Group();
        group.addPropertyChangeListener(new GroupListener());
        final String[] texts = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

        new Thread(() -> {
            for (String text : texts) {
                group.addText(text);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {}
            }
        }) .start();
    }

    private static class GroupListener implements PropertyChangeListener {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            // call back method that is called any time the listened-to
            // property has been changed
            System.out.println("Notification: "+ evt.getNewValue());
        }
    }
}

答案 2 :(得分:0)

您应该看看LinkedBlockingQueue class。 当您想要在将新元素添加到队列时唤醒线程时,此类非常有用。在下面的示例中,每次将新消息添加到队列时,线程都会打印该消息并等待下一条消息。

public class Foo extends Thread {

    LinkedBlockingQueue<String> messagesQueue;

    public Foo(LinkedBlockingQueue<String> messagesQueue) {
        this.messagesQueue = messagesQueue;
    }

    @Override
    public voir run() {
        while(true) {
            String message = messagesQueue.take();
           //The thread will sleep until there is a new message in the queue.
           System.out.println(message);
        }
    }

}