publicclassDemo1{publicstaticvoidmain(String[] args){//演示读读共享
Service service =newService();
Thread a =newThread("A"){@Overridepublicvoidrun(){
service.read();}};
Thread b =newThread("B"){@Overridepublicvoidrun(){
service.read();}};
a.start();
b.start();}}
publicclassDemo2{publicstaticvoidmain(String[] args){//演示写写互斥
Service service =newService();
Thread a =newThread("A"){@Overridepublicvoidrun(){
service.write();}};
Thread b =newThread("B"){@Overridepublicvoidrun(){
service.write();}};
a.start();
b.start();}}
publicclassDemo3{publicstaticvoidmain(String[] args){//演示读写互斥
Service service =newService();
Thread a =newThread("A"){@Overridepublicvoidrun(){
service.read();}};
Thread b =newThread("B"){@Overridepublicvoidrun(){
service.write();}};
a.start();
b.start();}}
写读互斥
运行类
publicclassDemo4{publicstaticvoidmain(String[] args){//演示写写互斥
Service service =newService();
Thread a =newThread("A"){@Overridepublicvoidrun(){
service.write();}};
Thread b =newThread("B"){@Overridepublicvoidrun(){
service.read();}};
a.start();
b.start();}}