亲宝软件园·资讯

展开

java多线程数据安全 Java多线程下解决数据安全问题

季沐 人气:0
想了解Java多线程下解决数据安全问题的相关内容吗,季沐在本文为您仔细讲解java多线程数据安全的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,多线程数据安全,Java,多线程安全,Java,多线程,下面大家一起来学习吧。

同步代码块

基本语句

synchronized (任意对象) {

操作共享代码

}

代码示例

public class SellTicket implements Runnable {
    private int tickets = 100;
    private Object object = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (object) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
                    tickets--;
                }
            }
        }
    }
    public static void main(String[] args) {
        SellTicket sellTicket = new SellTicket();
        Thread thread1 = new Thread(sellTicket, "窗口1");
        Thread thread2 = new Thread(sellTicket, "窗口2");
        Thread thread3 = new Thread(sellTicket, "窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

优缺点:

同步方法

同步方法:将synchronized关键字加到方法上

同步静态方法,就是把synchronized关键字加到静态方法上

代码示例

public class SellTicket implements Runnable {

//    private int tickets = 100;
    private static int tickets = 100;
    private Object object = new Object();
    private int x = 0;
    @Override
    public void run() {
        while (true) {
            if (x % 2 == 0) {
//                synchronized (object) {
//                synchronized (this) {
                synchronized (SellTicket.class) {
                    if (tickets > 0) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {

                            e.printStackTrace();

                        }                        System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
                        tickets--;
                    }
                }
            } else {
//                synchronized (object) {
//                    if (tickets > 0) {
//                        try {
//                            Thread.sleep(100);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
//                        tickets--;
//                    }
//                }
                sellTicket();
            }
            x++;
        }
    }
//    private void sellTicket(){
//        synchronized (object) {
//            if (tickets > 0) {
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
//                tickets--;
//            }
//        }
//    }
//    private synchronized void sellTicket(){
//            if (tickets > 0) {
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
//                tickets--;
//            }
    private static synchronized void sellTicket(){
        if (tickets > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
            tickets--;
        }
    }
    public static void main(String[] args) {
        SellTicket sellTicket = new SellTicket();
        Thread thread1 = new Thread(sellTicket, "窗口1");
        Thread thread2 = new Thread(sellTicket, "窗口2");
        Thread thread3 = new Thread(sellTicket, "窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

lock锁

lock实现提供比使用synchronized方法和语句可获得更广泛的操作

lock是接口不能直接实例化,采用实现类实例化ReentrantLock

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SellTicket implements Runnable {
    private int tickets = 100;
    private Object object = new Object();
    private Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true) {
           try {
                lock.lock();
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
                    tickets--;
                }
            } finally {
                lock.unlock();
            }
        }
    }
    public static void main(String[] args) {
        SellTicket sellTicket = new SellTicket();
        Thread thread1 = new Thread(sellTicket, "窗口1");
        Thread thread2 = new Thread(sellTicket, "窗口2");
        Thread thread3 = new Thread(sellTicket, "窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }

加载全部内容

相关教程
猜你喜欢
用户评论