如何同步访问多个线程访问的数组列表?

时间:2011-11-09 20:53:59

标签: java arraylist add

我有一个需要从GUI的多个组件读取和写入的ArrayList。我已经大大减少了代码量,试图在下面这个简洁的代码段中说明问题。

父帧可能有许多内部帧,每个内部帧都需要自己的ArrayList实例。但是,特定内部框架的所有子组件都需要访问此ArrayList的同一实例,以便在特定内部框架的一个真实ArrayList中维护添加和删除。对于此示例,ArrayList中的所有数据都需要在内存中。但是,每次在内存中进行更改时,我都会添加代码来更新持久性数据文件。

这是我缩减的代码段。任何人都可以告诉我如何更改此代码,以便它给我我寻求的读/写访问权限?此外,任何相关文章的链接将不胜感激。

ParentFrame.java:

package testGlobalArrayList;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Panel;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JTabbedPane;
import java.util.*;

public class ParentFrame extends JFrame{
private static final long serialVersionUID = 1L;
JLayeredPane desktop;
JInternalFrame internalFrame;

public ParentFrame() {
    super("parent frame");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setPreferredSize(new Dimension(600, 300));
    Panel p = new Panel();
    this.add(p, BorderLayout.SOUTH);
    desktop = new JDesktopPane();
    this.add(desktop, BorderLayout.CENTER);
    this.pack();
    this.setSize(new Dimension(600, 300));
    this.setLocationRelativeTo(null);
    final int DELTA = 40;
    int offset = DELTA;
        int ifWidth = 400;
        int ifHeight = 200;
        internalFrame = new JInternalFrame("internal frame", true, true, true, true);
        internalFrame.setLocation(offset, offset);
        offset += DELTA;

        JTabbedPane jtp = createTabbedPane();
        internalFrame.add(jtp);

        // want to make this ArrayList read/write accessible to every GUI component below this level            
        ArrayList<Integer> myArrayList= new ArrayList<Integer>();
        myArrayList.add(8);
        myArrayList.add(6);
        myArrayList.add(7);

        desktop.add(internalFrame);
        internalFrame.pack();
        internalFrame.setSize(new Dimension(ifWidth,ifHeight));
        internalFrame.setVisible(true);
    }
private JTabbedPane createTabbedPane() {
    JTabbedPane jtp = new JTabbedPane();
    jtp.setMinimumSize(new Dimension(600,300));
    createTab(jtp, "Tab1");
    createTab(jtp, "Tab2");
    return jtp;
    }
private void createTab(JTabbedPane jtp, String s) {
    if(s=="Tab1"){
        TestGUI myTimeSeriesGUI = new TestGUI();
        jtp.add(s,myTimeSeriesGUI);
    }
    else{jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));}
    }
public static void main(String args[]) {
    ParentFrame myParentFrame = new ParentFrame();
    myParentFrame.setVisible(true);
    }
}

TestGUI.java:

package testGlobalArrayList;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.Box;

public class TestGUI extends JPanel{

TestGUI(){
    Box verticalBox = Box.createVerticalBox();
    verticalBox.add(new TestPanel());
    verticalBox.add(new TestPanel());
    verticalBox.add(new TestPanel());
    this.add(verticalBox, BorderLayout.CENTER);
}
void anotherMethod(){
    // want to be able to add or delete records to same ArrayList here
    myArrayList.add(5);
    myArrayList.add(3);
    myArrayList.add(0);
    myArrayList.add(9);
}
}

TestPanel.java:

package testGlobalArrayList;

import java.awt.Color;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

public class TestPanel extends JPanel {
public TestPanel (){
    this.setBackground(getRandomColor());
    this.setBorder( new EtchedBorder() );
    this.setSize(150,20);
}
void anotherMethod(){
    //want to be able to add or delete records from same ArrayList here
    myArrayList.remove(1);
    myArrayList.remove(2);
    myArrayList.remove(3);
}
    private static Color getRandomColor(){
    Random rand = new Random();
    float r = rand.nextFloat();
    float g = rand.nextFloat();
    float b = rand.nextFloat();
    Color randomColor = new Color(r, g, b);
        return randomColor;
    }
}

4 个答案:

答案 0 :(得分:1)

您需要将对全局列表(或封装此全局列表的对象)的引用传递给每个组件。在构造函数中传递它是最简单的方法:

