Questions tagged [synchronized]

A block or method is said to be 'synchronized' if entry to it is controlled by the Java `synchronized` keyword. This causes access to it to be restricted to a single thread at a time: when concurrent accesses occur, they are sequentialized in an unspecified order.

A block or method is said to be 'synchronized' if entry to it is controlled by the Java synchronized keyword. This causes access to it to be restricted to a single thread at a time: when concurrent accesses occur, they are sequentialized in an unspecified order.

1865 questions
0
votes
4 answers

Java: synchronize keyword doesn't block object on different thread

public class SynchronizeTest { public synchronized void methodA() { System.out.println("calling method a ..."); sleep(2000); System.out.println("ending method a"); } public void methodC() { …
Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107
0
votes
1 answer

Wait for daemon threads to complete an iteration using an executor service

I have to parallelize an existing background task such that instead of serially consuming 'x' resources, it parallely finishes the job at hand using only 'y' threads (y << x). This task constantly runs in the background and keeps processing some…
0
votes
1 answer

Scala Partial Function Application Semantics + locking with synchronized

Based on my previous question on locks based on value-equality rather than lock-equality, I came up with the following implementation: /** * An util that provides synchronization using value equality rather than referential equality * It is…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
0
votes
3 answers

Questions about how the synchronized keyword works with locks and thread starvation

In this java tutorial there's some code that shows an example to explain the use of the synchronized keyword. My point is, why I shouldn't write something like this: public class MsLunch { private long c1 = 0; private long c2 = 0; …
0
votes
1 answer

Skip synchronization when iterating over a LinkedHashSet and no remove is done?

I have to iterate on a Set which is shared among multiple threads. Something like this code: class MyObj{} final static Set instances = Collections.synchronizedSet(new LinkedHashSet()); // returns the same object from the set if any,…
Aldian
  • 2,592
  • 2
  • 27
  • 39
0
votes
1 answer

Synchronize a variable throughout all the threads of a Storm Topology

I have a storm topology to write some data from Kafka queue to Cassandra DB. The program is a multithreaded one. To facilitate the cassandra db insertion, I have this as my DBUtils: public DBUtils() { if(session == null) { session =…
0
votes
4 answers

How does a synchronized statement work in this case?

Assume I have a class like this: public class Server { public static void main(String[] args) { Map registry = Collections.synchronizedMap(new LinkedHashMap()); ... while(true)…
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0
votes
1 answer

How can two threads enter two synchronized blocks which hold a lock on the same object

I have some code like this: public class HelloWorld { public static void main(String[] args){ ThreadB b = new ThreadB(); b.start(); Runnable a = new Runnable(){ public void run(){ …
trans1st0r
  • 2,023
  • 2
  • 17
  • 23
0
votes
1 answer

Synchronize on variable only when it is being updated

Usecase : Rotation of credentials for a datastore What I want : When updateCredentials is called, it will wait until it all threads are done fetching credentials (via the synchronize) to update the credentials to the new ones. I DO NOT want calls…
Malcolm O'Hare
  • 4,879
  • 3
  • 33
  • 53
0
votes
0 answers

HashMap with synchronized block:

After these posts Locking value objects of a ConcurrentHashMap ConcurrentHashMap: how to replace the value of an entry only if the current value is smalle Can we use Synchronized for each entry instead of ConcurrentHashMap? And spending so much…
Mohammad Roohitavaf
  • 439
  • 2
  • 6
  • 15
0
votes
1 answer

why synchronized object can still be manipulated

Code snippet like below: List list = new ArrayList<>(); public void addValue(int i) { synchronized (list) { list.add("list" + i); } } My question is that what is locked by keyword synchronized. What will be checked of the…
xuanzhui
  • 1,300
  • 4
  • 12
  • 30
0
votes
1 answer

Why this code have performance issue even without synchronized keyword?

Why following code have performance issue, the frame from camera is not smooth. public class VideoCaptureAndroid implements PreviewCallback, Callback{ private Integer deviceRotation = Integer.MAX_VALUE; public VideoCaptureAndroid(Context…
ZijingWu
  • 3,350
  • 3
  • 25
  • 40
0
votes
1 answer

Does calling object.notifyAll() cause lock rebiasing/inflation in Hotspot JVM?

When I call object.notifyAll() on a completely uncontented (possibly biased, if this is allowed for the current JVM) monitor, in particular if no threads are actually waiting on the monitor, does it cause monitor rebiasing and/or inflation?
leventov
  • 14,760
  • 11
  • 69
  • 98
0
votes
3 answers

synchronized keyword does not work as expected in java

Edit: I have already found the answer on the stack: https://stackoverflow.com/a/16280842/3319557 I face a problem with synchronization. I have two following methods: public synchronized void incrementCounter1() { counter++; } public void…
mlecz
  • 985
  • 11
  • 19
0
votes
0 answers

Synchronised JS Timer with PHP

I want to know if some one knows or have idea about making a JS Timer synchronised to all viewing users / clients ? I'm using PHP & Firebase as backends and vanilla JS & also Firebase for client side processing. The problem now is that, if a user /…
Kim Maravilla
  • 51
  • 2
  • 4