{"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
\u9632\u6b62\u7ebf\u7a0b\u5e72\u6270\u3002<\/span> \u8fdb\u7a0b\u540c\u6b65<\/span> \u4e92\u65a5 \u540c\u6b65\u65b9\u6cd5\u3002<\/span> \u540c\u6b65\u5757\u3002<\/span> \u9759\u6001\u540c\u6b65\u3002<\/span> <\/span> \u901a\u8fc7\u540c\u6b65\u65b9\u6cd5<\/span> Java \u540c\u6b65\u5757 <\/span>
\n \u4e3a\u9632\u6b62\u4e00\u81f4\u6027\u95ee\u9898\u3002<\/span> <\/p>\n
\n\u540c\u6b65\u7c7b\u578b<\/h3>\n
\n \u7ebf\u7a0b\u540c\u6b65<\/span> <\/p>\n
\n\u7ebf\u7a0b\u540c\u6b65<\/h3>\n
\n \u5408\u4f5c(java\u4e2d\u7684\u7ebf\u7a0b\u95f4\u901a\u4fe1)<\/span> <\/p>\n
\n\u4e92\u65a5<\/h3>\n
\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\u4e86\u89e3\u6ca1\u6709\u540c\u6b65\u7684\u95ee\u9898<\/h3>\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 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
\/\/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 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
\/\/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 Output: 5 10 15 20 25 100 200 300 400 500
<\/span><\/code><\/pre>\n<\/p><\/div>\nJava\u7ebf\u7a0b\u540c\u6b65\u77e5\u8bc6<\/h2>\n
\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}]}}