欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

synchronized的用法

程序员文章站 2022-07-04 17:01:38
...

1.synchronized的几种用法 synchronized关键字最主要有以下5种应用方式,下面分别介绍。 1. 修饰对象普通方法,作用于当前对象,进入同步方法前要获得当前对象的锁 2. 修饰对象静态方法,作用于当前类对象加锁,进入同步该方法前要获得当前类对象的锁 3. 修饰代码块,指定加锁对象,对给定对象加锁,进入同步代码库前要获得指定对象的锁。 4. 修饰 this,指定当前对象,对给当前对象加锁,进入同步代码前要获得当前对象的锁(该方式和方式1作用相同)。 5. 修饰 xxx.class,(该方式和方式2作用相同)。

 

 

2.锁竞争说明 同时调用两个都加有synchronized的方法时存在锁竞争关系(同时调用时会一个先输出,另一个晚两秒输出)以下代码同时调用会产生竞争关系(this的作用相同)

 

 

	public synchronized void getSex() throws InterruptedException {
		Thread.sleep(2000);
	    System.out.println("getSex synchronized : " + System.currentTimeMillis());
	}
	
	public synchronized void getHeight() throws InterruptedException {
	    Thread.sleep(2000);
	    System.out.println("getHeight synchronized : " + System.currentTimeMillis());
	}
	
	public void getWeight() throws InterruptedException {
	     synchronized (this){
	          Thread.sleep(2000);
	          System.out.println("getWeight synchronized : " + System.currentTimeMillis());
	     }
	}

 

  • 同时调用两个都加有synchronized的静态(static)方法时存在锁竞争关系(同时调用时会一个先输出,另一个晚两秒输出)
  • 	public static synchronized void getName() throws InterruptedException {
    	     Thread.sleep(2000);
    	     System.out.println("getName static synchronized : " + System.currentTimeMillis());
    	}
    	
    	public static synchronized void getAge() throws InterruptedException {
    	      Thread.sleep(2000);
    	      System.out.println("getAge static synchronized : " + System.currentTimeMillis());
    	}
  • 同时调用锁代码块的要看锁的目标是不是同一个,如果是同一个时就存在锁竞争关系,如果不是则没有关系 当传入的参数一样则有竞争关系
  • 这里要注意点的就是 当传入的是getWX(“55”)和getQQ(“55”),getWX(new String(“55”))和getQQ(new String(“55”))这两中的结果是不同的 注意前面的存在竞争关系,后面不存在,因为new 是从新创建了对象,内存地址不是同一个
  • 	public void getQQ(String qq) throws InterruptedException {
    	      synchronized (qq){
    	           Thread.sleep(2000);
    	           System.out.println("getQQ QQ synchronized : " + System.currentTimeMillis());
    	      }
    	}
    	
    	public void getWX(String wx) throws InterruptedException {
    	      synchronized (wx){
    	           Thread.sleep(2000);
    	           System.out.println("getWX WX synchronized : " + System.currentTimeMillis());
    	      }
    	}
    
     
  • 同时一个synchronized的静态(static)方法和一个普通synchronized方法时 和一个synchronized代码块时(代码块不是 this和xxx.class),不存在竞争关系
  • 这里要注意点的就是 synchronized代码块必须不是this和xxx.class,要不然会有竞争关系 因为this和synchronized普通方法效果相同,xxx.class与synchronized静态方法效果相同
  • public synchronized void getSex() throws InterruptedException {
           Thread.sleep(2000);
           System.out.println("getSex synchronized : " + System.currentTimeMillis());
    }
    
    public static synchronized void getAge() throws InterruptedException {
          Thread.sleep(2000);
          System.out.println("getAge static synchronized : " + System.currentTimeMillis());
    }
    
    public void getWX(String wx) throws InterruptedException {
          synchronized (wx){
              Thread.sleep(2000);
              System.out.println("getWX WX synchronized : " + System.currentTimeMillis());
          }
    }
    
     以下是全部代码
  • /**
     * 文件介绍
     *
     * @author YangLD
     * @date 2018/8/3 21:17
     */
    public class RunMain {
    
    
        static{
            System.out.println("static");
        }
    
        public static void main(String[] args) throws IllegalAccessException, InstantiationException {
    
            RunMain runMain = RunMain.class.newInstance();
    
            System.out.println(System.currentTimeMillis());
            CyclicBarrier cyclicBarrier = new CyclicBarrier(9);
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        RunMain.getName();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        RunMain.getAge();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        runMain.getEmail();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        runMain.getSex();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
    
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        runMain.getHeight();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        runMain.getWeight();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        runMain.getQQ("qq");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        runMain.getWX("qq");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        runMain.getAli();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
    
        public static synchronized void getName() throws InterruptedException {
            Thread.sleep(2000);
            System.out.println("getName static synchronized : " + System.currentTimeMillis());
        }
    
        public static synchronized void getAge() throws InterruptedException {
            Thread.sleep(2000);
            System.out.println("getAge static synchronized : " + System.currentTimeMillis());
        }
    
        public synchronized void getSex() throws InterruptedException {
            Thread.sleep(2000);
            System.out.println("getSex synchronized : " + System.currentTimeMillis());
        }
    
        public synchronized void getHeight() throws InterruptedException {
            Thread.sleep(2000);
            System.out.println("getHeight synchronized : " + System.currentTimeMillis());
        }
    
        public void getEmail() throws InterruptedException {
            Thread.sleep(2000);
            System.out.println("getEmail : " + System.currentTimeMillis());
        }
    
        public void getWeight() throws InterruptedException {
            synchronized (this){
                Thread.sleep(2000);
                System.out.println("getWeight synchronized : " + System.currentTimeMillis());
            }
        }
    
        public void getQQ(String qq) throws InterruptedException {
            synchronized (qq){
                Thread.sleep(2000);
                System.out.println("getQQ QQ synchronized : " + System.currentTimeMillis());
            }
        }
    
        public void getWX(String wx) throws InterruptedException {
            synchronized (wx){
                Thread.sleep(2000);
                System.out.println("getWX WX synchronized : " + System.currentTimeMillis());
            }
        }
    
        public void getAli() throws InterruptedException {
            synchronized (RunMain.class){
                Thread.sleep(2000);
                System.out.println("getAli synchronized : " + System.currentTimeMillis());
            }
        }
    }
    
       1)CountDownLatch和CyclicBarrier都能够实现线程之间的等待,只不过它们侧重点不同:

        CountDownLatch一般用于某个线程A等待若干个其他线程执行完任务之后,它才执行;

        而CyclicBarrier一般用于一组线程互相等待至某个状态,然后这一组线程再同时执行;

        另外,CountDownLatch是不能够重用的,而CyclicBarrier是可以重用的。

      2)Semaphore其实和锁有点类似,它一般用于控制对某组资源的访问权限。