北京建设网站哪里好,元宇宙app技术开发,做网站可以赚多少钱,网站上加一个浮动小框怎么做Java 中synchronize函数的实例详解java中的一个类的成员函数若用synchronized来修饰#xff0c;则对应同一个对象#xff0c;多个线程像调用这个对象的这个同步函数时必须等到上一个线程调用完才能由下一个线程调用。那么如果一个类同时有两个成员函数是由synchronized修饰如…Java 中synchronize函数的实例详解java中的一个类的成员函数若用synchronized来修饰则对应同一个对象多个线程像调用这个对象的这个同步函数时必须等到上一个线程调用完才能由下一个线程调用。那么如果一个类同时有两个成员函数是由synchronized修饰如代码所示对与同一个对象是否可以在两个线程运行时一个调用funcA同时另一个调用funcBMysyn是这样一个类如果我有两个线程一个在run方法中先运行funcA再运行funcB另一个线程在run方法中先运行funcB再运行funcA。那有没有可能出现这样的情况在输出时start A...后面直接输出start B...public class MySyn {public synchronized void funcA(String str){System.out.println(str:);System.out.println(start A...);try {Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(...A end);}public synchronized void funcB(String str){System.out.println(str:);System.out.println(start B...);try {Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(...B end);}}测试代码如下这个线程是先运行funcA的public class Mythread implements Runnable {private MySyn mysyn;private String id;public Mythread(MySyn syn, String id){this.mysyn syn;this.id id;}Overridepublic void run() {this.mysyn.funcA(id);try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.mysyn.funcB(id);}public static void main(String arg[]){MySyn synnew MySyn();Thread t1 new Thread(new Mythread(syn, t1));Thread t2 new Thread(new YourThread(syn, t2));t1.start();t2.start();}}这个线程是先运行funcB的public class YourThread implements Runnable {private MySyn mysyn;private String id;public YourThread(MySyn syn, String id){this.mysyn syn;this.idid;}Overridepublic void run() {this.mysyn.funcB(id);this.mysyn.funcA(id);}}输出结果多是t1:start A......A endt2:start B......B endt2:start A......A endt1:start B......B end如果取消Mythread的run方法中两个函数调用间的sleep那结果多是t1:start A......A endt1:start B......B endt2:start B......B endt2:start A......A end个人结果可能因线程调度不同但是永远不会有start A...后面直接输出start B...那如果funcB不是一个同步函数那上述代码运行结果会是怎么样呢代码稍加改动把funcB的synchronized关键字去掉。运行结果为t2:t1:start A...start B......A endt1:start B......B endt2:start A......B end...A end显然出现了start A...后面直接输出start B...的结果。同样如果Mysyn类如果有一个public 的成员变量多线程也可以再同步函数被调用的同时由另一个线程修改这个成员变量。上述实验说明了同步的成员函数只能在同一个对象的同步函数调用中对其他同步函数(包括本身)有排它的效果即多线程运行中同一个对象当前只能有一个同步函数在运行但不排除其他非同步函数的运行或对成员进行访问。那现在假设某个类有两个静态同步方法那情况怎么样呢具体实现我就不重复了因为结果类似在多线程中同一个类当前只能有一个类同步函数(静态同步函数)在运行但不排除其他非同步静态函数的运行或对静态成员的访问如有疑问请留言或者到本站社区交流讨论感谢阅读希望能帮助到大家谢谢大家对本站的支持