multithreading

understanding synchronized in java

狂风中的少年 提交于 2020-01-17 07:48:06
问题 Why usually threading samples put so many code in synchronized block. According to my understanding in following case synchronized is used just for locking b for wait and notify: Main class ThreadA : class ThreadA { public static void main(String [] args) { ThreadB b = new ThreadB(); b.start(); synchronized(b) { try { System.out.println("Waiting for b to complete..."); b.wait(); } catch (InterruptedException e) {} System.out.println("Total is: " + b.total); System.out.println(Thread

understanding synchronized in java

て烟熏妆下的殇ゞ 提交于 2020-01-17 07:48:03
问题 Why usually threading samples put so many code in synchronized block. According to my understanding in following case synchronized is used just for locking b for wait and notify: Main class ThreadA : class ThreadA { public static void main(String [] args) { ThreadB b = new ThreadB(); b.start(); synchronized(b) { try { System.out.println("Waiting for b to complete..."); b.wait(); } catch (InterruptedException e) {} System.out.println("Total is: " + b.total); System.out.println(Thread

WinDivert connection loss

时间秒杀一切 提交于 2020-01-17 06:48:55
问题 I am using WinDivert 1.1.8 MSVC x64 in Visual Studio 2015 along with a P/Invoke wrapper for C# to create a simple latency simulator, but the connection fails when performing bandwidth intensive tasks, such as a speedtest. Output ERROR_IO_PENDING 997 (0x3E5) Overlapped I/O operation is in progress. ERROR_INSUFFICIENT_BUFFER 122 (0x7A) The data area passed to a system call is too small. Notes Possible deadlocking caused by x64 remote debugging? Possibly overwhelming the WinDivert queue? Update

C# lock issues when writing to log

心已入冬 提交于 2020-01-17 06:34:14
问题 I have a class that programmatically creates a log using the Nlog framework. I have several processes running and creating their logs at the same time. I added a lock to the constructor of the class, as before the two threads were attempting to create a file at the same time which resulted in some annoying bugs (like only creating one log). This seems to have solved that problem. However now i have the same issue with writing to the log, and using a lock has not helped. Here is the class.

Object publication through constructor

本秂侑毒 提交于 2020-01-17 06:23:39
问题 Consider the following class: class Ideone { private Map<String, String> m; public Ideone(){ synchronized(this){ m = new ConcurrentHashMap<>(); } } public synchronized Map<String, String> getM(){ return Collections.unmodifiableMap(m); //effectively immutable } } In order to allow other classes to observe Ideone 's internal state, we should publish its internal state safely (with correct synchronization). If we don't do this, it's not guaranteed that another thread read the correct value (not

listBox selectedIndex is always 0 unless I make the thread wait

情到浓时终转凉″ 提交于 2020-01-17 06:22:34
问题 I have a listBox populated manually. When a user selects an item I use the value of the selected index to recreate the list with new data. However, it always seems to return 0 unless I put a breakpoint in. With the breakpoint it works perfectly. I looked at the question: WPF ListBox SelectionChanged event and the suggestion to make the thread sleep for 70ms works most of the time. 200ms works even more reliably!! I am guessing this is a threading issue but I don't know how to make the event

listBox selectedIndex is always 0 unless I make the thread wait

扶醉桌前 提交于 2020-01-17 06:22:25
问题 I have a listBox populated manually. When a user selects an item I use the value of the selected index to recreate the list with new data. However, it always seems to return 0 unless I put a breakpoint in. With the breakpoint it works perfectly. I looked at the question: WPF ListBox SelectionChanged event and the suggestion to make the thread sleep for 70ms works most of the time. 200ms works even more reliably!! I am guessing this is a threading issue but I don't know how to make the event

Service vs Thread

☆樱花仙子☆ 提交于 2020-01-17 05:38:55
问题 What should I use to make an application that will: Ask the user for username and password Authorize Run an infinite loop in which it will fetch some data from the website every 10 seconds or so. I want to be able to do some basic tasks in the meantime, or lock my screen without the thread getting killed. I don't want the service to continue running after I close the application, I just want to be sure the thread is never killed while it's running for a long time. I also wanted to ask: Are

Android Vibration while locked/sleep

我与影子孤独终老i 提交于 2020-01-17 05:34:09
问题 Whether the user has the screen on or off I want to notify the user by a vibration. If the screen is on, this works well: Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); long sleep = 100; long vibrate = 500; long[] vibratePattern = {0, vibrate, sleep, vibrate, sleep}; vibrator.vibrate(vibratePattern, -1); Its called by a Thread that is implemented in a Service. The Thread runs always even if the screen is off. I checked that because im

Timed events overlapping during execution

我的未来我决定 提交于 2020-01-17 05:23:07
问题 I am having an issue with the a Service I used to perform automated tasks. The service uses a Timer and executes after 20 seconds. The function that is executed opens the database, reads from it, sends the values through the network, receives a response and updates the database with that response. Its been working well until I wanted to carry out the automated tasks for about 1000 rows in the database and the system 'failed'. After checking my logs, I found out that the function executes