码迷,mamicode.com
首页 > 编程语言 > 详细

线程安全

时间:2019-08-18 11:22:44      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:ant   thread   run   执行   code   demo   产生   exit   get   

单线程相当于一个人做一件事,而多线程程序中,当需要多个线程共享同一个数据时,一个线程对共享的数据进行修改时,在他没有完成相关操作之前不允许其他线程进行相关操作,否则会出现线程安全问题。

package pers.zhb.runnable;

public class MyThread implements Runnable {
    private int tickets = 10;

    public void run() {
        while (true) {
            if (tickets > 0)
                System.out.println(Thread.currentThread().getName()
                        + "售票窗口,售卖了" + tickets-- + "号票");
            else
                System.exit(0);
        }

    }

}
package pers.zhb.runnable;

public class RunnableDemo {
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        Thread thread1 = new Thread(mt);
        Thread thread2 = new Thread(mt);
        Thread thread3 = new Thread(mt);
        Thread thread4 = new Thread(mt);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

    }
}

技术图片

可以看出,10号票在被1号窗口卖出后,又被2号窗口卖了一次,显然这是不符合常理的。产生的原因是这样的:2号线程开始执行,tickets为10,进行了 if (tickets > 0);语句的执行,可以继续往下执行了,但是他没有抢夺到CPU资源,接着是1号线程读取数据10,进行判断可以执行,1号线程执行完后,2号线程依旧没有抢夺到CPU资源,只能继续等待。接着是3号线程、0号线程进行相关操作。当2号线程抢夺到CPU资源时,他的数据还是10,就造成了有重复票的情况。

添加同步语句:

package pers.zhb.runnable;

public class MyThread implements Runnable {
    private int tickets = 10;
    Object lock = new Object();

    public void run() {

        synchronized (lock) {
            while (true) {
                if (tickets > 0)
                    System.out.println(Thread.currentThread().getName()
                            + "售票窗口,售卖了" + tickets-- + "号票");
                else
                    System.exit(0);
            }
        }

    }

}
package pers.zhb.runnable;

public class RunnableDemo {
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        Thread thread1 = new Thread(mt);
        Thread thread2 = new Thread(mt);
        Thread thread3 = new Thread(mt);
        Thread thread4 = new Thread(mt);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

    }
}

技术图片

创建同步方法:

package pers.zhb.runnable;

public class MyThread implements Runnable {
    private int tickets = 10;
    Object lock = new Object();

    public void run() {
        while (true)
            lock();

    }

    public synchronized void lock() {
        while (true) {
            if (tickets > 0)
                System.out.println(Thread.currentThread().getName()
                        + "售票窗口,售卖了" + tickets-- + "号票");
            else
                System.exit(0);
        }
    }
}
package pers.zhb.runnable;

public class RunnableDemo {
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        Thread thread1 = new Thread(mt);
        Thread thread2 = new Thread(mt);
        Thread thread3 = new Thread(mt);
        Thread thread4 = new Thread(mt);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

    }
}

技术图片

 

ReentrantLock (重入锁):

可以对资源重复加锁:

 

package pers.zhb.runnable;

import java.util.concurrent.locks.ReentrantLock;

public class MyThread implements Runnable {
    private int tickets = 10;

    ReentrantLock lock = new ReentrantLock();

    public void run() {

        while (true) {
            lock.lock();
            if (tickets > 0)
                System.out.println(Thread.currentThread().getName()
                        + "售票窗口,售卖了" + tickets-- + "号票");

            else
                System.exit(0);

        }

    }

}

 

package pers.zhb.runnable;

public class RunnableDemo {
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        Thread thread1 = new Thread(mt);
        Thread thread2 = new Thread(mt);
        Thread thread3 = new Thread(mt);
        Thread thread4 = new Thread(mt);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

    }
}

技术图片

 

线程安全

标签:ant   thread   run   执行   code   demo   产生   exit   get   

原文地址:https://www.cnblogs.com/zhai1997/p/11371536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!