Home  >  Article  >  Java  >  Detailed explanation of code to implement synchronization function in Java

Detailed explanation of code to implement synchronization function in Java

黄舟
黄舟Original
2017-10-09 09:59:381248browse

This article mainly introduces the relevant content of synchronization functions in Java threads, including example codes. Friends who need it can refer to it.


/*
同步函数
当函数中的代码全部放在了同步代码块中,那么这个函数就是同步函数
*/
//同步函数的锁是this锁,this是一个引用,this指向的对象就是锁
//下面证明一下同步函数的锁就是this
//创建两个线程,一个在同步代码块中执行,另一个在同步函数中执行
//同步代码块用的锁是obj,同步函数用的所是this
//这就导致了两个线程存在两把锁,会出现上次所说的安全问题,即出现错误数据
//只有两个线程同时用一把锁,才能解决多线程的安全问题
class Ticket implements Runnable{
  private int num = 50;//当用静态同步函数时,需要将对象也改为静态的
  private Object obj = new Object();
  //加一个flag标记,一个线程得到CPU,判断flag值
  //如果是true,让他在同步代码块中执行,一旦进去就出不来了,因为任务代码为死循环
  //否则让他在同步函数中执行
  boolean flag = true;
  public void run(){
    if(flag){
      while(true){
        //同步代码块,这里用的锁是obj,与同步函数用不一样的锁,会出现安全问题
        //synchronized(obj){
        //将锁改为this,与同步函数为同一把锁,就没有问题了
        synchronized(this){//如果下面是静态同步函数,则应该把this改为Ticket.class,同一把锁
          if(num>0){
            //强制线程放弃CPU,睡眠的线程不会放弃锁
            try{Thread.sleep(20);}catch(InterruptedException e){e.printStackTrace();}
            System.out.println(Thread.currentThread().getName()+"...sale..."+num--);//1
          }
        }//释放锁
      }
    }
    else{
      while(true){
        fun();
      }
    }
  }
  ////静态函数进内存的时候不存在对象,但是存在其所属类的字节码文件对象,属于Class类型的对象,
  //锁必须是对象,字节码文件,也是个对象,所以,静态同步函数的锁就是其所属类的字节码文件对象
  //public static synchronized void fun(){//锁为Ticket.class
  //这个函数的代码都是同步代码块中的,所以这个函数可以修饰为同步的,即同步函数
  public synchronized void fun(){
    if(num>0){
      //强制线程放弃CPU,睡眠的线程不会放弃锁
      try{Thread.sleep(20);}catch(InterruptedException e){e.printStackTrace();}
      System.out.println(Thread.currentThread().getName()+"...sale..."+num--);//1
    }
  }
}
class test{
  public static void main(String[] args){
    Ticket t = new Ticket();
    Thread t1 = new Thread(t);
    Thread t2 = new Thread(t);
    t1.start();
    //t1先启动,但是他并不一定能抢到CPU,主线程依旧拿着CPU
    //主线程拿着CPU往下走,将flag改为了false,导致两个
    //线程同时用的一个任务代码,即一把锁,不会出现安全问题,所以,应该在此处
    //让主线程进入睡眠状态,主线程放弃CPU,然后t1立刻拿到CPU,
    //这样t1就可以,在flag是true的情况下,进入同步代码块中执行
    //所以t1用的就是obj锁,然后主线程再拿上CPU,将flag改为false
    //t2拿上CPU时,flag就为false,所以进入的是同步函数中执行,
    //同步函数用的锁是this,两把锁,肯定会出现线程安全问题,所以,
    //如果想解决安全问题,将同步代码块的锁,也改为this,即可解决
    //让主线程放弃CPU
    try{
      Thread.sleep(20);
    }catch(InterruptedException e){
      e.printStackTrace();
    }
    t.flag = false;
    t2.start();
  }
}

Summary

The lock of the synchronization function is this, and the lock of the static synchronization function is the bytecode file of the class to which it belongs. object.

The above is the detailed content of Detailed explanation of code to implement synchronization function in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn