服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java多线程下解决数据安全问题

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

2021-11-21 14:34季沐 Java教程

这篇文章主要介绍了Java多线程下解决数据安全问题,本文使用代码进行讲解,可供大家学习参考

同步代码块

基本语句

synchronized (任意对象) {

操作共享代码

}

代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 返回值类型 方法名(){ }
  • 同步方法的锁对象是this

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

  • 格式: 修饰符 static synchronized 返回值类型 方法名(){ }
  • 同步静态方法的锁对象是 类名.class

代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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方法和语句可获得更广泛的操作

  • void lock()获得锁
  • void unlock()释放

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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();
    }<br type="_moz">

到此这篇关于Java多线程下解决数据安全问题的文章就介绍到这了,更多相关java多线程数据安全内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/testero/p/15135598.html

延伸 · 阅读

精彩推荐