Java uses polling to get thread return data

1. The characteristics of multi-threading

In general programs, there is only one main thread, which executes sequentially from top to bottom.

However, in a multithreaded program, the execution of multiple threads is concurrent, and the programmer cannot predict the execution end time of each thread at design time.

Look at the thread class below, used to read the file size, and then put the result in the thread local variable.

/**
* 读取文件大小
*/
public class ReadFileRunnable implements Runnable {
    
    
   /**
    * 文件名
    */
   private String fileName;

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

   /**
    * 文件大小,默认为-2
    */
   private long length = -2;

   public long getLength() {
    
    
   	return length;
   }

   @Override
   public void run() {
    
    
   	File f = new File(fileName);
   	if (f.exists() && f.isFile()) {
    
    
   		this.length = f.length();
   	} else {
    
    
   		this.length = -1;// 文件不存在
   	}
   }
}

If you follow the general thinking, we first start the thread to get the file size, and then output the file size.

	public static void main(String[] args) {
    
    
		// 启动线程
		ReadFileRunnable writeFileRunnable = new ReadFileRunnable("D:\\temp\\1.txt");
		Thread thread = new Thread(writeFileRunnable);
		thread.start();
		// 输出结果
		System.out.println("length:" + writeFileRunnable.getLength());// 输出-2
	}

After running it many times, the output is -2. This is because the thread we started has not been executed yet, and the statement that outputs the result below has been executed. This is because the thread we started needs to read the file, which is an IO operation, and the speed must be relatively slow.

2. Use polling to force a wait

The simplest solution is to use polling and always check the results of thread execution.

	public static void main(String[] args) {
    
    
		// 启动线程
		ReadFileRunnable writeFileRunnable = new ReadFileRunnable("D:\\temp\\1.txt");
		Thread thread = new Thread(writeFileRunnable);
		thread.start();
		// 一直等待
		while (true) {
    
    
			if (writeFileRunnable.getLength() == -2) {
    
    // 无结果
				continue;
			} else {
    
    
				System.out.println("length:" + writeFileRunnable.getLength());// 输出-2
				break;// 直到获取结果结束,此时可以输出文件大小
			}
		}
	}

3. Disadvantages of polling

Using polling seems to solve the problem, but it is actually a waste of performance. We tested:

	public static void main(String[] args) {
    
    
		// 启动线程
		ReadFileRunnable writeFileRunnable = new ReadFileRunnable("D:\\temp\\1.txt");
		Thread thread = new Thread(writeFileRunnable);
		thread.start();
		// 一直等待
		int i = 0;
		while (true) {
    
    
			i++;
			if (writeFileRunnable.getLength() == -2) {
    
    // 无结果
				continue;
			} else {
    
    
				System.out.println("循环执行次数:" + i);// 循环执行次数:14365
				System.out.println("length:" + writeFileRunnable.getLength());// 输出-2
				break;// 直到获取结果结束
			}
		}
	}

In other words, we checked more than 10,000 times, and only one time was effective, which is a huge waste.

4. Summary

In a multithreaded program, polling can be used to obtain the data returned by the thread, but it is a waste of performance. This method is generally not recommended.

Guess you like

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