synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制。synchronized关键字用于保护共享数据。

synchronized实现同步的机制:synchronized依靠"锁"机制进行多线程同步,"锁"有2种,一种是对象锁,一种是类锁

  • 1.依靠对象锁锁定

初始化一个对象时,自动有一个对象锁。synchronized {普通方法}依靠对象锁工作,多线程访问synchronized方法,一旦某个进程抢得锁之后,其他的进程只有排队对待。

synchronized {普通方法}依靠对象锁工作,多线程访问synchronized方法,一旦某个进程抢得锁之后,其他的进程只有排队对待。
 
synchronized void method{}功能上,等效于
void method{
   synchronized(this) {
    ...
   }
}
通过代码看比较清楚:
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互斥

method1 begin at:1381584063557
method1 end at:1381584069557
method2 running
method2 running
Thread1 still alive
method2 running
Thread1 still alive
method2 running
Thread1 still alive
method2 running
method2 running
method2 running
method2 running
method2 running
method2 running
method2 running
method2 running
 
synchronized {修饰代码块}的作用不仅于此,synchronized void method{}整个函数加上synchronized块,效率并不好。在函数内部,可能我们需要同步的只是小部分共享数据,其他数据,可以自由访问,这时候我们可以用 synchronized(表达式){//语句}更加精确的控制
  • 2.synchronized {static方法}此代码块等效于
void method{
   synchronized(Obl.class)
   }
}
使用该类的类对象的锁定去做线程的共享互斥.
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

java synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较的更多相关文章

  1. 牛客网Java刷题知识点之同步方法和同步代码块的区别(用synchronized关键字修饰)

    不多说,直接上干货! 扩展博客 牛客网Java刷题知识点之多线程同步的实现方法有哪些 为何要使用同步?      java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查 ...

  2. Java中this,static,super及finalkeyword和代码块

    this: 能够使用this表示类中的属性------this.name=name 能够使用this强调调用的是本类的方法 能够使用this调用本类的构造方法------this();调用本类中无參构 ...

  3. “全栈2019”Java第四十二章:静态代码块与初始化顺序

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  4. synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较

    synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制.synchronized关键字用于保护共享数据. synchronized实现同步的机制 ...

  5. java 类、方法、代码块修饰式关键字总结

    super 关键字 this和super的区别 访问成员的区别 this关键字 this特点 this使用场景 static关键字 例子 访问权限修饰符 特点 总结: 四个修饰符的特点 访问权限修饰符 ...

  6. Java 基础 面向对象之关键字内部类代码块修饰符

    final final概念 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承,或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后 ...

  7. 0040 Java学习笔记-多线程-线程run()方法中的异常

    run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...

  8. java 为什么wait(),notify(),notifyAll()必须在同步方法/代码块中调用?

    在Java中,所有对象都能够被作为"监视器monitor"——指一个拥有一个独占锁,一个入口队列和一个等待队列的实体entity.所有对象的非同步方法都能够在任意时刻被任意线程调用 ...

  9. 编程开发之--java多线程学习总结(2)同步代码块

    1.第一种解决办法:同步代码块,关键字synchronized package com.lfy.ThreadsSynchronize; /** * 1.使用同步代码块 * 语法: synchroniz ...

随机推荐

  1. ssh-keygen详解

    先来一段google wiki关于ssh key的解释,对应的连接为:https://wiki.archlinux.org/index.php/SSH_keys_(%E7%AE%80%E4%BD%93 ...

  2. 解决osg路径与文件名中的中文字符问题

    转至:http://blog.csdn.net/zhuqinglu/article/details/2064013 在打开或者保存一个osg模型的时候,经常遇到中文路径或者中文文件名的问题,此时会提示 ...

  3. cutpFTP设置步骤

    cutpFTP设置步骤 平常时为了方便两台电脑之间传送数据,我们可以使用cutpftp这个工具实现,而且cutpftp还具有定时传送的功能,非常方便使用.以下是使用该工具的“同步文件夹”功能同步两台电 ...

  4. 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)

    1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  5. Pyqt QSplashScreen启动画面

    多大数应用程序启动时都会在程序完全启动时显示一个启动画面,在程序完全启动后消失.程序启动画面可以显示一些有关产品的信息,让用户在等待程序启动的同时了解有关产品的功能,也是一个宣传的方式.QSplash ...

  6. EChart使用简单介绍

    Echart是百度研发团队开发的一款报表视图JS插件,功能十分强大,使用内容做简单记录:(EChart下载地址 http://echarts.baidu.com/download.html) 1.ti ...

  7. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  8. revert merge会出现的问题

    比如当我们git revert的时候, git revert Git会抱怨: is a merge but no -m option was given 这是因为你revert的那个commit是一个 ...

  9. select * from salgrade for update和select * from salgrade for update nowait区别

    1,select * from salgrade for update session1 session2 SQL> delete salgrade where grade=1; 1 row d ...

  10. 免费电子书:C#代码整洁之道

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:<Clean Code(代码整洁之道)>是一本经典的著作,那么对于编写整洁 ...