Synchronized的用法

Java中Synchronized的用法:

synconized:举个例子就能知道什么意思

                             火车站买票程序,比如说有10张票开了4个窗口(分别为A,B,C,D)同时买票,应该是A卖出一张剩9张,B卖出一张剩8张,张数应该没卖出一张减一,直到为0,但是如果不适用synconized,会出现什么情况呢?不使用synconized关键字,A卖出一张剩9张,B卖出一张有可能还剩9张,但是这不是我们需要的,接下来就是我们来了解synconized啦!

1.同步方法

public class NewRunnable implements Runnable{
    int a=10;
    public void run() {
        method();
    }
    public synchronized void method(){
        while (a>=0) {
            System.out.println(Thread.currentThread().getName()+"=="+a);
            a--;
        }
    }

}

调用方法:(1):卖票程序正常

public class MianTest {
    public static void main(String[] args) {
        NewRunnable newRunnable=new NewRunnable();
        Thread thread1=new Thread(newRunnable,"thread1");
        Thread thread2=new Thread(newRunnable,"thread2");
        thread1.start();
        thread2.start();
    }
}

                  (2):卖票程序不正常

public class MianTest {
    public static void main(String[] args) {    
        Thread thread1=new Thread(new NewRunnable(),"thread1");
        Thread thread2=new Thread(new NewRunnable(),"thread2");
        thread1.start();
        thread2.start();
    }
}

总结:必须使用同一个NewRunnable对象

2同步代码块

public class NewRunnable implements Runnable{
    int a=10;
    public void run() {
        synchronized (this) {
            while (a>=0) {
                System.out.println(Thread.currentThread().getName()+"=="+a);
                a--;
            }
        }
    }
}

调用方法:(1):卖票程序正常

public class MianTest {
    public static void main(String[] args) {
        NewRunnable newRunnable=new NewRunnable();
        Thread thread1=new Thread(newRunnable,"thread1");
        Thread thread2=new Thread(newRunnable,"thread2");
        thread1.start();
        thread2.start();
    }
}

                  (2):卖票程序不正常

public class MianTest {
    public static void main(String[] args) {    
        Thread thread1=new Thread(new NewRunnable(),"thread1");
        Thread thread2=new Thread(new NewRunnable(),"thread2");
        thread1.start();
        thread2.start();
    }
}

总结:必须使用同一个NewRunnable对象

3.静态同步方法

public class NewRunnable implements Runnable{
    static int a=10;
    public void run() {
        method();
    }
    public static synchronized void method(){
        while (a>=0) {
            System.out.println(Thread.currentThread().getName()+"=="+a);
            a--;
        }
    }
}

调用方法:(1):卖票程序正常

public class MianTest {
    public static void main(String[] args) {
        NewRunnable newRunnable=new NewRunnable();
        Thread thread1=new Thread(newRunnable,"thread1");
        Thread thread2=new Thread(newRunnable,"thread2");
        thread1.start();
        thread2.start();
    }
}

                  (2):卖票程序正常

public class MianTest {
    public static void main(String[] args) {    
        Thread thread1=new Thread(new NewRunnable(),"thread1");
        Thread thread2=new Thread(new NewRunnable(),"thread2");
        thread1.start();
        thread2.start();
    }
}

总结:静态使用时都可以,因为静态方法是属于类而不是对象。类直接调用静态方法而不是利用类的对象调用方法。

4.synchronized同步类

public class NewRunnable implements Runnable{
    static int a=10;
    public void run() {
        synchronized (NewRunnable.this) {
            while (a>=0) {
                System.out.println(Thread.currentThread().getName()+"=="+a);
                a--;
            }
        }
    }

}

调用:

(1)不正常显示

public class MianTest {
    public static void main(String[] args) {
        Thread thread1=new Thread(new NewRunnable(),"thread1");
        Thread thread2=new Thread(new NewRunnable(),"thread2");
        thread1.start();
        thread2.start();
    }
}

(2)正常显示

public class MianTest {
    public static void main(String[] args) {
        NewRunnable newRunnable=new NewRunnable();
       Thread thread1=new Thread(newRunnable,"thread1");
       Thread thread2=new Thread(newRunnable,"thread2");
       thread1.start();
       thread2.start();
    }
}

总结:同步类时,必须使用同一个对象。



总结:对象调用时,必须使用同一个对象;当同步方法是静态方法时,可以用类直接调用。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值