首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:同步方法如何与同步静态方法同步?

Java:同步方法如何与同步静态方法同步?
EN

Stack Overflow用户
提问于 2013-12-20 15:11:38
回答 1查看 83关注 0票数 0

代码片段:

代码语言:javascript
复制
public class SyncTest {

public static void main(String[] args) {
    new SyncTest().test();
}

private void test() {
    final Outputer outputer = new Outputer();

    // Thread 1
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                outputer.outPut("一二三四五六七八九");
            }
        }
    }).start();

    // Thread2
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Outputer.outPut2("123456789");  
            }
        }
    }).start();
}

static class Outputer {
    public void outPut( String name) {          
        int len = name.length();
        synchronized (Outputer.this) {                          // lock is Outputer.class
            for (int i = 0; i < len; i++) {
                System.out.print(name.charAt(i));
            }
            System.out.println();
        }
    }
    public static synchronized void outPut3( String name) {     // lock also is Outputer.class
        int len = name.length();
        for (int i = 0; i < len; i++) {
            System.out.print(name.charAt(i));
        }
        System.out.println();
    }
}

}

输出

123456789 1一2二三四五六七八九3456789

显然没有同步,请帮忙,谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-20 15:23:35

您需要指定类实例而不是this,因此两者都使用相同的Monitor对象

代码语言:javascript
复制
static class Outputer {
    public void outPut( String name) {          
        int len = name.length();
        synchronized (Outputer.class) {      // Outputer.this is not the same as Outputer.class
            for (int i = 0; i < len; i++) {
                System.out.print(name.charAt(i));
            }
            System.out.println();
        }
    }
}

或者,您可以使用一个单独的监视器对象,因此这是显式的,它将被使用:

代码语言:javascript
复制
static class Outputer {
    private static Object syncronisationMonitor = new Object();

    // nonstatic method
    public void outPut( String name) {          
        synchronized (syncronisationMonitor ) { // we use the same monitor as in the static method                        
           [...]
        }
    }

    //static method
    public static void outPut3( String name) {    
        synchronized (syncronisationMonitor ) {  // we use the same monitor as in the non-static method                                               
            [...]
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20706599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档