zoukankan      html  css  js  c++  java
  • java:多线程的 共享资源冲突问题

    一,java中使用Thread类实现多线程。

    1,如果有两以上的线程同时访问同一个共享资源,可能造成线程冲突,线程冲突会造成数据丢失、重复等严重问题。

    以下通过两个线程同时访问同一个类,来表现线程冲突,如果产生冲突便会打印输出。

    例:

    package Thread;
    
    public class Tested {
    
        String name;
        public static void main(String[] args) {
            Tested b=new Tested();
            Thread a=new Thread() {
                public void run() {
                    try {
                        while(true) {
                        Thread.sleep(2000);
                        b.aa("ls");
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                
            };
            a.start();
            
            Thread e=new Thread() {
                public void run() {
                    try {
                        while(true) {
                        Thread.sleep(2000);
                        b.aa("aas");
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                
            };
            e.start();
        }
        
        public void aa(String name) {
            this.name=name;
            eqrr(name);
        }
        public void eqrr(String name) {
            if(!(name==this.name)) {
                System.out.println(this.name+""+name);
            }
        }
    }

     2,解决方法可以使用synchronized关键字让线程同步。

    例:

    package thinkmore;
    
    import java.util.Scanner;
    
    /*线程的冲突
        在多个线程访问同一个对象的同一个属性时,才会出现线程冲突,线程冲突会造成数据丢失、重复等严重问题。
       为了避免这个问题可以synchronized关键字让线程同步。
    
    
       */
    public class Test {
        String name;
        public static void main(String[] args) {
            final Test obj=new Test();
            final Test obj2=new Test();
            final Thread t=new Thread(){
                @Override
                public  void run(){
                    while (true){
                        try {
                            obj.setName("张三");
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            t.start();
            final Thread t2=new Thread(){
                @Override
                public  void run(){
                    while (true){
                        try {
                            obj2.setName("李四");
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            t2.start();
        }
       /*
    1,放在函数中锁住整个函数,只有前一个线程执行完下一个函数才能访问
    2,放在代码块中,锁住需要共享的资源,推荐使用
    */
    public /*synchronized*/ void setName(String name){ // synchronized(this) { this.name = name; equalsName(name); // } } public void equalsName(String name){ if(name!=this.name) System.out.println(name+" "+this.name); } }
  • 相关阅读:
    22.112.leetcode_path_sum
    21.leetcode111_minimum_depth_of_binary_tree
    20.leetcode110_balanced_binary_tree
    19.leetcode108_convert_sorted_array_to_binary_search_tree
    论文阅读 | RoBERTa: A Robustly Optimized BERT Pretraining Approach
    CheckList:ACL 2020 Best Paper
    激活函数综述
    盘点深度学习中的损失函数
    逻辑回归
    机器学习之参数估计
  • 原文地址:https://www.cnblogs.com/dybe/p/8039791.html
Copyright © 2011-2022 走看看