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

confused by synchronized usage

I see the netflix code, in class DynamicPropertyFactory,there's a method like public static DynamicPropertyFactory getInstance() { if (config == null) { synchronized (ConfigurationManager.class) { if (config == null) { …
lawrence
  • 353
  • 2
  • 17
0
votes
1 answer

Synchronization and multithreading

I'm currently working on an Airport app with 2 lanes, but I have a problem with the planes landing. If there is only one lane working everything works fine but if I add a lane, the planes land one at a time, when they should be landing on both the…
GamerGirl
  • 55
  • 8
0
votes
0 answers

Tensorflow synchronous gradient GD getting hung after one epoch on master

I have the following code (below) to perform tensorflow synchronous gradient descent based on their example here: https://github.com/tensorflow/models/blob/master/inception/inception/inception_distributed_train.py Run With The command I'm using for…
Raul Puri
  • 119
  • 1
  • 1
  • 12
0
votes
2 answers

how to Block two threads object accessing 1 method, which is from same class

public class B extends Thread { @Override public void run() { print(); } public synchronized void print(){ int i; for (i=0;i<1000;i++){ System.out.println(Thread.currentThread().getName() ); } …
0
votes
2 answers

java/android - How to guarantee if one thread or method is running, no one else can trigger it?

I'm working on android. Please help give any advice. I have 3 methods(threads). Method_1 { trigger Method_3; ...} Method_2 { trigger Method_3; ...} Method_3 { ... } How can I make sure that Method_3 only can be triggered by Method_1 or…
Ray Sun
  • 25
  • 1
  • 7
0
votes
1 answer

Java SCJP/OCPJP

Given: public class NamedCounter{ private final String name; private int count; public NamedCounter(String name) { this.name = name; } public String getName() { return name; } public void increment() { count++; } public int…
0
votes
1 answer

Block 2 threads accessing one if block in java

I have a while loop in a method. In that while loop I have many if blocks. If I have 2 threads accessing one while loop at the same time how to stop one a unique if block at the same time. Should i have to import any thing? while (true){ if…
0
votes
2 answers

Simplification of synchronized block in Java

I'm having some trouble wrapping my head around the concept of synchronized blocks in Java. I feel I have understood synchronized methods well enough. So I thought of an analogy to help me understand synchronized blocks in terms of synchronized…
user5464136
0
votes
2 answers

Facing issue in trying to synchronize two threads

I was trying to synchronize to two threads, one printing "ping" and another printing "pong", to print something like "ping pong ping pong ping...." I wrote the following program to achieve this, but couldn't get the expected result. I wonder what…
0
votes
2 answers

Volatile variable and synchronized setter and getter

I have thread class that has two variables var1 and var2 that might be accessed from different threads. Can we assume that using var2 via synchronized getter and setter is the same like using volatile var1 without them? public class Processor…
vico
  • 17,051
  • 45
  • 159
  • 315
0
votes
3 answers

Field lock Java

I am using the following code: public class SomeClass extends Thread { ... private double a; private double b; private double increaseVal; public void setIncreaseVal(double increaseVal) { this.increaseVal =…
Mihai
  • 189
  • 2
  • 10
0
votes
1 answer

meet IllegalMonitorStateException when doing multi-thread programming in Java

I wrote a simple program to learn about synchronized block. The program is as follow: public class SychronizedBlock { static int balance = 0; static Integer lock = 0; public static void deposit(int amt) { Thread t1 = new…
Tom
  • 69
  • 2
  • 8
0
votes
1 answer

Synchronized block: lock many objects?

I have many clients Ai (A1,...,An) everyone of which does the action X. And a server B which does the action Y. But Y could only be done only if there is no one executes X. So I think of the mutex lock: every client has an own lock Li. It's no…
Silver
  • 77
  • 10
0
votes
1 answer

Singletone classes injected by dagger with synchronized methods

I use singletone classes, are injected by dagger. But if synchronized methods start running, but they terminates. But if I remove synchronization from methods, they run well. If I inject sungletone classes by dagger, I shouldn't use synchronized…
user3057944
  • 701
  • 1
  • 8
  • 19
0
votes
1 answer

For an immutable type in a mutable reference field, use volatile and cache locally or synchronize?

final class NameVolatile { @Nullable private volatile String name; void setName(String name) { this.name = name } void run() { String name = name; if (name != null) { print(name); } } } final class NameSynchronized…
Eric Cochran
  • 8,414
  • 5
  • 50
  • 91