Java synchronized block unexpected behavior

Java synchronized block unexpected behavior

By : naybnet
Date : November 20 2020, 01:01 AM
it should still fix some issue Quite simple: your a is not shared between any of your Experimenters; it is an instance variable, one per Experimenter. And the volatile is pretty much useless in this situation by the way.
If you want a shared lock, make it a private static final variable. And no need for volatile in this situation either!
code :

Share : facebook icon twitter icon
In a Java synchronized block, are writes visible on all fields or just the synchronized variable?

In a Java synchronized block, are writes visible on all fields or just the synchronized variable?

By : Clay Shwery
Date : March 29 2020, 07:55 AM
With these it helps Yes. Synchronizing on lockObject establishes a Happens Before Relationship (aka sets up a memory barrier). This means that all threads that subsequently get the lock will see any changes that happened while the lock was held previously.
For what it's worth, though, your implementation of lazy initialization is flawed. This is the proper way:
code :
private volatile String cachedToken;

retrieveToken() {
    if (cachedToken == null) {
        synchronized(lockObject) {
            if (cachedToken == null) {
                cachedToken = goGetNewToken();
    return cachedToken
kotlin, why decompiled java code translate the synchronized lock block into synchronized(var1){} + code block?

kotlin, why decompiled java code translate the synchronized lock block into synchronized(var1){} + code block?

By : sushma
Date : March 29 2020, 07:55 AM
Any of those help I believe @tkausl is correct in that your decompiler simply decompiled the code incorrectly. If you inspect the byte code generated by the following:
code :
fun main() {
    val lock = Any()
    synchronized(lock) {
        println("Hello, World!")
Compiled from "Main.kt"
public final class MainKt {
  public static final void main();
       0: new           #4                  // class java/lang/Object
       3: dup
       4: invokespecial #12                 // Method java/lang/Object."<init>":()V
       7: astore_0
       8: iconst_0
       9: istore_1
      10: iconst_0
      11: istore_2
      12: aload_0
      13: monitorenter
      14: nop
      15: iconst_0
      16: istore_3
      17: ldc           #14                 // String Hello, World!
      19: astore        4
      21: iconst_0
      22: istore        5
      24: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      27: aload         4
      29: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      32: getstatic     #32                 // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
      35: astore_2
      36: aload_0
      37: monitorexit
      38: goto          46
      41: astore_2
      42: aload_0
      43: monitorexit
      44: aload_2
      45: athrow
      46: return
    Exception table:
       from    to  target type
          14    36    41   any
          41    42    41   any

  public static void main(java.lang.String[]);
       0: invokestatic  #9                  // Method main:()V
       3: return
what is the behavior of java after returning to a synchronized block after notify on an object

what is the behavior of java after returning to a synchronized block after notify on an object

By : Spadeas
Date : March 29 2020, 07:55 AM
may help you . From the Javadoc of wait() (emphasis added):
Java threading synchronized block behavior - synchronized vs synchronized()?

Java threading synchronized block behavior - synchronized vs synchronized()?

By : Kean Seng
Date : March 29 2020, 07:55 AM
Any of those help synchronized method and synchronized(this) means absolutely the same thing, and uses the same mutex behind. It's more question of taste what notation to prefer.
Personally I prefer synchronized(this), because it explicitly specifies the scope of the mutex lock which could be smaller than the whole method
Fine-tuning Java synchronized block behavior

Fine-tuning Java synchronized block behavior

By : Aishwarya Patwardhan
Date : March 29 2020, 07:55 AM
hope this fix your issue I suggest to look into the ReadWriteLock respectively the implementing class ReentrantReadWriteLock. That thing is espeically designed to allow multiple "reader" threads; but only one "writer" thread.
If i read your question correctly, that is exactly what you are asking for. On the other hand, it might also be wise to step back and eloborate what the real problem is that you are trying to solve here.
Related Posts Related Posts :
  • Pros and Cons of Clojure http client libraries
  • use final inside a for each loop
  • Convert java data object to service object
  • Unsupported major.minor version 51.0 while executing JSP
  • How do I display a word diagonally in Java?
  • making sure one task completes before another starts in java
  • My program gives an index out of bounds error
  • Removing duplicate characters in a String (user inputted keyword)
  • Jersey 2.0 Content-Length not set
  • AWS.SimpleQueueService.NonExistentQueue Exception thrown when Accessing Existing SQS queue
  • Where can I find the source code for the com.sun.jdi package?
  • How can I get path of resource under WEB-INF/class folder (java ee web dynamic project)
  • convert a hexadecimal into a decimal via string convert in java
  • error populating a table using jstl
  • If Singletons are bad then how do you store global state for a framework
  • input system where the user inputs the array position of the object followed by a # to indiacte quantity but it gives me
  • Java: when would I ever want to use static methods when I can use a singleton instead?
  • Extracting RSA public key modulus and exponent stored in unsigned char arrays
  • What does "cannot be resolved to a type" mean, and how can I fix it? Java Android 4.0
  • How can I have a callback on stdout/stderr readiness instead of busy polling?
  • How to report AWT/Swing event queue length programmatically?
  • What is the difference between different for loops in Java?
  • Java arrays effective use/alternative
  • EasyMock chained method call producing null pointer exception
  • Installing Java on Windows 8
  • Java: Is it bad practice to write methods that start with "get" when they aren't getters?
  • Can Iterable.filter() skip processing with "constant" (including short circuited) Predicates?
  • How to save edited JTable data to database?
  • Java Packet Sniffer
  • I have a getText error
  • Reading bytestream and writing to file
  • dat file is not get created by buffered writer
  • Converting a pdf to word document using java
  • Measuring memory use of a piece of code at runtime in Java as an on-going functionality of a service
  • What is Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); in OpenGL ES 20?
  • Opening 2 audio-files and playing them simultaneously
  • Accept a word then print every letter of that word on new line in java?
  • How can I send http request to another servlet
  • Reattaching a detached instance: hibernate lock()
  • How to add similar objects into an array?
  • Changing views within an activity
  • Conditions in while loop
  • Why do we need Set and Map for Java Enum
  • Converting a list of objects to a list of optional objects in Java
  • how threads work in java
  • Importing/Exporting Google Contacts in Java App
  • Finding non duplicate element in an array
  • How to prevent JDialog from popping up in front of the JFrame when I close the JFrame?
  • Program hangs indefinitely after two threads print even off numbers alternatively
  • Does JPA support getting data from Database Views?
  • docx4j traversalutil finds no TextElements in the Paragraph node
  • How to create a ListView with AlertDialog.Builder onItemClickListener?
  • Run code after Spring context has been initialized in web app deployed on Tomcat
  • Gradle project does not build when I add RoboBlender
  • java.lang.VerifyError with Mockito 1.10.17
  • Ivy can not resolve the scope of a dependency which is a dependency of a transitive dependency
  • Cannot assign value to static integer
  • Use case of method overriding in PHP
  • Printing and returning the correct list
  • Inferring parameter types in a generic class which are nested generic types of provided arguments
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org