首页 \ 问答 \ java:并发集合(java: concurrent collections)

java:并发集合(java: concurrent collections)

我正在尝试找到一个或多个要使用的并发集合,我可以实现以下行为(这些名称是出于类比目的而设计的):

/**
 * Acts as a broker for a concurrent hash map that stores its keys in order 
 * of submission. At shipping time, the concurrent map is "sealed" 
 * (picture a truck with its cargo door being closed)
 * and its contents presented as an immutable map, and is replaced 
 * by a new concurrent map ready to accept values.
 *
 * Consumers of this class that submit information to it, are expected to 
 * know that this contains a concurrent collection, and should use the 
 * compareAndSet paradigm, e.g. the following:
 *
 * LoadingDock loadingDock = ...
 * boolean done = false;
 * while (!done)
 * {
 *    V oldValue = loadingDock.get();
 *    V newValue = computeNewValue(oldValue, otherInformation);
 *    if (oldValue == null)
 *       done = loadingDock.putIfAbsent(newValue) == null;
 *    else
 *       done = loadingDock.replace(oldValue, newValue) == oldValue;
 * }
 *    
 *
 * Keys and values must be non-null. Keys are not ordered.
 */
class LoadingDock<K,V>
{
    /**
     * analogous to ConcurrentMap's replace, putIfAbsent, and get methods
     */
    public boolean replace(K key, V oldValue, V newValue);
    public V putIfAbsent(K key, V value);
    public V get(K key)

    /* see above */
    public Map<K,V> ship();
}

我有两个问题。

一个是Java和Guava都不包含ConcurrentLinkedHashMap。 这让我想知道为什么不 - 也许我错过了这样一个野兽的微妙之处。 看起来我可以通过使用一个类来装饰ConcurrentHashMap来自己创建一个,如果putIfAbsent()并且返回null,那么该类会向列表添加一个键 - 我不需要在ConcurrentHashMap中使用任何其他方法。上面,除了通过调用putIfAbsent()之外,没有办法向地图添加新密钥。

另一个更隐蔽的问题是,我似乎无法想到如何在不阻塞同步的情况下实现ship() - 当调用ship()时,LoadingDock需要将所有新调用指向新映射,并且可以返回旧映射,直到确定所有并发写入完成为止。 (否则我只会使用AtomicReference来保存并发映射。)

有没有办法做到这一点无需同步?


I'm trying to find one or more concurrent collections to use that I can implement the following behavior (the names are contrived for analogy purposes):

/**
 * Acts as a broker for a concurrent hash map that stores its keys in order 
 * of submission. At shipping time, the concurrent map is "sealed" 
 * (picture a truck with its cargo door being closed)
 * and its contents presented as an immutable map, and is replaced 
 * by a new concurrent map ready to accept values.
 *
 * Consumers of this class that submit information to it, are expected to 
 * know that this contains a concurrent collection, and should use the 
 * compareAndSet paradigm, e.g. the following:
 *
 * LoadingDock loadingDock = ...
 * boolean done = false;
 * while (!done)
 * {
 *    V oldValue = loadingDock.get();
 *    V newValue = computeNewValue(oldValue, otherInformation);
 *    if (oldValue == null)
 *       done = loadingDock.putIfAbsent(newValue) == null;
 *    else
 *       done = loadingDock.replace(oldValue, newValue) == oldValue;
 * }
 *    
 *
 * Keys and values must be non-null. Keys are not ordered.
 */
class LoadingDock<K,V>
{
    /**
     * analogous to ConcurrentMap's replace, putIfAbsent, and get methods
     */
    public boolean replace(K key, V oldValue, V newValue);
    public V putIfAbsent(K key, V value);
    public V get(K key)

    /* see above */
    public Map<K,V> ship();
}

I'm having two issues with this.

One is that neither Java nor Guava contain a ConcurrentLinkedHashMap. This makes me wonder why not -- maybe I'm missing the subtleties of such a beast. It looks like I could just make one myself by decorating a ConcurrentHashMap with a class that adds a key to a list if putIfAbsent() is ever called and returns null -- I don't need any of the other methods in ConcurrentHashMap beyond the ones above, so there's no way to add a new key to the map except through a call to putIfAbsent().

The other, more insidious issue, is that I can't seem to think of how to implement ship() without blocking synchronization -- when ship() is called, the LoadingDock needs to direct all new calls to the new map, and can't return the old map until it is certain all of the concurrent writes are done. (Otherwise I would just use AtomicReference to hold the concurrent map.)

Is there a way to do this w/o having to synchronize?


原文:https://stackoverflow.com/questions/9215418
更新时间:2024-01-28 10:01

最满意答案

我发现了一种很好的方式来通过以下方式重写paintComponent:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    drawWhiteSpace(g);
    drawBorder(g);
}

