Syncronized用法

syncronized是非公平锁,可重入锁
用法:
1.锁共享对象
2.静态同步方法:(其实锁的就是类对象)
3.同步代码块,锁类对象:
4.同步方法:(其实锁的就是this对象)
5.同步代码块,锁this对象

锁共享对象:

public class TrainThread extends Thread{

    //初始化100张票,要定义为静态
    private static int ticket  = 100;
    //定义了一个共享的对象,要定义为静态
    private static Object obj = new Object();

    public void run(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (obj){
            while(ticket > 0){
                System.out.println(Thread.currentThread().getName()+"卖出了第"+ (100  -ticket + 1)+"张票");
                ticket --;
            }
        }

    }

    public static void main(String[] args) {
        TrainThread thread1 = new TrainThread();
        TrainThread thread2 = new TrainThread();
        thread1.start();
        thread2.start();
    }

}

静态同步函数:(其实锁的就是类对象)

public class TrainThread2 extends Thread{

    //初始化100张票,要定义为静态
    private static int ticket  = 100;

    public void run(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        test();
    }

    //静态同步函数
    private static synchronized void test() {
        while(ticket > 0){
            System.out.println(Thread.currentThread().getName()+"卖出了第"+ (100  -ticket + 1)+"张票");
            ticket --;
        }
    }

    public static void main(String[] args) {
        TrainThread2 thread1 = new TrainThread2();
        TrainThread2 thread2 = new TrainThread2();
        thread1.start();
        thread2.start();
    }

}

锁类对象:

public class TrainThread3 extends Thread{

    //初始化100张票,要定义为静态
    private static int ticket  = 100;

    public void run(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //锁类对象
        synchronized (TrainThread3.class){
            while(ticket > 0){
                System.out.println(Thread.currentThread().getName()+"卖出了第"+ (100  -ticket + 1)+"张票");
                ticket --;
            }
        }
    }

    public static void main(String[] args) {
        TrainThread3 thread1 = new TrainThread3();
        TrainThread3 thread2 = new TrainThread3();
        thread1.start();
        thread2.start();
    }

}

同步函数:(其实锁的就是this对象)

public class TrainRunnable2 implements Runnable{

    //初始化100张票
    private int ticket  = 100;

    public void run(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       test();

    }
    //同步函数
    private  synchronized void test() {
        while(ticket > 0){
            System.out.println(Thread.currentThread().getName()+"卖出了第"+ (100  -ticket + 1)+"张票");
            ticket --;
        }
    }

    public static void main(String[] args) {
        TrainRunnable2 trainRunnable = new TrainRunnable2();
        new Thread(trainRunnable).start();
        new Thread(trainRunnable).start();
    }

}

锁this对象:

public class TrainRunnable implements Runnable{

    //初始化100张票
    private int ticket  = 100;

    public void run(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (this){
            while(ticket > 0){
                System.out.println(Thread.currentThread().getName()+"卖出了第"+ (100  -ticket + 1)+"张票");
                ticket --;
            }
        }

    }

    public static void main(String[] args) {
        TrainRunnable trainRunnable = new TrainRunnable();
        new Thread(trainRunnable).start();
        new Thread(trainRunnable).start();
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 整理来自互联网 1,JDK:Java Development Kit,java的开发和运行环境,java的开发工具...
    Ncompass阅读 1,559评论 0 6
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,422评论 0 4
  • 一:java概述: 1,JDK:Java Development Kit,java的开发和运行环境,java的开发...
    慕容小伟阅读 1,845评论 0 10
  • 在大海边看潮起潮落,任世事怎么变化,我心淡然~。 每天坐在大海边或窗前就那么待着也是好的!每个 人在红尘...
    晚安好萌阅读 190评论 0 1