ParentFrame.java:

    List<Integer> myArrayList= new ArrayList<Integer>();
    myArrayList.add(8);
    myArrayList.add(6);
    myArrayList.add(7);

    JTabbedPane jtp = createTabbedPane(myArrayList);
    internalFrame.add(jtp);

...

private JTabbedPane createTabbedPane(List<Integer> list) {
    JTabbedPane jtp = new JTabbedPane();
    jtp.setMinimumSize(new Dimension(600,300));
    createTab(jtp, "Tab1", list);
    createTab(jtp, "Tab2", list);
    return jtp;
}
private void createTab(JTabbedPane jtp, String s, List<Integer> list) {
    if ("Tab1".equals(s)){
        TestGUI myTimeSeriesGUI = new TestGUI(list);
        ...

TestGUI.java:

private List<Integer> list;
TestGUI(List<Integer> list) {
    this.list = list;
    ...

答案 1 :(得分:1)

您需要使用控制器来维护这些类型的资源。对于java UI,您可以使用您在需要时通过单例方法访问的控制器。这应该充当您应用的交通警察。

答案 2 :(得分:1)

在构造函数中传递arraylist(并使其成为一个字段)

public class ParentFrame extends JFrame{
    private static final long serialVersionUID = 1L;
    JLayeredPane desktop;
    JInternalFrame internalFrame;
    private List<Integer> myArrayList;

    public ParentFrame() {
        super("parent frame");
        myArrayList= new ArrayList<Integer>();
        myArrayList.add(8);
        myArrayList.add(6);
        myArrayList.add(7);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(600, 300));
        Panel p = new Panel();
        this.add(p, BorderLayout.SOUTH);
        desktop = new JDesktopPane();
        this.add(desktop, BorderLayout.CENTER);
        this.pack();
        this.setSize(new Dimension(600, 300));
        this.setLocationRelativeTo(null);
        final int DELTA = 40;
        int offset = DELTA;
        int ifWidth = 400;
        int ifHeight = 200;
        internalFrame = new JInternalFrame("internal frame", true, true, true, true);
        internalFrame.setLocation(offset, offset);
        offset += DELTA;

        JTabbedPane jtp = createTabbedPane();
        internalFrame.add(jtp);




        desktop.add(internalFrame);
        internalFrame.pack();
        internalFrame.setSize(new Dimension(ifWidth,ifHeight));
        internalFrame.setVisible(true);
    }


    private void createTab(JTabbedPane jtp, String s) {
        if(s=="Tab1"){
            TestGUI myTimeSeriesGUI = new TestGUI(myArrayList);
            jtp.add(s,myTimeSeriesGUI);
        }
        else{jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));}
    }
}

TestGui成为

public class TestGUI extends JPanel{
private List<Integer> myArrayList;

TestGUI(List<Integer> myArrayList){
    Box verticalBox = Box.createVerticalBox();
    verticalBox.add(new TestPanel(myArrayList));
    verticalBox.add(new TestPanel(myArrayList));
    verticalBox.add(new TestPanel(myArrayList));
    this.add(verticalBox, BorderLayout.CENTER);
    this.myArrayList = myArrayList;
}
void anotherMethod(){
    // want to be able to add or delete records to same ArrayList here
    myArrayList.add(5);
    myArrayList.add(3);
    myArrayList.add(0);
    myArrayList.add(9);
}

和TestPanel

public class TestPanel extends JPanel {
private ArrayList<Integer> myArrayList;
public TestPanel (ArrayList<Integer> myArrayList){
    this.setBackground(getRandomColor());
    this.setBorder( new EtchedBorder() );
    this.setSize(150,20);
    this.myArrayList = myArrayList;
}
void anotherMethod(){
    //want to be able to add or delete records from same ArrayList here
    myArrayList.remove(1);
    myArrayList.remove(2);
    myArrayList.remove(3);
}

答案 3 :(得分:1)

您可以使用服务定位器模式(这也可能有助于您持久保存到磁盘)。

这样的东西
public ServiceLocator() {
  private static ArrayHandler handler=new ArrayHandler();

  public static IArrayHandler getArrayHandler() {
    return handler;
  }

}

然后,您可以创建ArrayHandler来处理数组的创建和跟踪。因此,在您的代码中,您可以使用以下内容获取数组:

ServiceLocator.getArrayHandler().getArray("myarray");

这样,当您需要“不再变量”时,您不需要传递参数并更改方法或构造函数签名。无论如何,这是我喜欢和感觉舒适的模式。因人而异。欢呼声。