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

volatile + synchronized combination in multithreaded scenario

I have good knowledge of synchronization internals and basic knowledge of volatile internals. I have one query, which is follow-up to below SE question: Why is volatile used in this example of double checked locking I'm confused about one part. If I…
Aditya W
  • 652
  • 8
  • 20
0
votes
1 answer

synchronized with local object

I've just read some code, and notice there is some code with synchronized a local object. Could some one tell me what dose it mean when we do it since we just create a new local object, why should we lock it ? list queue; ... public send() { …
dinhha471
  • 3
  • 1
0
votes
1 answer

How should the synchronization be used in case of strings?

I have a student table in DB which has name, subject and marks fields in it. 1 student can have multiple records for different subjects in this table. I have a student DAO class which has an update method like this: public marks updateStudent(String…
Ishan Rastogi
  • 808
  • 2
  • 8
  • 17
0
votes
1 answer

method and lock issue in thread

here is my thread code Thread decode = new Thread(new Runnable() { @Override public void run() { // System.out.println("decode thread started"); synchronized (queueLock) { while (true) { …
mjdcsy
  • 17
  • 6
0
votes
1 answer

Am I understanding the concept of resource allocation correctly?

I am little confused about how synchronized works in Java for resource allocation. When I am given with the following code in Java: import java.util.concurrent.Semaphore; public class Deadlock { public Deadlock () { Semaphore mutex[] = new…
epseattle
  • 23
  • 5
0
votes
0 answers

Should a method be synchronized if it access a volatile variable?

I have a volatile field in a class, I have a method which modify this field, this method can be invoked by different threads. Must the method be synchronized or not ?
Aleks
  • 111
  • 2
  • 10
0
votes
2 answers

synchronized threads not blocking each other

I'm not trying to fill the board with another of the same question, but I read about 15 solutions and nobody has quite the same issue. Here is the code I am looking at: private AgentModel agent; private UserModel user; private int type; public…
BKreger
  • 89
  • 1
  • 1
  • 11
0
votes
0 answers

Do I need the "synchronized" keyword when i only invoke the "size()" method of a collection

I have a worker thread which uses a "LinkedBlockingQueue" to save the remaining command objects. The thread itself polls command of the queue and other threads can add commands. The calls to "poll" and "add" are wrapped in synchronized methods. Now…
Richard Loth
  • 79
  • 1
  • 7
0
votes
2 answers

Synchronized and threads not working as expected

I am trying to do something like this: There is a class Q which has a field called n and two methods put() and get() which sets the value of n or retrieves the value of n. And then there are two classes Producer and Consumer. Producer class has a…
Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
0
votes
2 answers

Java TCP synchronized method

My result Expected result public void run () { try { handlers.addElement (this); broadcast("Welcome " + name); while(handlers.size() != 2){ if(handlers.size() > 2){ this.out.writeUTF ("The Room is full!"); …
0
votes
1 answer

Public final mutex for Java thread safety

In my project I have to use a class X which offers lots of methods, but the document doesn't mention if these methods are thread-safe, and I don't have the source code either. So I have encapsulated X in an other class with a mutex: public class MyX…
0
votes
1 answer

synchronizing only the assigning of a static variable

I have a static variable which will be fetched a lot. I want to synchronize the initialization, but want it to be lazy loaded. So not making it final. Is this a correct/acceptable approach ? Here is the code. public class Test { private static…
0
votes
0 answers

Java - How to detect deadlocks and recovery from this?

Right now I write a Java program that has as purpose detect deadlocks and recovery from this situation. The program input is two numbers, N = Number of types of resources and M = Number of process. I wanted to do something like this: private static…
0
votes
2 answers

How wait several requests are resolved to send an response in javascript?

I need to do the the same request to external api with differents parameter (node.js and express.js) If all answers to my requests are correct, then I send the angular client code 200, however, if any erroneous send him the error. I try something…
Izaskun Peña
  • 177
  • 2
  • 4
  • 14
0
votes
2 answers

Should instance fields access be synchronized in a Tapestry page or component?

If a page or component class has one instance field which is a non-synchronized object, f.ex. an ArrayList, and the application has code that structurally modifies this field, should the access to this field be synchronized ? F.ex.: public class…
SantiBailors
  • 1,596
  • 3
  • 21
  • 44
1 2 3
99
100