multithreading

What is the difference between a synchronized function and synchronized block? [duplicate]

∥☆過路亽.° 提交于 2020-01-19 17:36:46
问题 This question already has answers here : What is the difference between a synchronized method and synchronized block in Java? [duplicate] (6 answers) Closed 2 years ago . what is the difference between public synchronized void addition() { //something; } and public void addtion() { synchronized (//something) { //something; } } If I am wrong Ignore this question. 回答1: it the first one only one thread can execute whole method at a time whereas in second one only one thread can execute that

What is the difference between a synchronized function and synchronized block? [duplicate]

為{幸葍}努か 提交于 2020-01-19 17:35:56
问题 This question already has answers here : What is the difference between a synchronized method and synchronized block in Java? [duplicate] (6 answers) Closed 2 years ago . what is the difference between public synchronized void addition() { //something; } and public void addtion() { synchronized (//something) { //something; } } If I am wrong Ignore this question. 回答1: it the first one only one thread can execute whole method at a time whereas in second one only one thread can execute that

Android ANRs from code running in a Handler?

青春壹個敷衍的年華 提交于 2020-01-19 17:35:47
问题 A game I wrote some time ago has a problem with ANRs, and debugging suggests they're down to HTTP requests taking a long time (and thus causing the ANR). I'd thought that by assigning the HTTP code into a Runnable called from within a Handler, I'd could avoid the ANR - but it seems this isn't the case? The stack dumps suggest the runnable/handler code is still running within the 'Main' thread and thus still causes ANRs?? The task it's doing is asynchronous (uploading highscores and

Android ANRs from code running in a Handler?

社会主义新天地 提交于 2020-01-19 17:35:14
问题 A game I wrote some time ago has a problem with ANRs, and debugging suggests they're down to HTTP requests taking a long time (and thus causing the ANR). I'd thought that by assigning the HTTP code into a Runnable called from within a Handler, I'd could avoid the ANR - but it seems this isn't the case? The stack dumps suggest the runnable/handler code is still running within the 'Main' thread and thus still causes ANRs?? The task it's doing is asynchronous (uploading highscores and

Concurrent byte array access in Java with as few locks as possible

依然范特西╮ 提交于 2020-01-19 15:23:13
问题 I'm trying to reduce the memory usage for the lock objects of segmented data. See my questions here and here. Or just assume you have a byte array and every 16 bytes can (de)serialize into an object. Let us call this a "row" with row length of 16 bytes. Now if you modify such a row from a writer thread and read from multiple threads you need locking. And if you have a byte array size of 1MB (1024*1024) this means 65536 rows and the same number of locks. This is a bit too much, also that I

Lock only if value is same?

China☆狼群 提交于 2020-01-19 12:41:09
问题 If i have an function that edits the database in my webservice that i only want one thread to execute at one time if they try to edit the same row. void EditCheck(long checkid) { if (isCheckCosed) throw new Exception("check is already closed"); //do stuff //after i edit my check i want to close it. CloseCheck(); } i know i can lock the whole function, but then i loose preformance because its almost never that different threads will try to edit the same check. is there a way to only lock out

Time limit on individual threads with ExecutorService

眉间皱痕 提交于 2020-01-19 07:35:10
问题 I have an ExecutorService managing a number of Callables. The tasks that the Callables run are mostly black box transformations and number crunching. Under certain conditions, the data being transformed will oscillate and the thread will take over an hour to finish. For comparison, most threads are completed within a minute. It's been deteremined that the data from the long-running threads is not relevent. I would like to interrupt any thread that runs longer than a certain amount of time.

Threading UI updates in Android

核能气质少年 提交于 2020-01-19 05:44:25
问题 I've just started with android development and updating the UI is really bugging me :/ This is what I've got working so far - package projects.Move; import android.os.Bundle; import android.view.View; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Color; public class Move extends Activity { private float y = 0; private long now = 0; private float delay = 75; private Paint paint = new Paint();

How many threads Parallel.For(Foreach) will create? Default MaxDegreeOfParallelism?

三世轮回 提交于 2020-01-19 05:40:29
问题 I want to know, how many threads will be used when I run Parallel.For/ForEach loop. I found, that it can be changed by MaxDegreeOfParallelism option. MaxDegreeOfParallelism help on MSDN says (link): By default, For and ForEach will utilize however many threads the underlying scheduler provides, so changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used. But I don't know how many threads underlying scheduler provides. How can I find out that? I could

Why GCC does not use LOAD(without fence) and STORE+SFENCE for Sequential Consistency?

风格不统一 提交于 2020-01-19 04:58:07
问题 Here are four approaches to make Sequential Consistency in x86/x86_64: LOAD(without fence) and STORE+MFENCE LOAD(without fence) and LOCK XCHG MFENCE+LOAD and STORE(without fence) LOCK XADD(0) and STORE(without fence) As it is written here: http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html C/C++11 Operation x86 implementation Load Seq_Cst: MOV (from memory) Store Seq Cst: (LOCK) XCHG // alternative: MOV (into memory),MFENCE Note: there is an alternative mapping of C/C++11 to x86, which