Synchronized的用法

来源:互联网 发布:python获取当天日期 编辑:程序博客网 时间:2024/09/21 06:33

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
原创粉丝点击