concurrency

How can I avoid blocking with Java ServerSocket?

♀尐吖头ヾ 提交于 2020-01-01 08:06:14
问题 Im working on a socket listener that has to listen on 2 ports for 2 types of data( port 80 and port 81). These data are very similar as in the kind of operations that are performed on the data and are just different because they arrive n different ports. I went ahead and coded an implementation using Java's ServerSocket class, only to realize later that the accept() method of the ServerSocket class is block and my implementation cant afford that. So now i was thinking of implementing the same

How can I avoid blocking with Java ServerSocket?

爷,独闯天下 提交于 2020-01-01 08:06:03
问题 Im working on a socket listener that has to listen on 2 ports for 2 types of data( port 80 and port 81). These data are very similar as in the kind of operations that are performed on the data and are just different because they arrive n different ports. I went ahead and coded an implementation using Java's ServerSocket class, only to realize later that the accept() method of the ServerSocket class is block and my implementation cant afford that. So now i was thinking of implementing the same

Mutating array while reading, not enumerating

馋奶兔 提交于 2020-01-01 07:29:08
问题 If I have two different threads via GCD accessing an NSMutableArray and one is merely creating a new array based off the mutable array while the other thread is deleting records from the array, should I expect this to be a problem? That is, shouldn't the copy, which I presume is merely "reading" the array, just get whatever happens to be in the array at that moment? I am not enumerating the array in either thread, but it is still crashing. As soon as I remove the read routine, it works fine.

Does ConcurrentMap.remove() provide a happens-before edge before get() returns null?

僤鯓⒐⒋嵵緔 提交于 2020-01-01 06:01:06
问题 Are actions in a thread prior to calling ConcurrentMap.remove() guaranteed to happen-before actions subsequent to seeing the removal from another thread? Documentation says this regarding objects placed into the collection: Actions in a thread prior to placing an object into any concurrent collection happen-before actions subsequent to the access or removal of that element from the collection in another thread. Example code: { final ConcurrentMap map = new ConcurrentHashMap(); map.put(1, new

Erlang course concurrency exercise: Can my answer be improved?

旧街凉风 提交于 2020-01-01 05:31:06
问题 I am doing this exercise from the erlang.org course: 2) Write a function which starts N processes in a ring, and sends a message M times around all the processes in the ring. After the messages have been sent the processes should terminate gracefully. Here's what I've come up with: -module(ring). -export([start/2, node/2]). node(NodeNumber, NumberOfNodes) -> NextNodeNumber = (NodeNumber + 1) rem NumberOfNodes, NextNodeName = node_name(NextNodeNumber), receive CircuitNumber -> io:format("Node

Java Memory Model: reordering and concurrent locks

走远了吗. 提交于 2020-01-01 05:10:06
问题 The java meomry model mandates that synchronize blocks that synchronize on the same monitor enforce a before-after-realtion on the variables modified within those blocks. Example: // in thread A synchronized( lock ) { x = true; } // in thread B synchronized( lock ) { System.out.println( x ); } In this case it is garanteed that thread B will see x==true as long as thread A already passed that synchronized -block. Now I am in the process to rewrite lots of code to use the more flexible (and

downloading from AWS S3 while file is being updated

懵懂的女人 提交于 2020-01-01 04:56:05
问题 This may seem like a really basic question, but if I am downloading a file from S3 while it is being updated by another process, do I have to worry about getting an incomplete file? Example: a 200MB CSV file. User A starts to update the file with 200MB of new content at 1Mbps. 16 seconds later, User B starts download the file at 200Mbps. Does User B get all 200MB of the original file, or does User B get ~2MB of User A's changes and nothing else? 回答1: User B gets all 200MB of the original file

How to handle too many concurrent connections even after using a connection pool?

拟墨画扇 提交于 2020-01-01 04:52:06
问题 Scenario Say you have a website or app that has tons of traffic. And even with a database connection pool, performance is taking a real hit (the site/app may even be crashing) because there are too many concurrent connections. Question What are someone's options for dealing with this problem? My thoughts I was thinking someone with this problem could create multiple databases (possibly on different machines although I'm not sure that's necessary), each with the same information and updated at

Why java.util.concurrent.atomic.AtomicBoolean is internally implemented with int?

此生再无相见时 提交于 2020-01-01 03:58:08
问题 AtomicBoolean stores its value in: private volatile int value; Then, for example, extracting its value is done like this: public final boolean get() { return value != 0; } What is the reason behind it? Why boolean was not used? 回答1: AFAIK, int is the smallest type CAS operations can be implemented across different machine types. Note: as object allocations are 8 byte aligned, using a smaller type wouldn't save any memory. 回答2: This probably is to be able to base several of the Atomic classes

SELECT and UPDATE table so there is no overlap of Threads

陌路散爱 提交于 2020-01-01 03:34:06
问题 Say I have the following table: ID|Read ------- 1|true 2|false 3|false 4|false ... and I need to read the smallest ID, that has [Read] == false; plus, update that I have now read it. So if i execute my Stored Procedure dbo.getMinID, it will return ID: 2, and update [Read] -> true. CREATE PROCEDURE [dbo].[getMinID] ( @QueryID INT OUTPUT ) BEGIN SELECT TOP 1 @QueryID = [ID] from Table UPDATE Table SET [Read] = 1 WHERE [ID] = @QueryID END The problem is that I have ten (10) asynchronous Threads