java的多线程开发(一)
时间:2019-10-10
本文章向大家介绍java的多线程开发(一),主要包括java的多线程开发(一)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
java 启动新线程有三种方法:
类 Thread 直接new
接口 Runnable
接口 callable
Runnable 和Callable的区别:
Callable 可以返回值 即可return
package com.jwz.test; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class testThread { private static class UserRun implements Runnable { @Override public void run(){ System.out.println("I am runable "); } } private static class UserCall implements Callable{ @Override public String call(){ System.out.println("I am callable"); return "CallResult"; } } public static void main(String[] args) throws ExecutionException, InterruptedException { UserRun run = new UserRun(); new Thread(run).start(); UserCall call=new UserCall(); //用futureTask 构造Callable对象 FutureTask<String> future=new FutureTask<String>(call); new Thread(future).start(); System.out.println(future.get()); } }
线程的结束:
早期版本 用 stop(),resume(),suspend() 其中 stop() 不能释放资源 ,suspend()容易死锁。所以都不建议使用了
现在使用 interrput() 、 isInterrupted()、static 方法 interrputed()
interrput 方法用于中断一个线程(java 线程是协作式的),并不是强行关闭一个线程,只是跟这个线程打个招呼把中断线程的标志位设置为true ,是否重点又线程自己决定
isInterrupted 判断当前线程是否处于中断状态
static 方法 interrupted 判断当前线程是否处于中断状态,并把线程中断标志位设置为false
package com.jwz.thread; public class EndThread { private static class UseThread extends Thread{ public UseThread(String name){ super(name); } @Override public void run(){ String threadName=Thread.currentThread().getName(); while (!isInterrupted()){ System.out.println(threadName+" is run"); } System.out.println(threadName+" is finished and isInterrupt flag is "+isInterrupted()); } public static void main(String[] args) throws InterruptedException { Thread useThread=new UseThread("EndThread"); useThread.start(); sleep(20); useThread.interrupt(); } } }
线程里没有判断isInterrupted()的判断线程就不会中断
如果run() 方法 里有Thread.slee()方法 用try catch 抓取异常的时候 需要在 catch中调用 interrupt()方法才能中断线程
public class EndThreadRun { private static class useRun implements Runnable{ @Override public void run(){ String threadName=Thread.currentThread().getName(); while(!Thread.currentThread().isInterrupted()){ try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(threadName+" is strop and interrupt is " +Thread.currentThread().isInterrupted()); e.printStackTrace(); Thread.currentThread().interrupt(); System.out.println(threadName); } System.out.println(threadName+" is strop and interrupt is " +Thread.currentThread().isInterrupted()); } } } public static void main(String[] args) throws InterruptedException { useRun useRun=new useRun(); Thread endThread=new Thread(useRun,"endThread"); endThread.start(); Thread.sleep(20); endThread.interrupt(); } }
原文地址:https://www.cnblogs.com/dragonbad/p/11646704.html
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- Python叠加矩形框图层2种方法及效果
- PHP常量define和const的区别详解
- 解决运行出现'dict' object has no attribute 'has_key'问题
- tensorflow之读取jpg图像长和宽实例
- Python数据可视化实现多种图例代码详解
- Python使用tkinter实现摇骰子小游戏功能的代码
- pandas to_excel 添加颜色操作
- Python自带的IDE在哪里
- php+Ajax无刷新验证用户名操作实例详解
- PHP经典设计模式之依赖注入定义与用法详解
- 浅谈python出错时traceback的解读
- PHP判断是否是微信打开还是浏览器打开的方法
- php面向对象程序设计中self与static的区别分析
- PHP如何根据文件头检测文件类型实例代码
- PHP去除空数组且数组键名重置的讲解