1, the control of the thread is very common, such as file transfer to half, need to pause file transfer, or terminate file transfer, which is actually control the running of the thread.
2, the line Chengyu create, can run, run, block, Death 5 states.
Creating: Creating a thread with the new operator
Can run: After starting a thread using the Start method, the system allocates resources
Run state: Execute thread's Run method
Blocking: The running thread stopped running for some reason
Death Status: Thread End
3, the traditional method of security issues
Thread's Stop (), suspend (), resume (), Destroy () method, because it is unsafe, may cause deadlocks and is no longer in use.
4, how to control the operation of the thread
For example, if a file's transmission needs to be 10s, let it transfer to a certain point to suspend the transmission, and then continue, until the end of the pass. Using the method of implementing Runnable, the first is the Runnable class of file transfer
The code is as follows:
Copy Code code as follows:
public class ThreadControlTest1 implements Runnable
{
private int percent = 0;
public void Run ()
{
while (true)
{
SYSTEM.OUT.PRINTLN ("Transmission progress:" + percent + "%");
Try
{
Thread.Sleep (1000);
}
catch (Exception ex)
{}
Percent + 10;
if (percent = 100)
{
SYSTEM.OUT.PRINTLN ("transmit complete");
Break
}
}
}
public static void Main (string[] args)
{
ThreadControlTest1 ft = new ThreadControlTest1 ();
Thread th = new Thread (ft);
Th.start ();
}
}
5, the operation, the console will print the file transfer simulation process. As you can see, if you run the class object as a thread, the while loop executes 10 times and exits.
But what if you need to suspend a thread running (for example, 1 minutes) at some point (such as 5 seconds), but you can't use the related function of thread?
The common approach to solving this problem is as follows:
1. When you need to pause, simply let the thread's Run method finish running to release the resource (in fact, let the line Cheng Yongju end)
2. When a thread needs to continue, a new thread is opened to continue working
How do I get the Run method to end, there is a while loop in the Run method, and the change loop flag is changed from true to false.
6, the above code can be changed as follows:
Copy Code code as follows:
public class ThreadControlTest1 implements Runnable
{
private int percent = 0;
Private Boolean isrun = true;
public void Run ()
{
while (Isrun)
{
SYSTEM.OUT.PRINTLN ("Transmission progress:" + percent + "%");
Try
{
Thread.Sleep (1000);
}
catch (Exception ex)
{}
Percent + 10;
if (percent = 100)
{
SYSTEM.OUT.PRINTLN ("transmit complete");
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 ("Suspend for one minute");
Try
{
Thread.Sleep (1000*60);
}catch (Exception ex)
{}
Ft.isrun = true;
th = new Thread (ft);
Th.start ();
}
}