concurrency

Guava MultiMap and ConcurrentModificationException [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-30 08:27:49
问题 This question already has answers here : Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop (24 answers) Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . I don't understand why I get a ConcurrentModificationException when I iterate through this multimap . I read the following entry, but I am not sure if I understood the whole thing. I tried to add a synchronized block. But my doubt is

Guava MultiMap and ConcurrentModificationException [duplicate]

佐手、 提交于 2019-12-30 08:27:01
问题 This question already has answers here : Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop (24 answers) Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . I don't understand why I get a ConcurrentModificationException when I iterate through this multimap . I read the following entry, but I am not sure if I understood the whole thing. I tried to add a synchronized block. But my doubt is

File.WriteAllText and Concurrent Accesses

人盡茶涼 提交于 2019-12-30 08:08:13
问题 Suppose I'm writing a very long string to a file using File.WriteAllText, and another thread or process is trying to read the same file. Would it throw any exception? In other words, what is the FileShare parameter that the File.WriteAllText method uses? It's not written in the documentation! 回答1: This is the source code from .net Framework 4.0. clearly StreamWriter is used that Uses FileShare.Read Internally. [SecuritySafeCritical] public static void WriteAllText(string path, string contents

File.WriteAllText and Concurrent Accesses

梦想的初衷 提交于 2019-12-30 08:08:07
问题 Suppose I'm writing a very long string to a file using File.WriteAllText, and another thread or process is trying to read the same file. Would it throw any exception? In other words, what is the FileShare parameter that the File.WriteAllText method uses? It's not written in the documentation! 回答1: This is the source code from .net Framework 4.0. clearly StreamWriter is used that Uses FileShare.Read Internally. [SecuritySafeCritical] public static void WriteAllText(string path, string contents

Individual timeouts for concurrent.futures

丶灬走出姿态 提交于 2019-12-30 08:04:52
问题 I see two ways to specify timeouts in concurrent.futures. as_completed() wait() Both methods handle N running futures. I would like to specify an individual timeout for each future. Use Case: Future for getting data from DB has a timeout of 0.5 secs. Future for getting data from a HTTP server has a timeout of 1.2 secs. How do I handle this with concurrent.futures ? Or is this library not the right tool? Conclusion AFAIK the solution by mdurant is a good work-around. I think I will use a

Individual timeouts for concurrent.futures

大城市里の小女人 提交于 2019-12-30 08:04:43
问题 I see two ways to specify timeouts in concurrent.futures. as_completed() wait() Both methods handle N running futures. I would like to specify an individual timeout for each future. Use Case: Future for getting data from DB has a timeout of 0.5 secs. Future for getting data from a HTTP server has a timeout of 1.2 secs. How do I handle this with concurrent.futures ? Or is this library not the right tool? Conclusion AFAIK the solution by mdurant is a good work-around. I think I will use a

What is the correct way to create an already-completed CompletableFuture<Void>

允我心安 提交于 2019-12-30 07:59:10
问题 I am using Completable futures in java 8 and I want to write a method that, based on a received parameter, either runs multiple tasks with side effects in parallel and then return their "combined" future (using CompletableFuture.allOf() ), or does nothing and returns an already-completed future. However, allOf returns a CompletableFuture<Void> : public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) And the only way to create an already-completed future that know is using

Ensure that code within CompletableFuture callback executes after

◇◆丶佛笑我妖孽 提交于 2019-12-30 07:47:08
问题 Say I have this: public void foo(){ CompletableFuture.delayedExecutor(1, TimeUnit.MILLISECONDS).execute(() -> { doSomethingA(); }); doSomethingB(); } Is there any guarantee that doSomethingB(); will always run before doSomethingA();? Something tells me with thread pre-emption , it's possible, although unlikely, that doSomethingA() could be run first? 回答1: No, there is no guarantee, when on a machine which has multiple processors, that doSomethingB() would always execute before doSomethingA().

high resolution timer in java

瘦欲@ 提交于 2019-12-30 07:01:19
问题 I want to simulate TCP in Java. For that I have multiple threads, like sender and receiver threads for every TCP connection. My problem is, I want to pause (like Thread.sleep()) threads for microseconds time interval. So that I can simulate flow control, where sender thread will block for microseconds before sending next packet, and at same time CPU can be used by receiving and data processing threads. But I couldn't find any methods which perform sleep() or wait() for microsecond or

Java synchronized method around parameter value

无人久伴 提交于 2019-12-30 06:44:20
问题 Consider the following method: public void upsert(int customerId, int somethingElse) { // some code which is prone to race conditions } I want to protect this method from race conditions, but this can only occur if two threads with the same customerId are calling it at the same time. If I make the whole method synchronized it will reduce the efficiency and it's not really needed. What I really want is to synchronize it around the customerId . Is this possible somehow with Java? Are there any