简体   繁体   中英

Impact of one synchronized from another synchronized method in multi-thread environment?

I am working with the synchronization threads where I have three synchronized method and each method will access by individual thread (Total 3 threads in current program )

In our program we are calling one syn method is calling another sync method, below is sample code which is inspired from real application :

public class ThreadTest {

    public static synchronized void suncMessage() {
        System.out.print("1");
    }

    public static synchronized void suncMessage2() {
        suncMessage();
        System.out.print("2");
    }

    public static synchronized void suncMessage3(String s) {
        System.out.print("3m" + s);
    }

    public static void main(String... at) throws InterruptedException {
        Thread t1 = new Thread() {

            public void run() {
                for (int i = 0; i <= 2; i++) {
                    suncMessage();
                }
            }
        };

        Thread t2 = new Thread() {

            public void run() {
                for (int i = 0; i <= 2; i++) {
                    suncMessage2();
                }
            }
        };

        Thread t3 = new Thread() {

            public void run() {
                for (int i = 0; i <= 2; i++) {
                    suncMessage3("3");
                }
            }
        };

        t1.start();
        t2.start();
        t3.start();
    }
}

So my question is, what is impact of calling one synch. method from another synch.?? Is it good practices and how it will impact the complexity of program?

Nice question, but you have to try hard when you are working on this type of scenarios and the performance may effect. Because synchronization is approx 50 time slower than normal method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM