logo
down
shadow

java - calling synchronized method from inside synchronized while waiting


java - calling synchronized method from inside synchronized while waiting

By : Ritwik Singha
Date : November 19 2020, 07:01 PM
hope this fix your issue Java's mutexes are recursive, so you can invoke a synchronized method recursively or invoke another synchronized method for which you already hold a lock. You will need to tell us what it is you are specifically trying to do, though.... lots of stuff with thread locking/synchronization, if done incorrectly, can lead to deadlock, and it isn't entirely clear from your question what you are attempting to do.
code :


Share : facebook icon twitter icon
Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java


By : Clélio Henrique Ribe
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Probably not: new Thread().start() returns immediately, so by the time the new thread actually gets started and methodB() is called, it is very likely that the original thread will have already exited methodA().
If however you do:
code :
public synchronized void methodA() throws InterruptedException {
  new Thread() {
    public void run() {
      methodB();
    }
  }.start();
  Thread.sleep(10000);
}
Calling a synchronized from inside a non-synchronized method

Calling a synchronized from inside a non-synchronized method


By : Hiten Soni
Date : March 29 2020, 07:55 AM
around this issue OK, here's what synchronized does: It prevents two or more threads from synchronizing on the same object at the same time. It does not do anything else. It does not prevent two threads from entering the same synchronized method at the same time (the threads could be calling the same method on different instances). Synchronizing on an object does not prevent other threads from modifying that object. (The other threads might be in un-synchronized methods).
If you want to prevent other threads from printing messages between the two lines that printStatus() prints, then it is not enough to only synchronize printStatus(). You must synchronize every thread that can use System.out. Here's how I would do it:
code :
private void printStatus() {
    synchronized (System.out) {
        System.out.println("\t\t\t" + Thread.currentThread().getName());
        System.out.println("\t\t\tCookies: " + contents);
    }
}

public void putCookie(int amount) {  
    printStatus();
    contents += amount;
    synchronized (System.out) {
        System.out.println(amount + " cookies were Put");
    }
}

...
private static Object consoleLock = new Object();

...
    synchronized (consoleLock) {
        System.out.println(...);
        ...
    }
...
Java synchronized method calling not synchronized method

Java synchronized method calling not synchronized method


By : Han van Beusekom
Date : March 29 2020, 07:55 AM
it helps some times I have code like this: ,
is it still necessary to make doSomethingElse synchronized?
Waiting for semaphore inside synchronized method

Waiting for semaphore inside synchronized method


By : Sanchay Gupta
Date : March 29 2020, 07:55 AM
this one helps. No it won't: other Threads will block on the same TaskExecutor instance because of the synchronized.
Since you have a static Semaphore, synchronized is unnecessary since your semaphore ensure a stronger constraint: threads are blocking not only on the same TaskExecutor but globally (as long as the semaphore is acquired/released equivalently to the synchronized)
Confusing Java synchronized method, synchronized(this), and synchronized class

Confusing Java synchronized method, synchronized(this), and synchronized class


By : anand m
Date : March 29 2020, 07:55 AM
I hope this helps you . I'm very confused because of Java's synchronized concept. ,
As far as I know, foo and bar methods work same.
Related Posts Related Posts :
  • doubt on private members and Methods
  • How do I check if output stream of a socket is closed?
  • Is there a clean cut way of adding NOP records to an AFP file using Java?
  • Move multiple BufferedImage in Java2D?
  • how to learn Drools or another rule engine fast
  • Date Sorting - Latest to Oldest
  • unable to add objects to saved collection in GAE using JDO
  • Spring, Hibernate, Blob lazy loading
  • Including Applet in JSP page
  • Original object is also changed when values of cloned object are changed
  • Drag and drop + custom drawing in Android
  • Creating a time series with jfreechart
  • Hibernate Criteria: Perform JOIN in Subquery/DetachedCriteria
  • How to check if my string is equal to null?
  • Connect two client sockets
  • How to reference a .dll from a .jar file
  • Invoke web-services in JSF
  • servlet connection to DB
  • Can I un-assign (clear) all fields of an instance?
  • Java parsing XML document gives "Content not allowed in prolog." error
  • Deploy Play! application as executable jar
  • Castor: how to map a simple list of strings?
  • Hibernate Transient Extends problem
  • Posting a JSON array to webservice in Android
  • How to search for duplicate values in a huge text file having around Half Million records
  • Easy way to get a test file into JUnit
  • cast across classloader?
  • Image manipulation
  • Java - JPA - Generators - @SequenceGenerator
  • What's a good Java-based Master-Slave communication mechanism?
  • Design Help/Suggestion required
  • NullPointerException in com.sun.tools.jxc.SchemaGenTask
  • time required to finish the multithreaded program?
  • Traditional IO vs memory-mapped
  • Using StringBuilder to process csv files to save heap space
  • Response Time is different for mulitiple execution of the application with the same request Performance problem
  • Java - Weblogic - JMX : request available free memory on all servers
  • Error: Can't find common super class of
  • JNI problem when calling a native library that loads another native library
  • calling Stanford POS Tagger maxentTagger from java program
  • Notification framework for java
  • hide keyboard in android?
  • Alternative of JPA
  • How to get Java XP Look and Feel
  • Remote JMS connection still using localhost
  • Java NoClassDefFoundError
  • Is O(N!*N) an acceptable big oh complexity class or do I remove the constant and just say O(N!)?
  • Java File URI error?
  • Using an interface as a constructor parameter in Java?
  • Add additional path to exec-maven-plugin
  • Natural Language parsing of an appointment?
  • Java: How to tell which fontconfig file my system is currently using?
  • How to create notification icon badge on Android apps (like iPhone)
  • Interfaces with hibernate annotations
  • Getting ConnectionTimeoutException with the host did not accept the connection within timeout
  • How to get all property names of a Groovy class?
  • Java Netscape LDAP Remove One Attribute
  • A simple two column layout with Swing
  • Making a Linked list using Generics
  • how to handle result set data
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk