Java gets thread return data through callback

1. Low-key and natural callback concept

Multithreading means that the boss arranges multiple people to complete the work at the same time. Of course, the boss wants to know the completion of each work.

The simplest idea is to poll, that is, every once in a while, the boss will ask everyone if their work is completed. Is this method feasible? Of course it is feasible, but it is a waste of time for the boss. The boss is definitely unhappy.

So what is better, of course, everyone reports after they have completed their work, and reports the results of the work to the boss.

This way of actively notifying the main program of the result after the thread is executed is called a callback.

Don't be fooled by the seemingly high-end term Callback. In fact, callback is a very low-key, natural and common thinking concept.

2. Callback static method

Since static methods are globally universal, we can easily call back static methods in the main program in a thread.

Look at the thread first, and call back the static method of the main program after execution to tell the main program the result of execution.

/**
 * 员工:负责读取文件大小
 */
public class ReadFileMan implements Runnable {
    
    
	/**
	 * 文件名
	 */
	private String fileName;

	public ReadFileMan(String fileName) {
    
    
		this.fileName = fileName;
	}

	@Override
	public void run() {
    
    
		File f = new File(fileName);
		if (f.exists() && f.isFile()) {
    
    
			// 回调主程序的静态方法
			ReadFileBoss.knowFileLength(fileName, f.length());
		} else {
    
    
			// 回调主程序的静态方法
			ReadFileBoss.knowFileLength(fileName, f.length());
		}
	}
}

The main program is relatively simple, start the thread, and then wait for the callback:

/**
 * 主程序
 */
public class ReadFileBoss {
    
    
	public static void knowFileLength(String fileName, long length) {
    
    
		System.out.println("老板知道了文件[" + fileName + "]大小:" + length);
	}

	public static void main(String[] args) {
    
    
		// 启动线程
		ReadFileMan readFileMan = new ReadFileMan("D:\\temp\\1.txt");
		Thread thread = new Thread(readFileMan);
		thread.start();
		// 输出:老板知道了文件[D:\temp\1.txt]大小:6
	}
}

In the above program, the boss ReadFileBoss tells the employee readFileMan to read the file size, and only needs to wait for the employee to report the result.

For employees, it is easy to call back the static method of the boss directly after execution.

3. Callback instance method

Another method is the callback instance method. This model is equivalent to each employee having a manager who is responsible for managing the employees' daily behavior and supervising the execution results. The boss only needs to manage the manager, and the employees only need to report to the manager.

OK, the employee thread needs to know who its manager is, so modify it as follows:

/**
 * 员工:负责读取文件大小
 */
public class ReadFileMan implements Runnable {
    
    
	/**
	 * 文件名
	 */
	private String fileName;

	/**
	 * 员工需要知道自己的经理是谁
	 */
	private ReadFileManager readFileManager;

	public ReadFileMan(String fileName, ReadFileManager readFileManager) {
    
    
		this.fileName = fileName;
		this.readFileManager = readFileManager;
	}

	@Override
	public void run() {
    
    
		File f = new File(fileName);
		if (f.exists() && f.isFile()) {
    
    
			// 回调经理的实例方法
			readFileManager.knowFileLength(fileName, f.length());
		} else {
    
    
			// 回调经理的静态方法
			readFileManager.knowFileLength(fileName, f.length());
		}
	}
}

The manager needs to arrange employees to work, and then the manager also needs to manage the results of the work.

/**
 * 经理
 */
public class ReadFileManager {
    
    
	/**
	 * 执行结果
	 */
	public void knowFileLength(String fileName, long length) {
    
    
		System.out.println("经理知道了文件[" + fileName + "]大小:" + length);
		System.out.println("经理根据工作情况进行进一步处理");
	}

	/**
	 * 安排工作
	 */
	public void plan() {
    
    
		// 启动线程
		ReadFileMan readFileMan = new ReadFileMan("D:\\temp\\1.txt", this);
		Thread thread = new Thread(readFileMan);
		thread.start();
	}
}

The boss is relieved and directly let the manager take charge:

/**
 * 主程序
 */
public class ReadFileBoss {
    
    
	public static void main(String[] args) {
    
    
		// 把事情交给经理就行了
		ReadFileManager manager=new ReadFileManager();
		manager.plan();
		// 执行结果:经理知道了文件[D:\temp\1.txt]大小:6
	}	
}

4. Summary

From the above example, it can be found that it is better to let the manager manage, because each manager can manage multiple employees (multiple employee threads call back one manager instance); each employee can also report to multiple managers ( The employee saves the list of callback managers); this is very flexible.

Let an instance to manage the thread, you can realize various management logic, easy to expand, and more in line with the idea of ​​object-oriented programming.

Therefore, it is recommended to use instance method callback to obtain thread data.

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/107399610