The main thread is over. Why is the process still executing?

Keywords: Programming Java Linux

In the process of testing the performance of APP, I once encountered a rather awkward problem. The main thread has ended, but the program is still in execution, but I can not find what to do. It is very distressing for a moment. Share your code first, and then tell me why I found it.

public static void main(String[] args) {
		String timess = args[0];
		int times = Common.getInstance().changeStringToInt(timess);
		for (int i = 0; i < times; i++) {
			Date start = Common.getInstance().getDate();
			StartApp startApp = new StartApp();// New Start app Thread
			Logcat logcat = new Logcat();// New log Thread
			PerformanceThread performanceThread = new PerformanceThread("monkey", package_name);// Performance monitoring
			performanceThread.start();
			startApp.start();
			logcat.start();
			execCmdAdb(command, "monkey" + getNow() + ".log");
			startApp.stopThread();
			logcat.stopLoacat();
			performanceThread.stopRecord();
			Date end = Common.getInstance().getDate();
			Common.getInstance().outputTimeDiffer(start, end, "The first" + i + "second");
		}
		output("End monkey Now!");
	}

It's just a few new threads. I guess these threads didn't end well, which led to many problems later. After checking, it was found in the thread that started APP. What this thread does is start APP once a minute and check the WiFi status to keep WiFi on/off and reverse the WiFi status for ten minutes. After the main thread has finished, these processes are still sleeping (), so the execution code can not be found. Share the problematic code for this class:

@Override
	public void run() {
		while (MKEY) {
			int num = Common.getInstance().getRandomInt(4);
			if (num == 1) {
				stopJuziApp();
				Common.getInstance().sleep(500);
			}
			Common.getInstance().sleep(60 * 1000);
			keepWifiONorOFF(WIFISTATUS);
			startJuziApp();
		}
	}
public void run() {
		while (WIFIKEY) {
			for (int i = 0; i < 10; i++) {
				if (WIFIKEY) {
					break;
				}
				Common.getInstance().sleep(60 * 1000);
				keepWifiONorOFF(WIFISTATUS);
			}
			WIFISTATUS = !WIFISTATUS;// Reverse WiFi State
		}
	}

That's because sleep() in it led to the previous reason. Later, I changed the WiFiswitch code and put it in the startAPP method. Then join the main thread in each thread by using the join method in java multithreading, which can avoid the embarrassment of the end of the main thread while other threads are still running. Share the changed code:

public static void main(String[] args) throws InterruptedException {
		String timess = args[0];
		int times = Common.getInstance().changeStringToInt(timess);
		for (int i = 0; i < times; i++) {
			Date start = Common.getInstance().getDate();
			StartApp startApp = new StartApp();// New Start app Thread
			Logcat logcat = new Logcat();// New log Thread
			PerformanceThread performanceThread = new PerformanceThread("monkey", package_name);// Performance monitoring
			performanceThread.start();
			startApp.start();
			logcat.start();
			execCmdAdb(command, "monkey" + getNow() + ".log");
			startApp.stopThread();
			logcat.stopLoacat();
			performanceThread.stopRecord();
			logcat.join();
			performanceThread.join();
			startApp.join();
			Date end = Common.getInstance().getDate();
			Common.getInstance().outputTimeDiffer(start, end, "The first" + i + "second");
		}
		output("End monkey Now!");
	}

Or a method can be encapsulated to accomplish:

	public static void main(String[] args) throws InterruptedException {
		String timess = args[0];
		int times = Common.getInstance().changeStringToInt(timess);
		for (int i = 0; i < times; i++) {
			Date start = Common.getInstance().getDate();
			StartApp startApp = new StartApp();// New Start app Thread
			Logcat logcat = new Logcat();// New log Thread
			PerformanceThread performanceThread = new PerformanceThread("monkey", package_name);// Performance monitoring
			performanceThread.start();
			startApp.start();
			logcat.start();
			execCmdAdb(command, "monkey" + getNow() + ".log");
			startApp.stopThread();
			logcat.stopLoacat();
			performanceThread.stopRecord();
			joinThread(logcat, startApp, performanceThread);
			Date end = Common.getInstance().getDate();
			Common.getInstance().outputTimeDiffer(start, end, "The first" + i + "second");
		}
		output("End monkey Now!");
	}
 
	/**
	 * Add all threads to the main thread
	 * 
	 * @param threads
	 */
	public static void joinThread(Thread... threads) {
		for (Thread thread : threads) {
			try {
				thread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

Selection of Technical Articles

  1. One line of java code prints a heart
  2. Chinese Language Version of Linux Performance Monitoring Software netdata
  3. Interface Test Code Coverage (jacoco) Scheme Sharing
  4. Performance testing framework
  5. How to Enjoy Performance Testing on Linux Command Line Interface
  6. Graphic HTTP Brain Map
  7. How to Test Probabilistic Business Interface
  8. httpclient handles multi-user simultaneous online
  9. Automatically convert swagger documents into test code
  10. Five lines of code to build static blogs
  11. How httpclient handles 302 redirection
  12. A preliminary study on the testing framework of linear interface based on java
  13. Tcloud Cloud Measurement Platform

Selection of non-technical articles

  1. Why choose software testing as a career path?
  2. Ten Steps to Become a Great Java Developer
  3. Writing to everyone about programming thinking
  4. Obstacles to automated testing
  5. The Problems of Automated Testing
  6. Tested "Code Immortality" Brain Map
  7. Seven Steps to Become an Excellent Automated Testing Engineer
  8. Attitudes of Excellent Software Developers
  9. How to Execute Functional API Testing Correctly

Click on the Public Number Map

Posted by zulx on Mon, 16 Sep 2019 00:52:28 -0700