Java 107-join methods, synchronized mechanisms, and principles

1. join Method

1. The method is a member method

2. Thread Merge

 

package com.bjpowernode.java_learning;

​

public class D107_1_JoinMethod {

  public static void main(String[] args) throws InterruptedException{

    Thread t = new Thread (new Processer107());

    t.setName("t");

    t.start();

   

    //Merge Threads

    t.join();//t Merge with the main thread as two stacks merge into one stack,That is, the child thread and the main thread are merged into a single thread

   

    //Main Thread

    for(int i=0;i<5;i++) {

      System.out.println(Thread.currentThread().getName()+"-->"+i);

    }

  }

}

class Processer107 implements Runnable{

  public void run() {

    for(int i=0;i<5;i++) {

      try {

        Thread.sleep(1000);

      }catch(InterruptedException e) {

       

      }

      System.out.println(Thread.currentThread().getName()+"-->"+i);

    }

  }

}

 

 

 

2. Synchronization of threads (locking)

1. Asynchronous programming model: between threads, execute independently, no one waits for anybody.

2. Synchronization programming model: between threads, execute one before the other, in the order of execution.

3. When should I synchronize?Why introduce thread synchronization?

(1) For data security, such as bank withdrawals.However, in order to ensure that the data is secure, a thread synchronization mechanism must be added.

(2) Use threading synchronization scenarios: i. Must be a multithreaded environment; i I. Multithreaded environments share a single data; i I i. Shared data involves modification operations.

The following program demonstrates an example of withdrawal (this mechanism does not use synchronization to see what happens)

 

package com.bjpowernode.java_learning;

​

public class D107_2_SynchronizedMethod {

  public static void main(String[] args) throws InterruptedException{

    //Create a public account

    Accout107_2 a = new Accout107_2("actno-001",8000.0);

    Processer107_2 c = new Processer107_2(a);

    //Create a thread to withdraw from the same account

    Thread t1 = new Thread(c);

    Thread t2 = new Thread(c);

    t1.start();

    t2.start();

   

  }

}

class Accout107_2 {

  private String actno;

  private double balance;

 

  public Accout107_2(String actno,double balance) {

    this.actno = actno;

    this.balance = balance;

  }

​

  public String getActno() {

    return actno;

  }

​

  public void setActno(String actno) {

    this.actno = actno;

  }

​

  public double getBalance() {

    return balance;

  }

​

  public void setBalance(double balance) {

    this.balance = balance;

  }

  //Provide a method of withdrawal from the outside world

  public void withdraw(double money)  {

   

    double after = this.balance - money;

    try {

      //Here we delayed deliberately and we can see that the balance is wrong

      Thread.sleep(1000);

    }catch(InterruptedException e){

     

    }

    this.setBalance(after);

  }

}

class Processer107_2 implements Runnable{

  //account

  Accout107_2 act;

  //Constructer

  public Processer107_2(Accout107_2 act) {

    this.act = act;

  }

  public void run() {

    act.withdraw(1000.0);

    System.out.println("The withdrawal was successful with the following balance:"+act.getBalance());

  }

}

Modify the code to solve the above problem

We just need to modify the withdram method

 

  public void withdraw(double money)  {

   

    //Put the code that needs to be synchronized in the synchronization statement block,Parameters must be populated with shared objects

    synchronized(this) {

      double after = this.balance - money;

      try {

        //Here we delayed deliberately and we can see that the balance is wrong

        Thread.sleep(1000);

      }catch(InterruptedException e){

       

      }

      this.setBalance(after);

    }

  }

Summary: Adding a thread synchronization mechanism ensures that our data is secure and accurate, but at the expense of performance.

3. Principle of synchronized

t1 thread and t2 thread, t1 thread execution to the synchronized keyword, will find the object lock of this object, if found, will enter the synchronization statement block to execute the program, when the code execution in the synchronization statement block ends Chinese characters, t1 thread returns the object lock of this.

During the execution of the synchronization statement block by the t1 thread, if the t2 thread also comes to execute the following code, it also encounters the synchronized keyword, so it also looks for the object lock of this thread, but the object lock is held by the t1 thread and can only wait here for the return of this object lock.(

4. Source code:

D107_1_JoinMethod.java

D107_2_SynchronizedMethod.java

https://github.com/ruigege66/Java/blob/master/D107_1_JoinMethod.java

https://github.com/ruigege66/Java/blob/master/D107_2_SynchronizedMethod.java

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. Welcome to the WeChat Public Number: Fourier Transform, Personal Public Number, only for learning and communication, Background Reply "Gift Pack" to get big data learning materials

 

Tags: Java Programming github Big Data

Posted on Thu, 09 Apr 2020 11:28:14 -0700 by superdude