Can two threads call a synchronized method and normal method at the same time?

You are given a class with synchronized method A, and a normal method C. If you have two threads in one instance of a program, can they call A at the same time? Can they call A and C at the same time?

Solution:
Java provides two ways to achieve synchronization: synchronized method and synchronized statement.

  • Synchronized method: Methods of a class which need to be synchronized are declared with “synchronized” keyword. If one thread is executing a synchronized method, all other threads which want to execute any of the synchronized methods on the same objects get blocked.
    Syntax: method1 and method2 need to be synchronized

    public class SynchronizedMethod {
    	// Variables declaration
    	public synchronized returntype Method1() {
    		// Statements
    	}
    	public synchronized returntype method2() {
    		// Statements
    	}
    	// Other methods
    }	
    
  • Synchronized statement: It provides the synchronization for a group of statements rather than a method as a whole It needs to provide the object on which these synchronized statements will be applied, unlike in a synchronized method
    Syntax: synchronized statements on “this” object

    synchronized(this) {
    	// statement 1
    	// ...
    	// statement N
    }
    

i) If you have two threads in one instance of a program, can they call A at the same time?
Not possible; read the above paragraph.
ii) Can they call A and C at the same time?
Yes. Only methods of the same object which are declared with the keyword synchronized can’t be interleaved

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: