Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:sync方法如何与sync静态方法同步?_Java_Multithreading - Fatal编程技术网

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

Java:sync方法如何与sync静态方法同步?,java,multithreading,Java,Multithreading,代码段: 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

代码段:

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();
    }
}
公共类同步测试{
公共静态void main(字符串[]args){
新建SyncTest().test();
}
专用无效测试(){
最终输出器输出器=新输出器();
//线程1
新线程(newrunnable()){
@凌驾
公开募捐{
while(true){
试一试{
睡眠(50);
}捕捉(中断异常e){
e、 printStackTrace();
}
outputer.outPut(“一二三四五六七八九");
}
}
}).start();
//螺纹2
新线程(newrunnable()){
@凌驾
公开募捐{
while(true){
试一试{
睡眠(50);
}捕捉(中断异常e){
e、 printStackTrace();
}
Outputer.outPut2(“123456789”);
}
}
}).start();
}
静态类输出器{
公共无效输出(字符串名称){
int len=name.length();
已同步(Outputer.this){//锁是Outputer.class
对于(int i=0;i
}

输出

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


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

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

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();
        }
    }
}

synchronized
没有指定顺序。这取决于线程调度程序。您发布的代码对我来说没有任何同步问题。请尝试更多次,并滚动到顶部输出,您会发现问题。我建议另外将
syncronisationMonitor
标记为
final
。没错,我忘了……您请参阅线程处理和同步非常微妙//Outputer。这与Outputer.class不同?对不起,我不太理解
Outputer。这
与当前对象实例的引用
相同。
Outputer.class
是表示类
的类型的对象>输出计算机
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                                               
            [...]
        }
    }
}