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

Synchronized in Multithreading

I have three synchronized methods in a class when i try to execute all of them by starting different threads, I am unable to see the synchronized output, no lock is obtained on object & i could see in output all the threads are executed…
GoodToLearn
  • 41
  • 1
  • 5
0
votes
1 answer

Can I use the original list after it has been wrapped by Collections.synchronizedList(...)?

From Javadocs for Collections class: public static List synchronizedList(List list) Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the…
0
votes
4 answers

How can 2 threads access a synchronized block at the same time?

Can you describe how in a multithreaded environment, the below code works incorrectly? I took the code from https://www.javacodegeeks.com/2014/11/multithreading-concurrency-interview-questions-answers.html. The description says 2 threads may enter…
0
votes
1 answer

suspending, resuming and stopping threads in java

I'm learning Thread in java. The following example shows how to suspend, resume and stop threads: class MyNewThread implements Runnable { Thread thrd; boolean suspended; boolean stopped; MyNewThread(String name) { thrd = new Thread(this,…
0
votes
2 answers

Thread order using synchronized

Is there any situation when output will not be A/B/BC/AD (/ is new line)? Is it possible that the second Thread starts before first? public class JavaApplication6 extends Thread{ final StringBuffer sb1 = new StringBuffer(); final StringBuffer sb2 =…
Davidm176
  • 163
  • 1
  • 12
0
votes
1 answer

java:inefficiency of synchronized methods

I was going through this program and could not make much sense of it. class Q { int n; synchronized int get() { System.out.println("Got: " + n); return n; } synchronized void put(int n) { this.n = n; …
0
votes
3 answers

How to do inter thread communication inside static synchronized block

I have a question regarding static synchronized methods and class level locking. Can someone please help me in explaining this example: class Test { synchronized static void printTest(int n) { for (int i = 1; i <= 10; i++) { …
RPR
  • 3
  • 5
0
votes
1 answer

How to implement synchronised Java method

I am new to multithreading and am trying to create a multithreading java booking system. An agent can reserve a seat and has the choice of multiple airlines. This is my Agent class: public class Agent implements Runnable { private int id; private…
sums22
  • 1,793
  • 3
  • 13
  • 25
0
votes
2 answers

If I make class level lock on static method & if one thread execute it so will it block other thread executing other instance method of same class?

Suppose we have one class in which we have one instance method and static method. We have synchronized block in static method with class level lock & we have synchronized block in instance method with object level lock. So suppose when one thread…
Rahul Rabhadiya
  • 430
  • 5
  • 19
0
votes
1 answer

Java synchronized with double if condition

New to Java from C++ and came across this synchronized block example (more context on this page). My question is: Shouldn't the "if" statement also be included in the "synchronized block"? Or else other threads could be executing the same piece of…
FengC
  • 11
  • 3
0
votes
0 answers

Avoid multiple key pressing: choose only one

In my ULC frame, I implemented some F hotkeys (from F1 to F12). But there can be a little bug, for example if you want to press quickly the F10, maybe you press it with F11 also. I would like to avoid it somehow. Because now it will run both actions…
victorio
  • 6,224
  • 24
  • 77
  • 113
0
votes
1 answer

Why does Java's SynchronizedMap use a single mutex and not a ReadWrite lock?

Java's SynchronizedMap, which is created by calling Collections.synchronizedMap() seems to be using a single mutex for all of the map's operations. For instance: ... public V get(Object key) { synchronized (mutex) {return m.get(key);} } public…
Malt
  • 28,965
  • 9
  • 65
  • 105
0
votes
2 answers

Java multithreading - reader and writer thread in synchronized block

This is a question from a job interview: How to identify read thread and write thread in a synchronized block?
prity
  • 1,519
  • 1
  • 12
  • 17
0
votes
2 answers

Why can my synchronized methods lock on different objects?

I am learning Java concurrency right now. I came across a piece of code like this: package pac1; import java.util.*; import java.util.concurrent.*; class A1 { public void f() { synchronized (this) { for (int i = 0; i < 5;…
Lucas
  • 167
  • 1
  • 2
  • 9
0
votes
2 answers

Where is the entry count for an object stored in Java

I understand that the each Java object can be used as a Monitor. A thread can acquire the lock if the entry count associated with the object is zero. And if the same thread acquires the lock, the entry count is incremented thru the "monitorenter"…
UnderWood
  • 803
  • 3
  • 12
  • 23