• Java多线程


    多线程
    import java.io.IOException;
    import java.util.Set;
    
    public class ProcessBuilderDemo
    {
    
        public static void main(String[] args)
        { 
            final Person p=new Person();
    
            Thread thread=new Thread()
            {
                boolean flag=false;
                public void run()
                {
                    
                    while (true)
                    {
                        if (flag==false)
                        {
                            p.name="张三";
                            p.sex="女";
                            flag=true;
                        }
                        else 
                        {
                            p.name="李四";
                            p.sex="男";
                            flag=false;
                        }
                    }
                    
                }
            };
            Runnable runnable=new Runnable()
            {
                public void run()
                {
                    while (true)
                    {
                        System.out.println(p.name+""+p.sex);
                    }
                }
            };
            
            thread.start();
            new Thread(runnable).start();
            
        }
        
    }
     class Person
    {
          String name="1";
            String sex="1";
    }

    以上代码线程不是安全的,所以会出现交替打印

    public class ProcessBuilderDemo
    {
    
        public static void main(String[] args)
        {
            final Person p = new Person();
    
            Thread thread = new Thread()
            {
                boolean flag = false;
    
                public void run()
                {
    
                    while (true)
                    {
                        if (flag == false)
                        {
                            
                                p.SetNameAndSex("张三","男");
                                flag = true;
                            
                        }
                        else
                        {
                            
                                p.SetNameAndSex("李四","女");
                                flag = false;
                            
                        }
                    }
    
                }
            };
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    while (true)
                    {
                        p.GetNameAndSex();
                    }
                }
            };
    
            thread.start();
            new Thread(runnable).start();
    
        }
    
    }
    
    class Person
    {
        String name = "1";
        String sex = "1";
        public  void setName(String name)
        {
            this.name=name;
        }
        public String getName()
        {
            return this.name;
        }
        
        public  void setSex(String sex)
        {
            this.sex=sex;
        }
        public String getSex()
        {
            return this.sex;
        }
        public synchronized void SetNameAndSex(String name,String sex)
        {
            this.name=name;
            this.sex=sex;
        }
        public synchronized void GetNameAndSex()
        {
            System.out.println(this.name+"---"+this.sex);
        }
    }
  • 相关阅读:
    HashMap 原理?jdk1.7 与 1.8区别
    内存泄漏与溢出
    NIO
    Mysql 存储引擎
    编写一个 rpc
    dubbo 与 zookeeper
    MongoDB
    natapp 内网穿透服务
    【AHOI 2009】同类分布
    [HNOI 2016] 序列
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3010019.html
Copyright © 2020-2023  润新知