j在多线程中java关键字synchronized的修饰静态方法和非静态方法得到的不同情况
程序员文章站
2022-06-06 21:35:05
...
在java的同步关键字synchronized 我们知道用在多线程同步并发情况下,用来在修饰方法或者代码块上
修饰方法 监视的是整个对象 那么如果修饰的分别是静态方法和非静态方法呢 ,效果一样吗?
下面请看我这个例子
package com.test.thread; public class SynchronizedClass { /** * 非静态 synchroized 方法 * @throws InterruptedException */ public synchronized void nonStaticFunction1() throws InterruptedException{ for(int i=0;i<20;i++ ){ Thread.sleep(300); System.out.println("nonstaticFunc1 is running times"+i); } } /** * 非静态 synchroized 方法 * @throws InterruptedException */ public synchronized void nonStaticFunction2() throws InterruptedException{ for(int i=0;i<20;i++ ){ Thread.sleep(300); System.out.println("nonstaticFunc2 is running times"+i); } } /** * 静态 synchroized 方法 * @throws InterruptedException */ public static synchronized void staticFunction() throws InterruptedException{ for(int i=0;i<20;i++ ){ Thread.sleep(300); System.out.println("staticFunc2 is running times"+i); } } }
下面是测试代码
package com.test.thread; public class SynchronizedTest { public static void main(String[] args) throws InterruptedException { final SynchronizedClass cls= new SynchronizedClass(); Thread t1=new Thread(){ public void run() { try { cls.nonStaticFunction1(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }; Thread t2=new Thread(){ public void run() { try { cls.nonStaticFunction2(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }; Thread t3=new Thread(){ public void run() { try { cls.staticFunction(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }; t1.start(); t2.start(); t3.start(); } }
运行结果 如下
nonstaticFunc1 is running times0 staticFunc2 is running times0 staticFunc2 is running times1 nonstaticFunc1 is running times1 staticFunc2 is running times2 nonstaticFunc1 is running times2 staticFunc2 is running times3 nonstaticFunc1 is running times3 staticFunc2 is running times4 nonstaticFunc1 is running times4 staticFunc2 is running times5 nonstaticFunc1 is running times5 staticFunc2 is running times6 nonstaticFunc1 is running times6 staticFunc2 is running times7 nonstaticFunc1 is running times7 staticFunc2 is running times8 nonstaticFunc1 is running times8 staticFunc2 is running times9 nonstaticFunc1 is running times9 staticFunc2 is running times10 nonstaticFunc1 is running times10 staticFunc2 is running times11 nonstaticFunc1 is running times11 staticFunc2 is running times12 nonstaticFunc1 is running times12 staticFunc2 is running times13 nonstaticFunc1 is running times13 staticFunc2 is running times14 nonstaticFunc1 is running times14 staticFunc2 is running times15 nonstaticFunc1 is running times15 staticFunc2 is running times16 nonstaticFunc1 is running times16 staticFunc2 is running times17 nonstaticFunc1 is running times17 staticFunc2 is running times18 nonstaticFunc1 is running times18 staticFunc2 is running times19 nonstaticFunc1 is running times19 nonstaticFunc2 is running times0 nonstaticFunc2 is running times1 nonstaticFunc2 is running times2 nonstaticFunc2 is running times3 nonstaticFunc2 is running times4 nonstaticFunc2 is running times5 nonstaticFunc2 is running times6 nonstaticFunc2 is running times7 nonstaticFunc2 is running times8 nonstaticFunc2 is running times9 nonstaticFunc2 is running times10 nonstaticFunc2 is running times11 nonstaticFunc2 is running times12 nonstaticFunc2 is running times13 nonstaticFunc2 is running times14 nonstaticFunc2 is running times15 nonstaticFunc2 is running times16 nonstaticFunc2 is running times17 nonstaticFunc2 is running times18 nonstaticFunc2 is running times19
由上述运行结果可知 静态方法staticFunc2 和 非静态方法nonstaticFunc1 交替执行,而非静态方法 nonstaticFunc1 和nonstaticFunc2两方法则是有先后顺序执行的 说明
静态方法和非静态方法监视的对象不一样,我们知道静态方法属于类对象 那么它监视的对象就是Class类了,而非静态方法属于对象本身 那么他监视的对象就是对象本身了
上一篇: 不可思议的掌纹诊病
下一篇: 亚马逊推出云计算认证项目