zoukankan      html  css  js  c++  java
  • Java 基础【07】线程同步锁的选择

     在需要线程同步的时候如何选择合适的线程锁?

      例:选择可以存入到常量池当中的对象,String对象等

    复制代码
    public class SyncTest
    {
        private String name = "name";
    public void method(String flag)
        {
            synchronized (name)
            {
                System.out.println(flag + ", invoke method ....");
                try
                {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
       
        public static void main(String[] args)
        {
            SyncTest test1 = new SyncTest();
           
            SyncTest test2 = new SyncTest();
           
            MyThread1 myThread1 = new MyThread1();
            MyThread1 myThread2 = new MyThread1();
            myThread1.syncTest = test1;
            myThread2.syncTest = test1;
           
            MyThread1 myThread3 = new MyThread1();
            MyThread1 myThread4 = new MyThread1();
            myThread3.syncTest = test2;
            myThread4.syncTest = test2;
           
            myThread1.start();
            myThread2.start();
            myThread3.start();
            myThread4.start();
           
        }
    
    
    }
    复制代码

     线程类:

    复制代码
    public class MyThread1 extends Thread
    {
        SyncTest syncTest;
       
        @Override
        public void run()
        {
            syncTest.method(this.getName());
        }
    
    }
    复制代码

     本来应该是要实现线程thread1和thread2同步,线程thread3和thread4同步的,但结果呢?

      却是使得线程thread1、thread2、thread3、thread4同步了,很是郁闷。

      我推荐选用的同步锁对象:

    复制代码
    public class SyncTest
    {
        // 特殊的instance变量,用于充当同步锁的对象
        private byte[] lock = new byte[0];
       
        public void method(String flag)
        {
            synchronized (lock)
            {
                System.out.println(flag + ", invoke method f....");
                try
                {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
       
        public static void main(String[] args)
        {
            SyncTest test1 = new SyncTest();
           
            SyncTest test2 = new SyncTest();
           
            MyThread1 myThread1 = new MyThread1();
            MyThread1 myThread2 = new MyThread1();
            myThread1.syncTest = test1;
            myThread2.syncTest = test1;
           
            MyThread1 myThread3 = new MyThread1();
            MyThread1 myThread4 = new MyThread1();
            myThread3.syncTest = test2;
            myThread4.syncTest = test2;
           
            myThread1.start();
            myThread2.start();
            myThread3.start();
            myThread4.start();
           
        }
    }
  • 相关阅读:
    Git修改用户名和提交的邮箱
    在vue项目中引入bootstrap
    自定义radio,使用图片进行切换(点击切换时,获取变量值)
    nodejs知识清单
    swoft源码研究
    Nginx变量查询
    mongodb索引
    mongodb数据备份:导入导出
    mongodb主从复制和分片
    mongodb服务安装
  • 原文地址:https://www.cnblogs.com/zhengxingpeng/p/6674689.html
Copyright © 2011-2022 走看看