Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is synchronized inherited in Java?

I have superclass Point and a synchronized method draw(). Will the subclasses of Point inherit synchronized if I override method draw() in them or I have to always write it?

like image 664
Yoda Avatar asked Apr 14 '13 11:04

Yoda


People also ask

What is the disadvantage of synchronization in Java?

In simple two lines Disadvantage of synchronized methods in Java : Increase the waiting time of the thread. Create performance problem.

How does synchronize work in Java?

A synchronized block in Java is synchronized on some object. All synchronized blocks synchronize on the same object can only have one thread executing inside them at a time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.

What is synchronized data in Java?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.

Can synchronized method be overridden?

Synchronized method Overriding !! Above code compiles ,so answer to above question is “yes,synchronized method can be overriden” .


3 Answers

No, you will always have to write synchronized. If you call the synchronized method of the super class this will of course be a synchronized call. synchronized is not part of the method signature.

See http://gee.cs.oswego.edu/dl/cpj/mechanics.html for detailed description from Doug Lea, Java threading boss (or so).

like image 73
Sebastian Avatar answered Oct 08 '22 20:10

Sebastian


You can check it yourself by writing this:

public class Shape {

    protected int sum = 0;

    public synchronized void add(int x) {
        sum += x;
    }
}


public class Point extends Shape{

    public void add(int x) {
        sum += x;
    }

    public int getSum() {
        return sum;
    }
}

And test class

public class TestShapes {

    public final static int ITERATIONS = 100000;

    public static void main(String[] args) throws InterruptedException {

        final Point p = new Point();

        Thread t1 = new Thread(){
            @Override
            public void run() {

                for(int i=0; i< ITERATIONS; i++){
                    p.add(1);
                }
            }
        };

        Thread t2 = new Thread(){
            @Override
            public void run() {

                for(int i=0; i< ITERATIONS; i++){
                    p.add(1);
                }
            }
        };

        t1.start();
        t2.start();

        t1.join();
        t2.join();


        System.out.println(p.getSum()); // should equal 200000

    }
}

On my machine it was 137099 instead of 200000.

like image 8
rarry Avatar answered Oct 08 '22 19:10

rarry


your Overriden method will no longer be synchronized if you Override it and remove the synchronized. Found it here and here

like image 5
Simulant Avatar answered Oct 08 '22 20:10

Simulant