Home >Java >javaTutorial >Detailed introduction to java synchronization, asynchronous, blocking and non-blocking graphic code analysis

Detailed introduction to java synchronization, asynchronous, blocking and non-blocking graphic code analysis

黄舟
黄舟Original
2017-03-08 10:50:471818browse

This article mainly introduces the relevant information of java synchronization, asynchronous, blocking and non-blocking analysis. Friends who need it can refer to

java synchronization, asynchronous, blocking and non-blocking analysis

Summary:

Under normal circumstances, our program runs in a synchronous and non-blocking manner. However, our programs will always have some time-consuming operations, such as complex calculations (finding prime numbers between 1 and 1 billion) and operations that the program itself cannot control (IO operations, network requests). Methods that include these time-consuming operations can be called blocking methods, and tasks that include these time-consuming operations can be called blocking tasks. Blocking and non-blocking are defined by whether they take time.

If there are a large number of blocking operations in the program, it will affect program performance. But the existence of blocking is an objective fact, and our program cannot change it. A network request takes 3 seconds to respond. It is impossible for us to make it respond in 1 millisecond, because the server that accepts the request may be completely out of our control. But we can change the way we handle blocking - handle blocking tasks asynchronously. The main technology to achieve asynchronous is multi-threading. Illustration:

# Synchronization and asynchronousness are timing concepts. Synchronization means executing only one task at the same time, while asynchronous means executing multiple tasks at the same time.

Code example

Simulated network request:

package com.zzj.asyn; 
 
public class HttpRequest { 
  private Callable callable; 
   
  public HttpRequest(Callable callable) { 
    this.callable = callable; 
  } 
   
  public void send(){ 
    // 模拟网络请求 
    try { 
      Thread.sleep(1000 * 5); 
    } catch (InterruptedException e) { 
      e.printStackTrace(); 
      Thread.currentThread().interrupt(); 
    } 
    // 回调 
    this.callable.call("Hello world!"); 
  } 
   
  public interface Callable{ 
    void call(String result); 
  } 
}

Handle blocking tasks synchronously:

package com.zzj.asyn; 
 
import com.zzj.asyn.HttpRequest.Callable; 
 
/** 
 * 以同步的方式处理阻塞任务 
 * @author lenovo 
 * 
 */ 
public class App { 
  public static void main(String[] args) { 
    new HttpRequest(new Callable() { 
      @Override 
      public void call(String result) { 
        System.out.println("Thread:" + Thread.currentThread().getName()); 
        System.out.println("Message from remote server:" + result); 
      } 
    }).send(); 
    System.out.println("Thread " + Thread.currentThread().getName() + " is over!"); 
  } 
}

Result:

Thread:main 
Message from remote server:Hello world! 
Thread main is over!

Process blocking tasks asynchronously:

package com.zzj.asyn; 
 
import com.zzj.asyn.HttpRequest.Callable; 
 
/** 
 * 以异步的方式处理阻塞任务 
 * @author lenovo 
 * 
 */ 
public class App2 { 
  public static void main(String[] args) { 
    new Thread(new Runnable() { 
      @Override 
      public void run() { 
        new HttpRequest(new Callable() { 
          @Override 
          public void call(String result) { 
            System.out.println("Thread:" + Thread.currentThread().getName()); 
            System.out.println("Message from remote server:" + result); 
          } 
        }).send(); 
      } 
    }).start(); 
    System.out.println("Thread " + Thread.currentThread().getName() + " is over!"); 
  } 
}

Result:

Thread main is over! 
Thread:Thread-0 
Message from remote server:Hello world!


The above is the detailed content of Detailed introduction to java synchronization, asynchronous, blocking and non-blocking graphic code analysis. 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