multithreading

jvm reordering/visibility effect test

别说谁变了你拦得住时间么 提交于 2020-02-02 03:43:03
问题 While writing some java article I'm trying to reproduce re-ordering in case of unsynchronized object costruction in multithreaded environment. The case when a heavy object is constructed w/o synchonization/volatiles/finals and other threads get access to it right after constructor call. Here is the code I try: public class ReorderingTest { static SomeObject<JPanel>[] sharedArray = new SomeObject[100]; public static void main(String[] args) { for (int i = 0; i < 100; i++) { String name =

How to copy a file with the ability to cancel the copy?

别等时光非礼了梦想. 提交于 2020-02-02 03:08:50
问题 I’m trying to have the program be able to cancel the copy. Therefore I can’t use Microsoft.VisualBasic.FileIO.FileSystem.CopyFile . There are some wrappers for CopyFileEx on the web such as here. However, I rather not use something I don’t understand, not wanting any unexpected results (or bugs). Is there a managed way to do this? Or perhaps a wrapper by MS (in something like Windows API CodePack)? Thanks. 回答1: Read the file in small chunks and write it out to the destination. Periodically

How to copy a file with the ability to cancel the copy?

那年仲夏 提交于 2020-02-02 03:06:50
问题 I’m trying to have the program be able to cancel the copy. Therefore I can’t use Microsoft.VisualBasic.FileIO.FileSystem.CopyFile . There are some wrappers for CopyFileEx on the web such as here. However, I rather not use something I don’t understand, not wanting any unexpected results (or bugs). Is there a managed way to do this? Or perhaps a wrapper by MS (in something like Windows API CodePack)? Thanks. 回答1: Read the file in small chunks and write it out to the destination. Periodically

Understanding multi-threading

柔情痞子 提交于 2020-02-02 03:05:49
问题 I just have a question in regards to threads that run concurrently and the lock they have on an object. From what I understand is that the thread that calls the wait() method will go on a waiting list and allows another thread from a blocked list to take over the lock on and object(within synchronized code). If this thread that now has the lock on the object calls the notify() method it wakes up the thread that called wait() and it is moved to the blocked list. What happens to the thread that

Wait until fragment has been added to the UI

泄露秘密 提交于 2020-02-02 02:48:33
问题 In my app I got to a point where, in landscape mode, I need to attach two fragments. In order to do that, the second fragment needs to wait until the first fragment has been attached (added), before it's being added. The reason for it is that the first fragment needs to execute a function that the second fragment needs. I managed to do that via a thread, but in this case it only waits the amount of time indicated, before attaching the second fragment, and in case the first fragment isn't

How to pause all running threads? and then resume?

亡梦爱人 提交于 2020-02-02 02:46:07
问题 I have already seen this question : How pause and then resume a thread? I have seen many questions in stackoverflow related to my issue, but I couldn't understand them because they are abstract and not specific enough to my sitiuation. There are 2 Count Down Labels. When you click Start Button , the countdown is executed. In the same way, when you click Pause Button , it should be paused. However, I am getting an error: Exception in thread "AWT-EventQueue-0" java.lang

Keyboard interruptable blocking queue in Python

大憨熊 提交于 2020-02-01 15:34:54
问题 It seems import Queue Queue.Queue().get(timeout=10) is keyboard interruptible (ctrl-c) whereas import Queue Queue.Queue().get() is not. I could always create a loop; import Queue q = Queue() while True: try: q.get(timeout=1000) except Queue.Empty: pass but this seems like a strange thing to do. So, is there a way of getting an indefinitely waiting but keyboard interruptible Queue.get()? 回答1: Queue objects have this behavior because they lock using Condition objects form the threading module.

Keyboard interruptable blocking queue in Python

南楼画角 提交于 2020-02-01 15:32:05
问题 It seems import Queue Queue.Queue().get(timeout=10) is keyboard interruptible (ctrl-c) whereas import Queue Queue.Queue().get() is not. I could always create a loop; import Queue q = Queue() while True: try: q.get(timeout=1000) except Queue.Empty: pass but this seems like a strange thing to do. So, is there a way of getting an indefinitely waiting but keyboard interruptible Queue.get()? 回答1: Queue objects have this behavior because they lock using Condition objects form the threading module.

Keyboard interruptable blocking queue in Python

拟墨画扇 提交于 2020-02-01 15:31:36
问题 It seems import Queue Queue.Queue().get(timeout=10) is keyboard interruptible (ctrl-c) whereas import Queue Queue.Queue().get() is not. I could always create a loop; import Queue q = Queue() while True: try: q.get(timeout=1000) except Queue.Empty: pass but this seems like a strange thing to do. So, is there a way of getting an indefinitely waiting but keyboard interruptible Queue.get()? 回答1: Queue objects have this behavior because they lock using Condition objects form the threading module.

Is my code thread-unsafe?

梦想的初衷 提交于 2020-02-01 14:23:12
问题 I have wrote code to understand CyclicBarrier. My application emulates election. Each rounds selects candidate with small votes and this candidate eliminates from the race for victory. source: class ElectoralCommission { public volatile boolean hasWinner; public volatile String winner; private List<String> candidates; private Map<String, Integer> results = new ConcurrentHashMap<>(); ElectoralCommission(List<String> candidates) { this.candidates = candidates; } public void acceptVote(int index