synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较
synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制。synchronized关键字用于保护共享数据。
synchronized实现同步的机制:synchronized依靠"锁"机制进行多线程同步,"锁"有2种,一种是对象锁,一种是类锁。
1.依靠对象锁锁定
初始化一个对象时,自动有一个对象锁。synchronized {普通方法}依靠对象锁工作,多线程访问synchronized方法,一旦某个进程抢得锁之后,其他的进程只有排队对待。
public class TestSynchronized {
public synchronized void method1() throws InterruptedException {
System.out.println("method1 begin at:" + System.currentTimeMillis());
Thread.sleep(6000);
System.out.println("method1 end at:" + System.currentTimeMillis());
}
public synchronized void method2() throws InterruptedException {
while(true) {
System.out.println("method2 running");
Thread.sleep(200);
}
}
static TestSychronized instance = new TestSychronized();
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
instance.method1();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=1; i<4; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 still alive");
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
instance.method2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread1.start();
thread2.start();
}
}
运行结果:thread2一直等到thread1中的method1执行完了之后才执行method2,说明method1和method2互斥
- 2.synchronized {static方法}此代码块等效于
package com.free4lab.lol;
public class TestSychronized {
public synchronized static void method1() throws InterruptedException {
System.out.println("method1 begin at:" + System.currentTimeMillis());
Thread.sleep(6000);
System.out.println("method1 end at:" + System.currentTimeMillis());
}
public synchronized static void method2() throws InterruptedException {
while(true) {
System.out.println("method2 running");
Thread.sleep(200);
}
}
static TestSychronized instance1 = new TestSychronized();
static TestSychronized instance2 = new TestSychronized();
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
instance1.method1();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=1; i<4; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 still alive");
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
instance2.method2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread1.start();
thread2.start();
}
}
输出效果也是method1和method2互斥
- 3.synchronized {run方法}run方法的锁定.
package com.free4lab.lol;
public class TestSychronized {
static TestSychronized instance = new TestSychronized();
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public synchronized void run() {
for(int i=1; i<4; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 still alive, " + i);
}
}
});
new Thread(thread1).start();
new Thread(thread1).start();
}
}
如果加了synchronized当前线程取完所有数据后,才会释放锁,输出结果是有序的
Thread1 still alive, 1
Thread1 still alive, 2
Thread1 still alive, 3
Thread1 still alive, 1
Thread1 still alive, 2
Thread1 still alive, 3
synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较的更多相关文章
- java synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较
synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制.synchronized关键字用于保护共享数据. synchronized实现同步的机制 ...
- java 类、方法、代码块修饰式关键字总结
super 关键字 this和super的区别 访问成员的区别 this关键字 this特点 this使用场景 static关键字 例子 访问权限修饰符 特点 总结: 四个修饰符的特点 访问权限修饰符 ...
- Java 基础 面向对象之关键字内部类代码块修饰符
final final概念 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承,或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后 ...
- 0040 Java学习笔记-多线程-线程run()方法中的异常
run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...
- 堆、栈、方法区、静态代码块---Java
java 堆.栈.方法区 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm只有一个堆区(heap)被所有线程共享,堆中不存放基 ...
- static方法,属性,代码块初始化顺序和执行顺序
http://greateryang.blog.163.com/blog/static/81953375201232621031508/
- 牛客网Java刷题知识点之同步方法和同步代码块的区别(用synchronized关键字修饰)
不多说,直接上干货! 扩展博客 牛客网Java刷题知识点之多线程同步的实现方法有哪些 为何要使用同步? java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查 ...
- java中静态变量,静态代码块,静态方法,实例变量,匿名代码块等的加载顺序
转自:http://blog.csdn.net/mrzhoug/article/details/51581994 一.在Java中,使用”{}”括起来的代码称为代码块,代码块可以分为以下四种: 1.普 ...
- (转载)synchronized代码块
原文:http://blog.csdn.net/luoweifu/article/details/46613015 作者:luoweifu 转载请标名出处 <编程思想之多线程与多进程(1)——以 ...
随机推荐
- 【Guava】Optional接口来避免空指针错误
null会带来很多问题,从开始有null开始有无数程序栽在null的手里,null的含义是不清晰的,检查null在大多数情况下是不得不做的,而我们又在很多时候忘记了对null做检查,在我们的产品真正投 ...
- ubuntu apache2 .htaccess 下配置 反向代理
安装完apache2后, a2enmod rewrite //启用.htaccess规则 a2enmod proxy a2enmod proxy_http //启用反向代理支持 [P] 配置OK,就可 ...
- 思维导图让 Spring MVC 不再难懂
spring mvc简介与运行原理 Spring的模型-视图-控制器(MVC)框架是围绕一个DispatcherServlet来设计的,这个Servlet会把请求分发给各个处理器,并支持可配置的处理器 ...
- python-Event事件线程同步和互斥
#!/usr/bin/python #coding=utf-8 #用于线程间通信,通过事件标识控制 import threading from time import sleep,ctime def ...
- JavaScript数据结构-11.散列
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- *2.3.4_封装成agent
上一节在验证平台中加入monitor时,读者看到了driver和monitor之间的联系:两者之间的代码高度相似.其本质是因为二者处理的是同一种协议,在同样一套既定的规则下做着不同的事情.由于二者的这 ...
- iOS开源项目周报0323
由OpenDigg 出品的iOS开源项目周报第十三期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. CHIPag ...
- Postgresql 连接更新
update dbo.m_role_fun a set role_code = b.rsc from (select rsc, fun_code from dbo.m_fun) b where a.f ...
- Angular2-三种样式封装策略的区别
Angular2有三种样式封装方式,分别是None.Native.Emulated. 可用元数据“encapsulation”配置,配置方式如下: encapsulation: ViewEncapsu ...
- 再说优化MySQL索引
这几天开发尤其重视数据库索引的优化,是一件好事情,开发特意提过来几个要删除的索引,oh!我的佛陀呀!历史上出现过因为评估不到位,删索引引发故障的案例.那么有什么办法可以评估索引是否合理呢? perco ...