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
1 answer

What is use of synchronized method if we have synchronized block?

If I always call a method from a synchronised block then what is advantage of making any method as synchronized? Thanks in advance.
user1147070
  • 491
  • 7
  • 21
0
votes
0 answers

Quartz Job call synchronized method before completion of first one

Synchronized method called from next batch even though previous batch is not completed its execution in synchronized method. public class FTPClientInvocation implements Job { public FTPClientInvocation() { …
Brijesh
  • 148
  • 2
  • 16
0
votes
2 answers

Synchronization blocks

I'm a newbie to threads and specifically the synchronized key word. I understand that a thread's state changes to BLOCKED if it tried to access a synchronized block where another thread already owns the lock. For reference: synchronized (objA){ …
alwayscurious
  • 1,155
  • 1
  • 8
  • 18
0
votes
2 answers

StringBuffer or synchronized method?

I have following code: public class MyLogger { private StringBuilder logger = new StringBuilder(); public void log(String message, String user) { logger.append(message); logger.append(user); } } The programmer must guarantee…
0
votes
1 answer

Inserting Incrementing value into an ArrayBlockingQueue and thread saftey

I'm aware that the incrementing going on in the below code is not atomic. I'd like the incrementing, insertion into blocking queue and printing value of counter all together be an atomic operation. I'm aware of the atomic int but I'm trying to get…
user836087
  • 2,271
  • 8
  • 23
  • 33
0
votes
2 answers

Lazy initialization of "static" fields should be "synchronized" - How to fix it

I am trying to fix an issue which says Lazy initialization of "static" fields should be "synchronized" suggested by findbug.This link suggests that either I have to make the field variable volatile or make the initialization block synchronized.…
0
votes
0 answers

Synchronized in the method signature vs. synchronized block in part of the method

I'm aware that including the word synchronized to the getNext() method signature would make the method completely thread safe. But what if in the body of the method I surround only the portion where the actual incrementing is being done with a…
user836087
  • 2,271
  • 8
  • 23
  • 33
0
votes
4 answers

How synchronized methods in java guarantees none interference on objects

I wanted to make sure(that i understood correctly) and know how synchronized methods in java guarantees none interference on objects. For example I have this code: private void update_curr_mat(int stock_ind, double price, double time) { …
DeSpeaker
  • 1,159
  • 1
  • 11
  • 11
0
votes
3 answers

How to make all class methods run only on single thread? (synchronized class?)

I know synchronized keyword makes method run only on single class at a time. But here is the problem. I have a database class with methods e.g. insertAccount, updateSetting, etc. If I make insertAccount, updateSetting synchronized, each of them…
Gintas_
  • 4,940
  • 12
  • 44
  • 87
0
votes
3 answers

Can't I remove element from Java shared ArrayList while simultaneously adding into it

Firstly I'm not using iterator here. I'm using 2 threads on an shared ArrayList ,1st is used to adding values into ArrayList and other is to make a temporary copy of it and do some operations on it and then removing all temp elements from original…
Rohan Dodeja
  • 296
  • 1
  • 5
  • 18
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
0
votes
2 answers

problem with synchronized method in java

I have following implementation in Java where I am trying to use a synchronized method: class dbAccess{ public synchronized void getGUID(){ counter=/*Access last count from txn_counter table */ /*Insert a unique value…
jitendra
  • 1,438
  • 2
  • 19
  • 40
0
votes
3 answers

Why part of HandlerThread's run() method is synchronized?

I'm going thru HandlerThread source code and I can't understand why part of HandlerThread's run() method is synchronized? @Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper =…
Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
0
votes
1 answer

Best practice to track websocket connections in Java

Suppose my game server can host 1M users, meaning I need to keep track of 1M websocket connections, what is the suitable data structure (or collection) for doing that? And what is the strategy to handle synchronization issue? synchronize on the…
Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
0
votes
1 answer

Wrapping NSArray manipulations with '@synchronized', still crash

I have following class where I have several methods that manipulate with cachedRequests array MyClass.h @interface MyClass : NSObject { NSMutableArray *cachedRequests; } +(MyClass*) instance; //Singleton MyClass.m @interface MyClass () -…
snaggs
  • 5,543
  • 17
  • 64
  • 127