Questions tagged [synchronized-block]

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

two synchronised blocks at different threads with same object reference still executing simultaneously

public class SynchronizedTest { public static void main(String argv[]) { Thread t1 = new Thread(new Runnable(){public void run() { synchronized (this) //line 7 { for(int i=0; i<100; i++) …
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
1 answer

Behavior of synchronized method and block is different

I observed a scenario where use of synchronized method or synchronized block producing different results. From below code: class Callme { void call(String msg) { System.out.print("[" + msg); try { …
Pradeep
  • 1,057
  • 2
  • 9
  • 17
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
1 answer

synchronized(this) block within the synchronized method

The following is the code snippet from java concurrency in practice book while discussing open calls. The point I'm not getting is that the way setLocation method is declared, it is already synchronized and again calling the synchronized(this) block…
Curious
  • 921
  • 1
  • 9
  • 25
0
votes
1 answer

java synchronized block - locking the whole method

I am not quite sure about how to use a synchronized block correctly. I know that using the synchronized keyword locks the whole class. So that is not want I want. I want to lock the single methods within the class. Here is an example from my class.…
mollwitz
  • 213
  • 3
  • 15
0
votes
1 answer

Creating delay between threads

All, I have an api call which is called by many threads. The only issue is that the delay bet. threads should be at least 1 second. I realized - w/o the synchronized block - if one thread is calling the api at time t1, then all other threads wait…
blueSky
  • 649
  • 5
  • 13
  • 31
0
votes
1 answer

Does the latest JMM specify the synchronized block to be atomic to other threads even asynchronized ones?

As I went through an article about DOUBLE-CHECKED LOCKING on http://www.javaworld.com/article/2074979/java-concurrency/double-checked-locking--clever--but-broken.html , I encounter a comment which says " It should be noted that DCL might, in fact,…
0
votes
2 answers

Calling java timer in a synchronized block of codes

If I have a parent block of codes called A, A is synchronized. And in A, I executes a child block of code called B. Am I right to assume that B will be synchronized also? If in A I have a timer to delay the execution of B in a certain of t time, is…
Xitrum
  • 7,765
  • 26
  • 90
  • 126
0
votes
1 answer

Benefits of using ReentrantLock over synchronized

I find out one more benefit of using ReentrantLock over synchronized Below code shows even if exception occurs in critical section lock is released(Using ReentrantLock ) void someMethod() { //get the lock before processing critical section. …
0
votes
2 answers

Is the following code that retrieves an integer value from some queue implementation correct

I was reading something from the internet. Here is a question about the code below. Is the following code that retrieves an integer value from some queue implementation correct? And the answer is like this: Although the code above uses the queue…
Tech Noob
  • 510
  • 1
  • 9
  • 33
0
votes
2 answers

Synchronizers between two different execution blocks

Message-processing-task should stop processing a NEW message when it detects a client-login-task. However, the message-processor should complete the task it was processing before pausing. This basically means the client-login task should give the…
0
votes
3 answers

What does object reference expression means in synchronized block

Can some one explain me what does object reference expression is meant over herein synchronized block? synchronized (object reference expression) { //code block } public class DeadlockExample { public static void main(String[] args) {…
user3380123
  • 703
  • 1
  • 6
  • 14
0
votes
2 answers

Query about disadvantage of Reentrant locks over synchronized block

I am reading comparison between Reentrant locks and synchronization blocks in java. I am going through the various resources on internet. One disadvantage that I discovered using Reentrant locks over synchronization blocks is that in previous one,…
Vinit89
  • 583
  • 1
  • 7
  • 31