使用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
最满意答案
问 :为什么我无法使用.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 usingpeek()
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.
相关问答
更多-
如何使用Streams api peek()函数并使其工作?(How to use Streams api peek() function and make it work?)[2022-07-24]
您必须在流上使用终端操作才能执行(peek不是终端,它是一个中间操作,返回一个新的Stream),例如count(): Streamss = 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 ...
-
peek()前方多处?(peek() Multiple Places Ahead?)[2024-03-13]
您可以使用seekg方法在istream内跳转。 如果以二进制模式打开文件,则可以使用tellg方法标记要跳转到的位置。 但是,如果以文本模式打开文件,则最好使用offset来跳转(即strm.seekg(offset,strm.cur)),这是由于文本模式下tellg和seekg计算多行字符(如换行符)的方式。 所以,如果你发现下一个字符是“p”,那么你可以阅读下一个n字符,然后跳回-n字符,如果它不是你想要的。 You can use seekg method to jump around within ... -
Peek()和remove()方法用于Blackberry中的队列实现(Peek() and remove() methods for Queue implementation in Blackberry)[2021-12-01]
尝试使用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 ...