首页 \ 问答 \ 使用LinkedBlockingQueue并刷新到mysql(Using a LinkedBlockingQueue and flush to mysql)

使用LinkedBlockingQueue并刷新到mysql(Using a LinkedBlockingQueue and flush to mysql)

Linkedblockingqueue是否适合以下内容:

1. insert strings (maximum 1024 bytes) into the queue at a very high rate
2. every x inserts or based on a timed interval, flush items into mysql

在刷新期间,我正在查看API: http//docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/LinkedBlockingQueue.html

我们想知道它是一个不错的选择,因为我必须在冲洗前聚合。

所以我会排除队列中的项目,然后迭代并聚合然后写入mysql。

这适用于每秒高达10K的写入器吗?

我是否需要考虑任何锁定/同步问题或者是否已经解决?

我将这个linkedblockingqueue存储为concurrenthashmap中的值。

永远不会从hashmap中删除项目,只有在不存在的情况下才会插入,如果存在,我将追加到队列中。


Would a linkedblockingqueue be suitable for the following:

1. insert strings (maximum 1024 bytes) into the queue at a very high rate
2. every x inserts or based on a timed interval, flush items into mysql

During the flush, I was looking at the API: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/LinkedBlockingQueue.html

At was wondering it drainTo would be a good choice, since I have to aggregate before flushing.

So I would drainTo the items in the queue, then iterate and aggreate and then write to mysql.

Will this be suitable for upto 10K writers per second?

Do I need to consider any locking/synchronization issues or is that taken care of already?

I will store this linkedblockingqueue as the value in a concurrenthashmap.

Items will never be removed from the hashmap, only inserted if not present, and if present, I will append to the queue.


原文:https://stackoverflow.com/questions/8716423
更新时间:2023-12-29 21:12

最满意答案

:为什么我无法使用.parallal()?

:访问外部元素时我该怎么办?

您可以在并行流中使用它,当操作不修改共享状态时没有问题,例如:

stream.peek(System.out::println).allMatch(...);

如果操作修改共享状态,则它负责提供所需的同步,例如:

stream.peek(it->{synchronized(lock){ keys.set(idx, null); }}).allMatch(...);

:为什么我们不应该使用peek()?

您可以使用peek()方法,但如果无法控制流,则应避免使用peek() ,例如:

boolean foo(){ stream.peek(...).allMatch(..); }

//a violate rule
Stream<?> foo(){
  /*you need avoiding using peek() here since you can't control the stream*/
}

:我应该如何将代码“转移”到java 8流?

有一个详细的描述如何在其包汇总java.util.stream中操作流。


Q: Why am I unable to use .parallal()?

Q: What should I do when accessing outside elements?

you can use it in a parallel stream, there is no problem when the action doesn't modify shared state there, for example:

stream.peek(System.out::println).allMatch(...);

If the action modifies shared state, it is responsible for providing the required synchronization, for example:

stream.peek(it->{synchronized(lock){ keys.set(idx, null); }}).allMatch(...);

Q: Why shouldn't we use peek()?

you can using the peek() method, but you should avoiding using peek() if you can't control the stream, for example:

boolean foo(){ stream.peek(...).allMatch(..); }

//a violate rule
Stream<?> foo(){
  /*you need avoiding using peek() here since you can't control the stream*/
}

Q: How should I "transfer" the code to java 8 streams?

there is a detailed description how to operates a stream in its package summary java.util.stream.

相关问答

更多
  • 您必须在流上使用终端操作才能执行(peek不是终端,它是一个中间操作,返回一个新的Stream),例如count(): Stream ss = Stream.of("Hi","Hello","Halo","Hacker News"); ss.parallel().peek(System.out::println).count(); 或者用forEach替换peek (这是终端): ss.parallel().forEach(System.out::println); You ha ...
  • 读取TempDataDictionary的对象时,将在该请求结束时将其标记为删除。 这意味着如果你把某些东西放在TempData上 TempData["value"] = "someValueForNextRequest"; 而另一个请求,您访问它,该值将在那里,但一读取它,该值将被标记为删除: //second request, read value and is marked for deletion object value = TempData["value"]; //third request ...
  • 从设计模式的第108页:Gamma,Helm,Johnson和Vlissides的可重用面向对象软件的要素。 何时使用“出厂方法”模式 一个类不能预期它必须创建的对象类 一个类希望其子类来指定它创建的对象 类将责任委托给几个辅助子类之一,并且您想要本地化哪个助手子类是委托的知识 From page 108 of Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, and Vlissid ...
  • 我假设您正在讨论正在初始化的类的其他私有/公共函数。 像其他任何地方一样,我会说将大型复杂行为分解为子函数是一种很好的做法。 显而易见的复杂性是调用依赖于尚未初始化的对象内容的函数。 由于构造函数和类中的其他函数具有相同的可见性(从代码可维护性的角度来看),我认为可以利用其他函数但程序员必须确保这样做是不合理的。避免了这种循环依赖(如虚函数)。 I assume you are talking about other private/public functions of the class which i ...
  • 构造函数名称必须以区分大小写的方式匹配类名称。 在Account类中,进行更改 account(){ 至 Account(){ 同样适合你的其他构造函数。 Constructor names must match the class names in a case sensitive way. In the Account class, change account(){ to Account(){ and likewise for your other constructor.
  • 因为你是先投票 System.out.println(pq.poll() + ":" + pq.peek()); 因为它是优先级队列元素存储为 carrot -> banana -> apple 当你poll()你得到apple并从队列中删除。 队列的头部是banana ,这正是你peek()时得到的。 请在更新的问题中查看示例2。 排序很奇怪 这正是你不期望的。 你可以看到文档 方法iterator()中提供的迭代器不保证以任何特定顺序遍历优先级队列的元素。 如果需要有序遍历,请考虑使用Arrays. ...
  • 我做到了。 有几个'新的孩子'的错误。 正确的代码: // Add a search bar searchController = UISearchController(searchResultsController: nil) searchController.loadViewIfNeeded() tableView.tableHeaderView = searchController.searchBar searchController.searchResultsUpdater = self searc ...
  • 您可以使用seekg方法在istream内跳转。 如果以二进制模式打开文件,则可以使用tellg方法标记要跳转到的位置。 但是,如果以文本模式打开文件,则最好使用offset来跳转(即strm.seekg(offset,strm.cur)),这是由于文本模式下tellg和seekg计算多行字符(如换行符)的方式。 所以,如果你发现下一个字符是“p”,那么你可以阅读下一个n字符,然后跳回-n字符,如果它不是你想要的。 You can use seekg method to jump around within ...
  • 尝试使用Arrays类...如果需要查看,从Object数组中取出最后一个元素,删除刚刚删除的最后一个元素: class Queue { private Object[] mElements = new Object[] {}; public void enqueue(Object element) { Arrays.insertAt(mElements, element, 0); } public Object dequeue() { O ...
  • 问 :为什么我无法使用.parallal()? 问 :访问外部元素时我该怎么办? 您可以在并行流中使用它,当操作不修改共享状态时没有问题,例如: stream.peek(System.out::println).allMatch(...); 如果操作修改共享状态,则它负责提供所需的同步,例如: stream.peek(it->{synchronized(lock){ keys.set(idx, null); }}).allMatch(...); 问 :为什么我们不应该使用peek()? 您可以使用pee ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。