drawWhiteSpace方法将绘制一个白色矩形,从左下角-6像素开始,然后转到右下角

private void drawWhiteSpace(Graphics g) {
    int x1 = 0;
    int y1 = this.getSize().height-6;
    int x2 = this.getSize().width;
    int y2 = this.getSize().height;
    g.setColor(Color.WHITE);
    g.fillRect(x1, y1, x2, y2);     
}   

但是边框不好,所以我不得不把它移除并自己绘制,从左上角到白色空间的右上角绘制它

private void drawBorder(Graphics g) {
    int x1 = 0;
    int y1 = 0;
    int x2 = this.getSize().width-1;
    int y2 = this.getSize().height-6;
    g.setColor(Color.GRAY);
    g.drawRect(x1, y1, x2, y2); 
}

I found a great way to do that by rewriting the paintComponent that way:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    drawWhiteSpace(g);
    drawBorder(g);
}

drawWhiteSpace method will draw a white rectangle which start at the bottom-left corner -6 pixel, and go to the bottom rightcorner

private void drawWhiteSpace(Graphics g) {
    int x1 = 0;
    int y1 = this.getSize().height-6;
    int x2 = this.getSize().width;
    int y2 = this.getSize().height;
    g.setColor(Color.WHITE);
    g.fillRect(x1, y1, x2, y2);     
}   

however the border was not fine, so i had to remove it and to draw it myself by drawing it from the top-left corner to the top-right corner of the white space

private void drawBorder(Graphics g) {
    int x1 = 0;
    int y1 = 0;
    int x2 = this.getSize().width-1;
    int y2 = this.getSize().height-6;
    g.setColor(Color.GRAY);
    g.drawRect(x1, y1, x2, y2); 
}

相关问答

更多
  • 只需玩椭圆形项目值来获得所需的输出。 curve_toolbar_bg.xml
  • 或者1)绘制一个BufferedImage,然后将其显示在paintComponent重写中,或者2)将数据放入ArrayList或其他集合中,然后遍历paintComponent的集合。 如果我需要将数据用于其他目的,我会做后者。 另外,永远不要这样做: public void update(Graphics g) { paintComponent(g); } 这不是Swing图形应该如何完成的,并且是潜在的危险代码。 请阅读: 基本的Swing图形教程 高级Swing图形信息 编辑 关于选项1 ...
  • setBounds用于确定组件在其父容器中的位置和大小。 这不是你想要做的。 除非您使用绝对布局,否则不应使用此方法。 相反,尝试 component.scrollRectToVisible(new Rectangle(x1, y1, 500, 500)); 代替... 检出JComponent#scrollRectToVisible setBounds is used to determine the location and size of a component within it's parent ...
  • 这是我必须说的一个常见问题。 这些教程对我有用: Tutorial1 ---它描述的问题显示了页面的外观等。 Tutorial2 ---这里有一个很好的工作代码示例,我用它来解决我的问题。 作者还介绍了缩放,翻译等用于打印的用途。 我希望它也会对你有所帮助。 也许当你更了解你的问题意味着什么很棒时,我可以提供更多帮助。 :) EDIT1: double scale = 1; //scale only when component is wider then a page (page and ...
  • 我发现了一种很好的方式来通过以下方式重写paintComponent: public void paintComponent(Graphics g){ super.paintComponent(g); drawWhiteSpace(g); drawBorder(g); } drawWhiteSpace方法将绘制一个白色矩形,从左下角-6像素开始,然后转到右下角 private void drawWhiteSpace(Graphics g) { int x1 = 0; ...
  • 将背景绘制到BufferedImage,然后使用g.drawImage(...)将BufferedImage绘制到paintComponent(...)方法中的GrahpicPanel。 您可以通过调用getGraphics()或createGraphics() (对于Graphics2D对象)获取BufferedImage的Graphics上下文。 不要忘记处理以这种方式获得的Graphics对象(但永远不要处理JVM为您提供的Graphics对象)。 另外,不要忘记在覆盖中调用super.paintCo ...
  • 您可以使用分区根据给定谓词对白色像素进行分组。 在这种情况下,您的谓词可以是: 对给定欧几里德距离内的所有白色像素进行分组 。 然后,您可以计算每个组的边界框,保留最大的框(在下面的RED中),并最终放大它(在下面的绿色中): 码: #include #include #include using namespace std; using namespace cv; int main() { // Load th ...
  • 不要重新发明轮子,使用JPanels ,更好地使用GridLayout铺设的JLabels 通过使用JLabels ( JPanel嵌套多个JComponents ),没有理由使用paintComponents 注意JLabel透明,不透明, 使用SwingX JCalendar / JDatePicker ,我最喜欢的是Kai Toedter的JCalendar ,(渲染器,编辑器,特殊日期,最小和最大日期没有问题) don't to reinvent the wheel, use JPanels, be ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)