在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类了,而非静态方法属于对象本身 那么他监视的对象就是对象本身了
相关推荐
synchronized下的方法控制多线程程序中的线程同步非常方便,这里就来看一下Java使用synchronized修饰方法来同步线程的实例演示,需要的朋友可以参考下
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
《编程思想之多线程与多进程(1)》一文详细讲述了线程、进程的关系及在操作系统中的表现,这是多线程学习必须了解的基础。本文将接着讲一下Java线程同步中的一个重要的概念synchronized. synchronized是Java中的...
60、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现Runnable接口 用synchronized关键字修饰同步方法 反对使用...
多线程,用什么关键字修饰同步方法?stop()和suspend()方法为何不推荐使用? 59.使用socket建立客户端与服务器的通信的过程 60.JAVA语言国际化应用,Locale类,Unicode 61.描述反射机制的作用 62.如何读写一个...
与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而CGI对每个请求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。...
7.9.4 在构造方法中调用构造方法 184 7.10 方法大汇总 185 7.10.1 本例中用到的类 186 7.10.2 使用例程将本章的知识穿起来 189 7.11 小结:多方位理解Java方法 191 7.12 习题 192 第8章 Java中的包(Package)...
7.9.4 在构造方法中调用构造方法 184 7.10 方法大汇总 185 7.10.1 本例中用到的类 186 7.10.2 使用例程将本章的知识穿起来 189 7.11 小结:多方位理解Java方法 191 7.12 习题 192 第8章 Java中的包(Package)...
java8 集合源码分析 [TOC] 0. 项目构建 0.1 版本控制 0.1.1 Git 0.2 ...Java ...Java基础 ...BitSet解决数据重复和是否...关键字(修饰类,方法,静态方法,代码块)] [Java 多线程:Lock 接口(接口方法分析,ReentrantLock,
与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而CGI对每个请求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。...
50、多线程有几种实现方法?同步有几种实现方法? 33 51、启动一个线程是用run()还是start()? . 33 52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念...
50、多线程有几种实现方法?同步有几种实现方法? 51、启动一个线程是用run()还是start()? . 52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 53、线程的基本概念、线程...
50、多线程有几种实现方法?同步有几种实现方法? 33 51、启动一个线程是用run()还是start()? . 33 52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念...
50、多线程有几种实现方法?同步有几种实现方法? 33 51、启动一个线程是用run()还是start()? . 33 52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念...
50、多线程有几种实现方法?同步有几种实现方法? 33 51、启动一个线程是用run()还是start()? . 33 52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念...
70、多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么? 17 71、启动一个线程是用run()还是start()? 17 72、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 18 73...
5.4.4 Java中对象的创建和使用127 5.5 属性130 5.5.1 属性的定义130 5.5.2 变量131 5.6 方法132 5.6.1 方法的定义132 5.6.2 构造方法135 5.6.4 方法重载138 5.6.5 自定义方法138 5.6.6 系统提供方法139 5.6.7 方法...
50、多线程有几种实现方法?同步有几种实现方法? 33 51、启动一个线程是用run()还是start()? . 33 52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念...
44. 多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么? 12 45. 线程的基本概念、线程的基本状态以及状态之间的关系 12 46. 在linux下 怎么查看tomcat的进程? 12 47. 简述逻辑操作(&,|,^)与条件操作(&&,||...