• 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); } }
  • 相关阅读:
    Akka(2):Actor生命周期管理
    Akka(1):Actor
    Akka(0):聊聊对Akka的初步了解和想法
    FunDA:一个开源的函数式数据处理工具库,也是Slick的补充
    FunDA(17)- 示范:异常处理与事后处理
    FunDA(16)- 示范:整合并行运算
    FunDA(15)- 示范:任务并行运算
    FunDA(14)- 示范:并行运算,并行数据库读取
    FunDA(13)- 示范:用户自定义操作函数
    thinkjs中自定义sql语句
  • 原文地址:https://www.cnblogs.com/dybe/p/8039791.html
Copyright © 2020-2023  润新知