synchronized Questions

4

Solved

private double value; public synchronized void setValue(double value) { this.value = value; } public double getValue() { return this.value; } In the above example is there any point in making ...
Weltpolitik asked 12/7, 2012 at 19:50

2

How do I synchronize access of a property that uses didSet (using GCD or objc_sync_enter)? I have a property that has a property observer. How can I use a private queue to synchronize get/set of t...
Riehl asked 17/5, 2017 at 5:5

4

I came across a code like this synchronized(obj) { obj = new Object(); } Something does not feel right about this , I am unable to explain, Is this piece of code OK or there is something rea...
Baynebridge asked 15/7, 2011 at 18:51

4

Solved

On my server I call two emits at the same time, which looks like this. if (songs.length > 0) { socket.emit('data loaded', songs); socket.broadcast.to(opponent).emit('data loaded', songs)...
Quality asked 25/12, 2016 at 10:52

1

Solved

Here is the code I've stumbled across: class TransactionContextHolder { private static final ThreadLocal<TransactionContext> currentTransactionContext = new NamedInheritableThreadLocal<T...
Incardination asked 25/12, 2016 at 18:40

3

Solved

If I want to ensure exclusive access to an object in Java, I can write something like this: ... Zoo zoo = findZoo(); synchronized(zoo) { zoo.feedAllTheAnimals(); ... } Is there a way to ...
Leprosy asked 26/5, 2011 at 9:37

4

Solved

Let's say I have an object as follows: Map<String, String> m = new HashMap<>(); Then I synchronize on this object as follows and change its reference: synchronize(m){ m = new HashM...

3

Solved

In a nice article with some concurrency tips, an example was optimized to the following lines: double getBalance() { Account acct = verify(name, password); synchronized(acct) { return acct.balan...
Kisumu asked 23/6, 2010 at 15:45

6

Solved

What is the difference between a synchronized method and synchronized block in Java ? I have been searching the answer on the Net, people seem to be so unsure about this one :-( My take wou...

6

Solved

If I just use synchronized, not the wait/notify methods, will it still be thread-safe? What's the difference?
Selffulfillment asked 20/10, 2011 at 21:6

2

I've wrote some multithreading code in java and synchronized method that changed variable, but it doesn't synchronized my code, I still get random values. There is my code: public class Main { pu...
Hollington asked 14/8, 2016 at 6:47

6

Solved

I have always thought that synchronizing the run method in a java class which implements Runnable is redundant. I am trying to figure out why people do this: public class ThreadedClass implements ...
Twentyone asked 5/9, 2011 at 23:47

3

Solved

I want to force cancel AsyncTask. I see that you can use isCancelled() like in this valid solution (which under the hood uses AtomicBoolean. But I see solutions like suspiciousSolution1, suspiciou...

8

Solved

A warning is showing every time I synchronize on a non-final class field. Here is the code: public class X { private Object o; public void setO(Object o) { this.o = o; } public voi...
Newmann asked 2/8, 2011 at 10:44

3

Solved

I'm using EclEmma for coverage analysis. My Java code includes a synchronized(MyClass.class) {} block. EclEmma says it is only partially covered, event though I've got a unit test in which one th...
Necessitous asked 15/9, 2010 at 6:34

3

I'm trying to synchronize my Person class Methods so that my static counter variable gets decremented by one thread at the a time. public class Person extends Thread { private static int count ...
Aarhus asked 20/4, 2016 at 19:10

3

Solved

my problem is that: search_text.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { ArrayList<Object> GPDMvalue = (ArrayList<Object>) multiSor...
Treed asked 1/8, 2014 at 1:38

1

Solved

Let's imagine the next piece of Kotlin code that performs some query to a database by means a JDBC connector: var results : ResultSet preparedStatement.clearParameters() preparedStatement.setIn...
Palmieri asked 27/3, 2016 at 10:51

1

I have a singleton factory (edit: renamed "loader" to avoid confusion with factory pattern) that creates objects (in my example DAOs) or returns them if already created: public class DAOLoader { ...
Chew asked 29/2, 2016 at 18:45

4

Solved

I have 4 methods (m1, m2, m3 and m4) in a class. Method m1, m2 and m3 are synchronized methods. Also, I have 4 threads t1, t2, t3 and t4 respectively. If t1 access the m1 method (synchronized meth...
Trademark asked 1/7, 2010 at 17:6

6

I'm trying to learn about threads and synchronization. I made this test program: public class Test { static List<Thread> al = new ArrayList<>(); public static void main(String[] arg...
Pedigree asked 9/2, 2016 at 21:51

1

I try to prove that synchronized is slower when there are many readers and only some writers. Somehow I proved the opposite. The RW example, time of execution is 313 ms: package zad3readWriteLock...
Cleat asked 5/1, 2016 at 11:52

1

Solved

I decided to dig into source code a bit and noticed that Collections.synchronizedList(List) is implemented as follows: public static <T> List<T> synchronizedList(List<T> list) { ...
Anagnos asked 4/1, 2016 at 7:16

4

Solved

There is something i haven't yet understand about synchronized and volatile. I understand that a thread can safe changes locally. From what i have read so far is that synchronized > volatile. Say...
Isocyanide asked 30/12, 2015 at 10:32

3

Say I have such a method: synchronized void incrementIndex() { index++; } I want to unit test this method to see whether index's final value is set correctly if multiple threads simultaneously ...
Kimber asked 17/11, 2014 at 6:47

© 2022 - 2024 — McMap. All rights reserved.