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
0 answers

Why ConcurrentHashMap use 'synchronized' inside computeIfAbsent

Inside 'computeIfAbsent' method of ConcurrentHashMap there is code: synchronized (r) { if (casTabAt(tab, i, null, r)) { ... Why not 'ReentrantLock' ?
littleAlien
  • 721
  • 2
  • 8
  • 20
0
votes
4 answers

if i have two methods that cannot happen simultaneously in one class on separate threads, how to lock it?

If I have a class that has 2 methods say setA and setB that have synchronized but they are not synchronized on the same object, how do I lock it such that if there were two instances of the class, methods setA and setB cannot happen simultaneously?
dumdum
  • 55
  • 6
0
votes
1 answer

How to temporarily acquire lock for IntentService from Fragment

I have IntentService which I use for uploading photo files to server sequentially. I have Gallery fragment showing files from upload queue and files already uploaded to server. I get list of already uploaded files using api-call to server. Local…
0
votes
3 answers

What is happening behind "Synchronized"?

I got to know that to make the program thread-safe, it is recommended to use synchronized methods or blocks of code. The doubt is how the below code snippet will make the program thread-safe? What is happening behind the scenes? public class…
Aajan
  • 927
  • 1
  • 10
  • 23
0
votes
1 answer

Java synchronization across methods

I am looking for a way to avoid the two methods from running concurrently, but not stopping one (or both) of them for running concurrently on their own. For example, in the code below, I must have method A run concurrently on the same object, but…
Saul
  • 81
  • 1
  • 5
0
votes
1 answer

Synchronized method inside ActionForm

I have a Serializable ActionForm that holds an instance of another Serializable object. This object have a synchronized method which I can't change right now. I want to know if my form object is the same across different requests, because the…
dinhokz
  • 895
  • 15
  • 36
0
votes
1 answer

Access synchronized method from another thread using same instance

I've a core method in my project which I need it to be synchronized in order not to be accessed twice at the same time, and hence I have a thread which uses an instance from this class to access this method, but inside this thread I need to have a…
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
0
votes
1 answer

Creating delay between threads

All, I have an api call which is called by many threads. The only issue is that the delay bet. threads should be at least 1 second. I realized - w/o the synchronized block - if one thread is calling the api at time t1, then all other threads wait…
blueSky
  • 649
  • 5
  • 13
  • 31
0
votes
2 answers

Why the getRGB() method is written the way it did? Is there an alternative way to write getRGB() method?

I'm new to Java. I saw the code below from Java tutorial Oracle. I'm struggling to understand the purpose of this code snippet: public synchronized int getRGB() { return ((red << 16) | (green << 8) | blue); } I understand how…
Thor
  • 9,638
  • 15
  • 62
  • 137
0
votes
1 answer

Can I implement blocking queue with two objects serving as locks in Java?

I am trying to understand Java's synchronized keyword, wait(), and notify() by implementing a blocking queue class. One of the blocking queue implementation is described by this article. However I wonder if it is possible to use two objects serving…
Jack Chen
  • 79
  • 1
  • 7
0
votes
1 answer

Reusing an object without preventing it from being garbage collected

I want to write a class where an instance field holds a large amount of data that is expensive to create. Because the object is time consuming to create I want separate instances to be able to reuse the object if it has already been computed for a…
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
0
votes
1 answer

Threads in spring: synchronized or @Scope("proptery") performance

I have two spring beans. A JmsHandler which receives a Message. This is processed and handed over to a MailHandler which sends a mail. Now I've seen that sometimes messages arrive at the JmsHandler in exact the same time. When entering the sendMail…
meme
  • 81
  • 2
  • 12
0
votes
1 answer

Java : one lock per method per object

I'm building a webcrawler and it has 2 main feature wich are both executed as threads : -The fetcher (crawl a website and separate links from files store both of them into the database). -The downloader (download files based on their url returned by…
naurel
  • 625
  • 4
  • 18
0
votes
1 answer

Values are not updating in thread

i have made a background service which fetches value to database every 40 seconds but here longitude and latitude values are not updating, while in GPSTracker service, the values are updating every 30 second. This Service only fetches the first…
0
votes
1 answer

Why RW lock is slower by 100ms than synchronized in my example

I'd like to show difference in performance RW locks vs synchronized. I've done the lock part, but I got problem with synchronized. The add method is invoked by the Producer threads, the getRandomElement() is invoked by the Consumer threads. I would…
Yoda
  • 17,363
  • 67
  • 204
  • 344
1 2 3
99
100