java控制线程运行
时间:2023-03-05 15:35:30
1、线程的控制很常见,如文件传送到一半时,需要暂停文件传送,或终止文件传送,这实际上就是控制线程的运行。
2、线程有创建、可运行、运行中、阻塞、死亡5个状态。
创建:使用new运算符创建一个线程
可运行:使用start方法启动一个线程后,系统分配了资源
运行中状态:执行线程的run方法
阻塞:运行的线程因为某种原因停止继续运行
死亡状态:线程结束
3、传统方法的安全问题
Thread的stop(),suspend(),resume(),destroy()方法,因为不安全,可能造成死锁,已经不再使用了。
4、如何控制线程的运行
举个例子,假如某个文件的传输需要10s,让其传输到某个时刻暂停传输,然后继续,到传完为止。使用实现Runnable的方法完成,首先是文件传输的Runnable类
代码如下:
public class ThreadControlTest1 implements Runnable
{
private int percent = 0;
public void run()
{
while(true)
{
System.out.println("传送进度:"+ percent +"%");
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{}
percent += 10;
if(percent == 100)
{
System.out.println("传送完毕");
break;
}
}
}
public static void main(String[] args)
{
ThreadControlTest1 ft = new ThreadControlTest1();
Thread th = new Thread(ft);
th.start();
}
}
5、运行,控制台上将打印文件传输的模拟过程。可以看出,如果将该类对象以线程运行,while循环会执行10次后退出。
但是,需要在某个时刻(如5秒后)暂停线程运行(如1分钟),但又不能使用Thread的相关函数,该怎么办?
要解决这个问题,常见的方法如下:
1.当需要暂停时,干脆让线程的run方法结束运行以释放资源(实际上就是让该线程永久结束)
2.线程需要继续时,新开辟一个线程继续工作
如何让run方法结束呢,在run方法中有一个while循环,将改循环的标志由true改为false即可。
6、上面的代码可以改为如下:
public class ThreadControlTest1 implements Runnable
{
private int percent = 0;
private boolean isRun = true;
public void run()
{
while(isRun)
{
System.out.println("传送进度:"+ percent +"%");
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{}
percent += 10;
if(percent == 100)
{
System.out.println("传送完毕");
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("暂停一分钟");
try
{
Thread.sleep(1000*60);
}catch(Exception ex)
{}
ft.isRun = true;
th = new Thread(ft);
th.start();
}
}
标签:java,线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
为什么Spring和IDEA都不推荐使用 @Autowired 注解
2023-12-08 17:54:44
![](https://img.aspxhome.com/file/2023/9/66029_0s.png)
C#中IEnumerable、ICollection、IList、List之间的区别
2022-07-27 18:15:07
Android启动画面的实现方法
2023-12-27 23:22:11
java高并发之线程组详解
2023-02-15 21:30:25
![](https://img.aspxhome.com/file/2023/1/108171_0s.jpg)
C#仿Windows XP自带的扫雷游戏
2023-07-30 07:40:48
![](https://img.aspxhome.com/file/2023/5/69785_0s.jpg)
C#实现文本文件读写方法汇总
2023-10-27 16:57:40
浅谈Servlet开发技术基础
2023-01-17 04:03:15
java基础的详细了解第七天
2023-02-01 12:06:26
![](https://img.aspxhome.com/file/2023/0/63700_0s.png)
基于html5+java实现大文件上传实例代码
2023-09-26 02:14:29
Android编程实现实时监听EditText文本输入的方法
2023-04-23 02:58:16
![](https://img.aspxhome.com/file/2023/4/137324_0s.jpg)
Android6.0仿微信权限设置
2023-03-03 09:40:30
![](https://img.aspxhome.com/file/2023/0/123340_0s.gif)
C++野指针和悬空指针的实现方法
2022-11-02 08:26:56
Eclipse转Itellij IDEA导入Git/svn本地项目的详细步骤
2021-11-06 12:57:10
![](https://img.aspxhome.com/file/2023/1/103821_0s.png)
SpringBoot整合Mybatis与druid实现流程详解
2022-09-17 17:05:31
![](https://img.aspxhome.com/file/2023/4/118824_0s.png)
Java 面向对象通过new揭开对象实例化
2021-09-30 20:19:05
![](https://img.aspxhome.com/file/2023/6/122196_0s.png)
spring-boot读取props和yml配置文件的方法
2022-06-01 14:54:26
SpringBoot yml配置文件读取方法详解
2022-12-13 18:04:19
![](https://img.aspxhome.com/file/2023/6/99916_0s.png)
java15新功能的详细讲解
2023-08-23 04:40:21
深入c# 类和结构的区别总结详解
2023-01-25 10:27:27
详解SpringCloud mysql实现配置中心
2023-11-02 05:12:57
![](https://img.aspxhome.com/file/2023/8/58718_0s.png)