1. Thread control is very common. For example, when the file is transferred halfway, file transfer needs to be paused or file transfer is terminated. This is actually controlling the operation of the thread.
2. Threads have five states: creation, operation, operation, blocking, and death.
Create: Create a thread using the new operator
Runable: After starting a thread using the start method, the system allocates resources
Running status: Run method of executing thread
Blocking: The running thread stops running for some reason
Death state: thread ends
3. Safety issues of traditional methods
Thread's stop(), suspend(), resume(), destroy(), and destroy() methods are no longer used because they are unsafe and may cause deadlocks.
4. How to control the operation of threads
For example, if the transfer of a file takes 10 seconds, let it be transferred to a certain moment and then continue until the transfer is completed. Use the method to implement Runnable, first of all, the Runnable class for file transfer
The code is as follows:
The code copy is as follows:
public class ThreadControlTest1 implements Runnable
{
private int percent = 0;
public void run()
{
While(true)
{
System.out.println("Transfer progress:"+ percent +"%");
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{}
percent += 10;
if(percent == 100)
{
System.out.println("Transfer Completed");
break;
}
}
}
public static void main(String[] args)
{
ThreadControlTest1 ft = new ThreadControlTest1();
Thread th = new Thread(ft);
th.start();
}
}
5. Run the simulation process of printing file transfer on the console. It can be seen that if the object of this class is run as a thread, the while loop will be executed 10 times and then exit.
However, you need to pause the thread running (such as 1 minute) at a certain moment (such as 5 seconds later), but you cannot use Thread's related functions. What should you do?
To solve this problem, the common methods are as follows:
1. When it is necessary to pause, simply let the thread's run method end to run to free resources (in fact, let the thread end permanently)
2. When the thread needs to continue, a new thread will be opened to continue working.
How to make the run method end? There is a while loop in the run method, and change the loop change flag from true to false.
6. The above code can be changed to the following:
The code copy is as follows:
public class ThreadControlTest1 implements Runnable
{
private int percent = 0;
private boolean isRun = true;
public void run()
{
while(isRun)
{
System.out.println("Transfer progress:"+ percent +"%");
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{}
percent += 10;
if(percent == 100)
{
System.out.println("Transfer Completed");
break;
}
}
}
public static void main(String[] args)
{
ThreadControlTest1 ft = new ThreadControlTest1();
Thread th = new Thread(ft);
th.start();
try
{
Thread.sleep(5000);
}catch(Exception ex)
{}
ft.isRun = false;
System.out.println("Pause for one minute");
try
{
Thread.sleep(1000*60);
}catch(Exception ex)
{}
ft.isRun = true;
th = new Thread(ft);
th.start();
}
}