Questions tagged [synchronized-block]
85 questions
1
vote
3 answers
Using synchronized in Java
What's the difference between:
public synchronized void test(){}
and
public void test() {
synchronized(Sample.class){}
}

ward
- 31
- 3
1
vote
3 answers
Synchronization in Java - Vector vs ArrayList
I am attempting to understand the difference between the Vector and ArrayList classes in terms of thread-safety. Vector is supposedly internally synchronized. Is it synchronized by each element, or as a whole? (I could imagine the case where…

Alexander Mills
- 90,741
- 139
- 482
- 817
1
vote
2 answers
Synchronized block in java
I came across the code:
synchronized(Account.this)
{}
where Account is a class.
Does Account.this mean any current instance of class Account?

Sameer Sarmah
- 1,052
- 4
- 15
- 33
1
vote
3 answers
Java synchronization and collections
If a synchronized block of code contains an unsynchronized collection. Is the collection considered thread safe? If not, can you provide any practical scenarios where two threads could unsafely access the collection within the synced code?
Thanks.

sotn
- 1,833
- 5
- 35
- 65
1
vote
1 answer
Why does the code hang when calling cancelInquiry in Java to cancel, a Bluetooth inquiry, on some Nokia phones?
i have a problem on Nokia devices having S40 (but not on S60).
the problem is this:
when you call the discoveryAgent's cancelInquiry method from within a synchronized block,
it hangs. it is actually supposed to call back inquiryCompleted. the code…

inor
- 2,781
- 2
- 32
- 42
0
votes
3 answers
Java Threads: synchronized blocks
I need some help to make sure that I understand synchronized blocks. Assuming the following Example:
public class ThreadStarter {
public static void main(String[] args) {
Queue queueObject = new Queue();
ThreadA thread1 = new…

Bins Ich
- 1,822
- 6
- 33
- 47
0
votes
1 answer
A mutex blocks only the main thread when it reaches its call with the @synchronized directive
I'm building a multithreaded application, from which more than one thread can write to an sqlite3 database including the main thread. I declared a static public variable to be used for the mutex:
@implementation Application
#pragma mark -
#pragma…

Mousa
- 2,926
- 1
- 27
- 35
0
votes
2 answers
Synchronized block still locked after thread restart
I try to restart thread but synchronized block in thread keep locked after restarted. I shouldn't change socket properties because some processes take too long but when network connection lost it hangs forever. I try to use InterruptedException but…

Burak Kocaman
- 81
- 1
- 10
0
votes
0 answers
What is the correct way to avoid an empty synchronized block?
Recently I've started looking into multithreading, and I have a question, perhaps more experienced ones could help.
My program creates two parallel threads, each of them prints counts from 0 to 19 (the NumbersPrinter class, which implements the…

vetal22331122
- 65
- 1
- 1
- 8
0
votes
2 answers
Object in Synchronized code returned from a method
I want to have a synchronized block where the Object to synchronize is returned from a method call:
...
synchronized( someGetMethod() ) {
// synchronized block
}
...
Is there an assumption that the "someGetMethod" is synchronized or only the "//…

Tomer Armarnik
- 11
- 4
0
votes
1 answer
What if we use only the outer null check in double check singleton pattern?
Question 1: Why in singleton pattern for multi-threading we need two null checks? What if we use only the outer check?
if (instance == null) {
synchronized (ABC.class) {
// What if we remove this check?
if…

Xigstan
- 7
- 1
0
votes
1 answer
Why `synchronized (lock)` was entered twice by different threads?
In this simple example I have two synchronized (theLock) that are accessed by different threads
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("start");
final Object…

Vitalii
- 10,091
- 18
- 83
- 151
0
votes
1 answer
AtomicInteger vs synchronized block
I have a problem where I need to synchronize access to array of integers in java. My code looks something like this.
Class X {
int[] counters = new int[100];
Object lock = new Object ();
void increment (int idx){
…

grk939
- 53
- 1
- 7
0
votes
3 answers
Why my synchronized method not working properly?
I have this synchronized method that prints counter, I have 4 Threads so I am expecting final value of my counter to be 400000 as my counter is a static variable.
but every time I run my code, it is giving me different values of counter.
Following…

LeoScott
- 63
- 1
- 9
0
votes
1 answer
Increment Value of in a Hashmap in a thread safe manner keeping high performance without synchronized?
I have a Model which has different Variables.
public class Model implements Serializable{
public final static int STATE_INIT = 0;
public final static int STATE_READY = 1;
private Integer state = STATE_INIT;
private…

INFOSYS
- 1,465
- 9
- 23
- 50