“同步”真的只是语法糖吗? [英] Is 'synchronized' really just syntactic sugar?

查看:147
本文介绍了“同步”真的只是语法糖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是多线程的新手,我编写了这段代码,通过同时运行线程增量并打印变量来打印数字1-10000。

I am new to multithreading, and I wrote this code which prints the numbers 1-10000 by having concurrently running threads increment and print a variable.

这是代码我正在使用:

package threadtest;

public class Main{

    static int i=0;
    static Object lock=new Object();

    private static class Incrementer extends Thread{

        @Override
        public void run(){
            while (true){
                synchronized(lock){
                        if (i>=10000)
                            break;
                        i++;
                        System.out.println(i);
                }
            }               
        }
    }


    public static void main(String[] args) {
        new Incrementer().start();
        new Incrementer().start();
        new Incrementer().start();
        new Incrementer().start();
        new Incrementer().start();
        new Incrementer().start();
    }
}

这有效 - 我写了一个测试程序来检查输出,打印的数字按顺序正好是1-10000。

This works - I wrote up a test program to check the output, and the numbers printed are exactly 1-10000 in order.

我的问题是:我听说已同步只是语法糖。但是,如果不使用它,我似乎无法取得成功。我缺少什么?

My question is this: I've heard that synchronized is only syntactic sugar. But I can't seem to achieve a successful result without using it. What am I missing?

推荐答案

synchronized 绝不是语法糖为了任何东西。如果不使用 synchronized 关键字,就无法在Java中使用锁。

synchronized is by no means syntactic sugar for anything. There is no way to work locks in Java without using the synchronized keyword.

哪里有语法糖 Java中锁的一种排序是 synchronized 既可以应用于块(如上所述),也可以应用于整个方法。以下两种方法在语义上大致相同:

Where there is "syntactic sugar" of a sort in locks in Java is that synchronized can apply both to blocks (as you've done it above) and to whole methods. The following two methods are roughly equivalent in semantics:

synchronized void method1() {
  // ... do stuff ...
}

void method2() {
  synchronized(this) {
    // ... do stuff ...
  }
}

那你为什么要做第二个版本而不是第一个?

So why would you want to do the second version instead of the first?


  • 同步方法调用比普通的旧方法调用慢得多,比如大约一个数量级。如果你的同步代码不能保证总是执行(假设它是在条件中),那么你可能不希望同步整个方法。

  • 同步方法持有锁的时间超过同步块(因为所有方法设置/拆除代码)。上面的第二种方法可以保持锁定的时间更短,因为设置和拆除堆栈框架所花费的时间不会被锁定。

  • 你可以更准确地控制你的内容如果你使用同步块,则重新锁定。

  • (由 starblue <提供/ a>)同步块可以使用以外的对象进行锁定,这样可以提供更灵活的锁定语义。

  • Synchronized method invocations are far slower than plain old method invocations, like by about an order of magnitude. If your synchronized code isn't guaranteed to always execute (say it's in a conditional), then you probably don't want to synchronize the whole method.
  • Synchronized methods hold locks for longer than synchronized blocks (because of all the method setup/tear down code). The second method above will hold the lock for less time because the time spent setting up and tearing down the stack frame won't be locked.
  • You can have much finer control over exactly what you're locking if you go with the synchronized blocks.
  • (Courtesy of starblue) Synchronized blocks can use objects other than this for locking which gives you more flexible locking semantics.

这篇关于“同步”真的只是语法糖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