synchronized 应用于静态方法

本文通过实例对比了synchronized关键字应用于静态方法与非静态方法的区别,详细解析了类锁与对象锁的概念及其在多线程环境中的作用。

LZ就是这种臭脾气,就爱做只是搬运工。来喽,各位看官今天来写(chao)点多线程相关的东西。有钱没钱都捧个人场。

  1. 首先讲讲synchronized 用在静态方法是啥样子滴。
    synchronized关键字用在static方法上是对当前*.java文件对应的Class类进行持锁。来个栗子尝尝味:
//定义service类
package service;

/**
 * Created by xiaopguo on 2017/9/23.
 */
public class Service {
    synchronized public static void printA(){
        try{
            System.out.println("thread name is:"+Thread.currentThread().getName()+ " in "+System.currentTimeMillis() +" get into printA");
            Thread.sleep(5000);
            System.out.println("thread name is:"+Thread.currentThread().getName()+ " in "+System.currentTimeMillis() +" out printA");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    synchronized public static void printB(){
        try{
            System.out.println("thread name is " + Thread.currentThread().getName() +" in " + System.currentTimeMillis() +"get into printB");
            Thread.sleep(5000);
            System.out.println("thread name is "+ Thread.currentThread().getName() + " in " + System.currentTimeMillis() + "out PrintB");
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}
//定义ThreadA类
package srevicethread1;

import service.Service;

/**
 * Created by xiaopguo on 2017/9/23.
 */
public class ThreadA extends Thread{
    private Service service = new Service();
    public ThreadA(Service service){
        super();
        this.service = service;
    }
    @Override
    public void run(){
        service.printA();
    }
}
//定义ThreadB类
package srevicethread1;

import service.Service;

/**
 * Created by xiaopguo on 2017/9/23.
 */
public class ThreadB extends Thread {
    private Service service = new Service();
    public ThreadB(Service service){
        this.service = service;
    }
    @Override
    public void run(){
        service.printB();
    }
}
package test.run;

import service.Service;
import srevicethread1.ThreadA;
import srevicethread1.ThreadB;
import srevicethread1.ThreadC;

/**
 * Created by xiaopguo on 2017/9/23.
 */
public class Run9 {
    public static void main(String[] args){
        Service service = new Service();
        ThreadA thread1 = new ThreadA(service);
        thread1.setName("A");
        thread1.start();

        ThreadB thread2 = new ThreadB(service);
        thread2.setName("B");
        thread2.start();

    }
}

上个结果图:
这里写图片描述

从结果来看,与其他加锁方式没甚没别呀,都实现了同步效果。那你就会问那这与非static方法有什么区别,其实还是有区别的,在static方法上加synchronized方法是给类上锁,而夹在非static方法上是给对象枷锁。

来个例子吧

package service;

/**
 * Created by xiaopguo on 2017/9/23.
 */
public class Service {
    synchronized public static void printA(){
        try{
            System.out.println("thread name is:"+Thread.currentThread().getName()+ " in "+System.currentTimeMillis() +" get into printA");
            Thread.sleep(5000);
            System.out.println("thread name is:"+Thread.currentThread().getName()+ " in "+System.currentTimeMillis() +" out printA");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    synchronized public static void printB(){
        try{
            System.out.println("thread name is " + Thread.currentThread().getName() +" in " + System.currentTimeMillis() +"get into printB");
            Thread.sleep(5000);
            System.out.println("thread name is "+ Thread.currentThread().getName() + " in " + System.currentTimeMillis() + "out PrintB");
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
    //new add
    synchronized public void printC(){
        try{
            System.out.println("thread name is " + Thread.currentThread().getName() + " in " + System.currentTimeMillis() + "get into printC");
            Thread.sleep(5000);
            System.out.println("thread name is " + Thread.currentThread().getName() + " in " + System.currentTimeMillis() + "out printC");

        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

ThreadA、ThreadB借用上面代码。

//new add ThreadC
package srevicethread1;

import service.Service;
/**
 * Created by xiaopguo on 2017/9/23.
 */
public class ThreadC extends Thread {
    private Service service = new Service();
    public ThreadC(Service service){
        this.service = service;
    }
    @Override
    public void run(){
        service.printC();
    }
}

Run9改成:

package test.run;

import service.Service;
import srevicethread1.ThreadA;
import srevicethread1.ThreadB;
import srevicethread1.ThreadC;

/**
 * Created by xiaopguo on 2017/9/23.
 */
public class Run9 {
    public static void main(String[] args){
        Service service = new Service();
        ThreadA thread1 = new ThreadA(service);
        thread1.setName("A");
        thread1.start();

        ThreadB thread2 = new ThreadB(service);
        thread2.setName("B");
        thread2.start();

        ThreadC thread3 = new ThreadC(service);
        thread3.setName("C");
        thread3.start();
    }
}

话不多说直接上图:
这里写图片描述
可以看到printA与printC可以异步执行,而printA与printB必须同步执行。异步原因是printA是Class锁而printB是对象锁。

简单说一下对象锁:
1.synchronized + (函数)function 叫做同步synchronized
2.synchronized(this) 语句块 上述两种方式获得的是当前对象锁
3.synchronized(非this对象) 锁定的是指定对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值