Questions tagged [synchronized-block]
85 questions
2
votes
2 answers
Observer, Observable and Runnable. Why synchronized block lost monitor?
I'm trying to use class as Observer and Observable. This class will be runned as thread. In run() method thread will be waiting and after getting event thread will be notifyed. There is example code:
public class Runner {
public static void…

goblin
- 45
- 3
- 8
2
votes
2 answers
Synchronization with threads
I have a two part question...
I have a class with a function in it that can only be accessed by any one thread at a given time. Making this a synchronized function or a synchronized block still allows for multiple threads since different threads…

Nick
- 6,375
- 5
- 36
- 53
2
votes
3 answers
Should synchronized accessed methods be synchronized?
I want to access a method from within an synchronized block. Here is an example:
public void doSomething() {
// simple stuff
// a block to reduce the synchronized code to
// what really needs to be synchronized.
synchronized(this)…

scheffield
- 6,618
- 2
- 30
- 31
1
vote
1 answer
Kotlin synchronized returning to outer function causes issues
Consider the following code in Kotlin:
private val mLock = "lock"
suspend fun doJob(): String {
synchronized(mLock) {
if (someBoolean1) {
return "A"
}
if (someBoolean2) {
return@synchronized "B"
}
…

Damia Fuentes
- 5,308
- 6
- 33
- 65
1
vote
4 answers
Synchronizing to an object to be instantiated
Are there any synchronizing/reference issues with this code?
(Assume that myStrings is already instantiated.)
MySynch.java:
public class MySynch
{
public static String[] myStrings = new String[10];
public static void updateStrings()
{
…

Doddy
- 1,311
- 1
- 17
- 31
1
vote
0 answers
Using synchronization for audio focus in android
https://developer.android.com/reference/android/media/AudioFocusRequest
This is a link to android developers' documentation on AudioFocusRequest. Following are some code snippets from the code example given in that documentation. I doubt that why…

Pratyush Vatsa
- 36
- 1
- 3
1
vote
2 answers
Why is count1 < count2 after running the following code? Both count1 and count2 should be 2000000
Compile and run in JDK11 and look at the results. sum1 is the time taken by the synchronized code and sum2 is the time for the AtomicInteger code. count1 is the result of counting the number of calls to the synchronized count++. count2 is the same…

Charles Swires
- 11
- 2
1
vote
1 answer
Deadlock situation using threads in java?
I have created 3 classes,
class InCharge - Should check the current Balance, while checking Client thread should wait() until InCharge thread finish the testing(15Secs)
Class Client - Should withdraw money each 5Seconds, but when InCharge Thread…

Itzik.B
- 1,023
- 2
- 15
- 35
1
vote
1 answer
Synchronized Block locked on class
In the below code for producer and consumer, I thought that the produce() and consume() methods are synchronized on Class Lock (Processor.class), but i am getting an exception stating IllegalMonitorStateException, which occurs for objects on which…

Varun
- 13
- 1
- 5
1
vote
1 answer
Working of Java wait method
I have the following code:
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException {
ThreadImpl thr = new ThreadImpl();
thr.start();
Thread.sleep(1000);
synchronized(thr){
…

user9567992
- 51
- 3
1
vote
1 answer
something strange happen with synchronized (Test2.class)
public class Test2 {
static int count;
public static void main(String[] args) {
final Test2 t1 = new Test2();
final Test2 t2 = new Test2();
new Thread(new Runnable() {
@Override
public void…

Clinton John
- 11
- 1
1
vote
1 answer
How can I wake/suspend a particular (Group of) Thread(s) with notify/wait()?
I want to know does for example r.wait() works? With this code:
public class Buffer1 {
private T content;
private boolean empty;
private Object r = new Object();
private Object w = new Object();
public Buffer1() {
empty = true; }
public…

ovoxo
- 11
- 4
1
vote
2 answers
Reentrant Synchronization- Unlocking of called synchronized method
void method1() {
synchronized(this) { // Acquires intrinsic lock
method2();
}
}
void method2() {
synchronized(this) {} // Acquires same lock due to Reentrant synchronization
}
First time lock is acquired in method1 which…

Javed Solkar
- 162
- 11
1
vote
1 answer
Java Synchronized Write Block
im new to java and i have a question regarding Synchronized.
i have the following code for writing to network (simple implementation for now):
public void networkSendData(byte[] data){
try {
out.write(data);
out.flush();
}…

iTEgg
- 8,212
- 20
- 73
- 107
1
vote
3 answers
Am I correct in my assumption about synchronized block?
I have a method shout() with a synchronized block.
private void shout(){
System.out.println("SHOUT " + Thread.currentThread().getName());
synchronized(this){
System.out.println("Synchronized Shout" +…

unj2
- 52,135
- 87
- 247
- 375