java的多线程开发(一)
2019-10-12 08:25:16来源:博客园 阅读 ()
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
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:JAVA面试宝典
下一篇:稀疏数组(java实现)
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
