The Java control thread runs

  • 2020-04-01 03:13:52
  • OfStack

1, thread control is very common, such as the file transfer to the half, the need to suspend the file transfer, or to terminate the file transfer, this is actually the operation of the control thread.

2. The thread has 5 states: created, runnable, runnable, blocked and dead.

Create: creates a thread using the new operator

Runnable: after starting a thread using the start method, the system allocates resources

Run-time status: executes the thread's run method

Block: a running thread stops running for some reason

Dead status: thread ends

3. Security of traditional methods

The stop(),suspend(),resume(), and destroy() methods of Thread, which can cause deadlocks because they are not safe, are no longer used.

4. How to control the running of threads

For example, if the transfer of a file requires 10s, let the transfer pause at some point and continue until the transfer is complete. This is done using methods that implement Runnable, starting with the file transfer Runnable class

The code is as follows:


public class ThreadControlTest1 implements Runnable
{
 private int percent = 0;
 public void run()
 {
  while(true)
  {
   System.out.println(" Delivery schedule :"+ percent +"%");
   try
   {
    Thread.sleep(1000);
   }
   catch(Exception ex)
   {}
   percent += 10;
   if(percent == 100)
   {
    System.out.println(" Is transferred ");
    break;
   }
  }
 }
 public static void main(String[] args)
 {
  ThreadControlTest1 ft = new ThreadControlTest1();
  Thread th = new Thread(ft);
  th.start();
 }

}

5. Run, the simulation process of file transfer will be printed on the console. As you can see, if you run the object as a thread, the while loop exits after 10 executions.
But what if you need to pause the Thread at some point (say, 5 seconds later) (say, 1 minute), but you can't use Thread's correlation functions?

Common ways to solve this problem are as follows:

1. When you need to pause, simply let the thread's run method end to free the resource (in effect, let the thread end permanently)

2. When a thread needs to continue, a new thread is created to continue working

To end the run method, there is a while loop in the run method. Change the flag of the loop from true to false.

6. The above code can be changed as follows:


public class ThreadControlTest1 implements Runnable
{
 private int percent = 0;
 private boolean isRun = true;
 public void run()
 {
  while(isRun)
  {
   System.out.println(" Delivery schedule :"+ percent +"%");
   try
   {
    Thread.sleep(1000);
   }
   catch(Exception ex)
   {}
   percent += 10;
   if(percent == 100)
   {
    System.out.println(" Is transferred ");
    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();
 }
}


Related articles: