多线程——线程的安全问题

本文介绍了Java中解决线程安全问题的两种同步机制:同步代码块和同步方法。同步代码块通过指定同步监视器(锁)确保共享资源在同一时刻仅被一个线程访问,而同步方法则隐式使用this或类本身作为锁。同步虽然保证了数据一致性,但也可能导致效率下降,因为同一时间只有一个线程能执行同步代码。
摘要由CSDN通过智能技术生成

在java中,一般通过同步机制,来解决线程的安全问题

  • 方式一:同步代码块

    • synchronized(同步监视器){
      需要被同步的代码
      }
    • 说明:操作共享数据的代码,即为需要同步的代码
    • 共享数据:多个线程共同操作的变量。如:ticket就是共享数据
    • 同步监视器,俗称:锁。任何一个类的对象都可以充当锁
    • 要求:多个线程必须要共用一把锁
    public class WindowTest1 {
        public static void main(String[] args) {
            Window1 w1 = new Window1();
    
            Thread thread = new Thread(w1);
            Thread thread1 = new Thread(w1);
            Thread thread2 = new Thread(w1);
    
            thread.start();
            thread1.start();
            thread2.start();
        }
    }
    
    class Window1 implements Runnable {
    
        private int ticket = 100;
    
        @Override
        public void run() {
    
            while (true) {
    
                synchronized (this) {
    
                    if (ticket > 0) {
    
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        System.out.println(Thread.currentThread().getName() + ";" + ticket--);
                    } else {
                        break;
                    }
    
                }
    
            }
        }
    }
    
    
    public class WindowTest2 extends Thread {
    
        public static int ticket = 100;
    
        @Override
        public void run() {
        
            //WindowTest2.class:类也是对象
            synchronized (WindowTest2.class) {
                while (true) {
                    if (ticket > 0) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + ";" + ticket--);
                    } else {
                        break;
                    }
    
                }
            }
        }
    
        public static void main(String[] args) {
            WindowTest2 window1 = new WindowTest2();
            WindowTest2 window2 = new WindowTest2();
            WindowTest2 window3 = new WindowTest2();
    
            window1.start();
            window2.start();
            window3.start();
        }
    }
    
    
  • 方式二:同步方法

public class WindowTest3 {
    public static void main(String[] args) {
        Window3 w = new Window3();

        Thread wtest1 = new Thread(w);
        Thread wtest2 = new Thread(w);
        Thread wtest3 = new Thread(w);

        wtest1.start();
        wtest2.start();
        wtest3.start();
    }
}

class Window3 implements Runnable {
    private int ticket = 100;
    Object obj = new Object();

    @Override
    public void run() {

        while (true) {
            show();
            if(ticket <= 0){
                break;
            }
        }


    }

    public synchronized void show() {//同步监视器:this
   			/**
         * 如果是通过继承Thread实现,那么应改为静态方法
         * public static synchronized void show(){}//同步监视器:Window3.class
         */

        if (ticket > 0) {

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + ";" + ticket--);
        }
    }
}

	
  • 总结:

    • 同步方法仍然涉及到同步监视器,只是不需要我们显示的声明
    • 非静态的同步方法,其同步监视器是:this
    • 静态的同步方法,其同步监视器是:当前类本身
  • 优缺点:

    • 好处:同步的方式,解决了线程的安全问题
    • 局限性:操作同步代码时,只能有一个线程参与。相当于是单线程的过程,效率降低
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值