{"id":735,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java Synchronization","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/735.html","title":{"rendered":"Java Synchronization"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java Synchronization<\/h1>\n<\/p><\/div>\n
\n Java
\n Synchronization<\/em>\u662f\u63a7\u5236\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u4efb\u4f55\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u7684\u529f\u80fd\u3002\n <\/div>\n
\n \u5982\u679c\u8981\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u5219Java\u540c\u6b65\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\n <\/div>\n
\n

\u4e3a\u4ec0\u4e48\u4f7f\u7528Synchronization<\/h3>\n
\n \u540c\u6b65\u4e3b\u8981\u7528\u4e8e\n <\/div>\n

\u9632\u6b62\u7ebf\u7a0b\u5e72\u6270\u3002<\/span>
\n \u4e3a\u9632\u6b62\u4e00\u81f4\u6027\u95ee\u9898\u3002<\/span> <\/p>\n


\n

\u540c\u6b65\u7c7b\u578b<\/h3>\n
\n \u6709\u4e24\u79cd\u540c\u6b65\u7c7b\u578b\n <\/div>\n

\u8fdb\u7a0b\u540c\u6b65<\/span>
\n \u7ebf\u7a0b\u540c\u6b65<\/span> <\/p>\n

\n \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u53ea\u8ba8\u8bba\u7ebf\u7a0b\u540c\u6b65\u3002\n <\/div>\n
\n

\u7ebf\u7a0b\u540c\u6b65<\/h3>\n
\n \u6709\u4e24\u79cd\u7c7b\u578b\u7684\u7ebf\u7a0b\u540c\u6b65: \u4e92\u65a5\u548c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u3002\n <\/div>\n

\u4e92\u65a5 \u540c\u6b65\u65b9\u6cd5\u3002<\/span> \u540c\u6b65\u5757\u3002<\/span> \u9759\u6001\u540c\u6b65\u3002<\/span> <\/span>
\n \u5408\u4f5c(java\u4e2d\u7684\u7ebf\u7a0b\u95f4\u901a\u4fe1)<\/span> <\/p>\n


\n

\u4e92\u65a5<\/h3>\n
\n \u4e92\u65a5\u6709\u52a9\u4e8e\u9632\u6b62\u7ebf\u7a0b\u5728\u5171\u4eab\u6570\u636e\u65f6\u76f8\u4e92\u5e72\u6270\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7Java\u4e2d\u7684\u4e09\u79cd\u65b9\u5f0f\u5b8c\u6210:\n <\/div>\n

\u901a\u8fc7\u540c\u6b65\u65b9\u6cd5<\/span>
\n \u901a\u8fc7\u540c\u6b65\u5757<\/span>
\n \u901a\u8fc7\u9759\u6001\u540c\u6b65<\/span> <\/p>\n


\n

Java\u4e2d\u7684\u9501\u6982\u5ff5<\/h3>\n
\n \u540c\u6b65\u662f\u56f4\u7ed5\u79f0\u4e3a\u9501\u6216\u76d1\u89c6\u5668\u7684\u5185\u90e8\u5b9e\u4f53\u5efa\u7acb\u7684\u3002\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e0e\u4e4b\u5173\u8054\u7684\u9501\u3002\u6309\u7167\u60ef\u4f8b\uff0c\u9700\u8981\u5bf9\u5bf9\u8c61\u7684\u5b57\u6bb5\u8fdb\u884c\u4e00\u81f4\u8bbf\u95ee\u7684\u7ebf\u7a0b\u5fc5\u987b\u5728\u8bbf\u95ee\u5bf9\u8c61\u4e4b\u524d\u83b7\u53d6\u5bf9\u8c61\u7684\u9501\uff0c\u7136\u540e\u5728\u5b8c\u6210\u5bf9\u5b83\u4eec\u7684\u9501\u5b9a\u540e\u91ca\u653e\u8be5\u9501\u3002\n <\/div>\n
\n \u4eceJava 5\u5f00\u59cb\uff0c\u5305java.util.concurrent.locks\u5305\u542b\u591a\u4e2a\u9501\u5b9e\u73b0\u3002\n <\/div>\n
\n

\u4e86\u89e3\u6ca1\u6709\u540c\u6b65\u7684\u95ee\u9898<\/h3>\n
\n \u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6ca1\u6709\u540c\u6b65\uff0c\u56e0\u6b64\u8f93\u51fa\u4e0d\u4e00\u81f4\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u793a\u4f8b:\n <\/div>\n
\n
 class <\/span>Table{
    void printTable(int n){
        \/\/method not synchronized <\/span> for(int i=1;
        i<
        =5;
        i++){
            System.out.println<\/span>(n*i);
            try{
                Thread.sleep<\/span>(400);
            }
            catch(Exception e){
                System.out.println<\/span>(e);
            }
        }
    }
}
class <\/span>MyThread1 extends <\/span>Thread{
    Table t;
    MyThread1(Table t){
        this<\/span>.t=t;
    }
    public void <\/span>run(){
        t.printTable<\/span>(5);
    }
}
class <\/span>MyThread2 extends <\/span>Thread{
    Table t;
    MyThread2(Table t){
        this<\/span>.t=t;
    }
    public void <\/span>run(){
        t.printTable<\/span>(100);
    }
}
class <\/span>TestSynchronization1{
    public <\/span>static void <\/span>main(String args[]){
        Table obj = new <\/span>Table();
        \/\/only one objectMyThread1 t1=new MyThread1(obj);
        MyThread2 t2=new MyThread2(obj);
        t1.start<\/span>();
        t2.start<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Output: 5 100 10 200 15 300 20 400 25 500
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

Java\u540c\u6b65\u65b9\u6cd5<\/h3>\n
\n \u5982\u679c\u5c06\u4efb\u4f55\u65b9\u6cd5\u58f0\u660e\u4e3a\u540c\u6b65\u65b9\u6cd5\uff0c\u5219\u79f0\u4e3a\u540c\u6b65\u65b9\u6cd5\u3002\n <\/div>\n
\n \u540c\u6b65\u65b9\u6cd5\u7528\u4e8e\u9501\u5b9a\n <\/div>\n
\n \u5f53\u7ebf\u7a0b\u8c03\u7528\u540c\u6b65\u65b9\u6cd5\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u83b7\u53d6\u8be5\u5bf9\u8c61\u7684\u9501\uff0c\u5e76\u5728\u7ebf\u7a0b\u5b8c\u6210\u5176\u4efb\u52a1\u65f6\u91ca\u653e\u8be5\u9501\u3002\n <\/div>\n
\n
 \/\/example of java synchronized <\/span>methodclass <\/span>Table{
    synchronized void <\/span>printTable(int n){
        \/\/synchronized method for(int i=1;
        i<
        =5;
        i++){
            System.out.println<\/span>(n*i);
            try{
                Thread.sleep<\/span>(400);
            }
            catch(Exception e){
                System.out.println<\/span>(e);
            }
        }
    }
}
class <\/span>MyThread1 extends <\/span>Thread{
    Table t;
    MyThread1(Table t){
        this<\/span>.t=t;
    }
    public void <\/span>run(){
        t.printTable<\/span>(5);
    }
}
class <\/span>MyThread2 extends <\/span>Thread{
    Table t;
    MyThread2(Table t){
        this<\/span>.t=t;
    }
    public void <\/span>run(){
        t.printTable<\/span>(100);
    }
}
public <\/span>class <\/span>TestSynchronization2{
    public <\/span>static void <\/span>main(String args[]){
        Table obj = new <\/span>Table();
        \/\/only one objectMyThread1 t1=new MyThread1(obj);
        MyThread2 t2=new MyThread2(obj);
        t1.start<\/span>();
        t2.start<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Output: 5 10 15 20 25 100 200 300 400 500
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

\u4f7f\u7528\u533f\u540d\u7c7b\u7684\u540c\u6b65\u65b9\u6cd5\u793a\u4f8b<\/h3>\n
\n \u5728\u6b64\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u533f\u540d\u7c7b\u521b\u5efa\u4e86\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u56e0\u6b64\u6240\u9700\u7684\u7f16\u7801\u66f4\u5c11\u3002\n <\/div>\n
\n
 \/\/Program of synchronized <\/span>method by using annonymous classclass <\/span>Table{
    synchronized void <\/span>printTable(int n){
        \/\/synchronized method for(int i=1;
        i<
        =5;
        i++){
            System.out.println<\/span>(n*i);
            try{
                Thread.sleep<\/span>(400);
            }
            catch(Exception e){
                System.out.println<\/span>(e);
            }
        }
    }
}
public <\/span>class <\/span>TestSynchronization3{
    public <\/span>static void <\/span>main(String args[]){
        final <\/span>Table obj = new <\/span>Table();
        \/\/only one objectThread t1=new Thread(){
            public void <\/span>run(){
                obj.printTable<\/span>(5);
            }
        }
        ;
        Thread t2=new Thread(){
            public void <\/span>run(){
                obj.printTable<\/span>(100);
            }
        }
        ;
        t1.start<\/span>();
        t2.start<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Output: 5 10 15 20 25 100 200 300 400 500
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java\u7ebf\u7a0b\u540c\u6b65\u77e5\u8bc6<\/h2>\n

Java \u540c\u6b65\u5757 <\/span>
\n Java \u7ebf\u7a0b\u9759\u6001\u540c\u6b65 <\/span>
\n Java \u7ebf\u7a0b\u6b7b\u9501 <\/span>
\n Java \u7ebf\u7a0b\u95f4\u901a\u4fe1 <\/span>
\n Java \u4e2d\u65ad\u7ebf\u7a0b <\/span>
\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java Synchronizationzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-735","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/735"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=735"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/735\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}