1. 查看线程是否还存活

package tet;public class kk extends Thread{
//1. 查看线程是否还存活
public void run(){
for(int i=0;i<5;i++)
PrintName(2);
} public void PrintName(int i){
System.out.println(i+": "+Thread.currentThread().getName());
} public static void main(String[] args) {
//把main建立出来当做子线程
kk mythread = new kk();
mythread.setName("Thread"); System.out.println("before:"+mythread.isAlive());
mythread.start();
System.out.println("after:"+mythread.isAlive()); for(int i=0;i<5;i++)
mythread.PrintName(1); System.out.println("after:"+mythread.isAlive());
}
}

结果:

before:false
after:true
1: main
1: main
1: main
2: Thread
2: Thread
2: Thread
2: Thread
2: Thread
1: main
1: main
after:false

2. 状态监测

notify是唤醒wait的,wait可以是自己运行,也可以是别的程序运行;

结果:

class MyThread extends Thread{

    boolean tmp_wait = false;  //是否等待

    MyThread(){
System.out.println(getName()+" : construct");
}
public void run(){
System.out.println(getName()+" : running");
startWait();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //注意:必须要同步
synchronized void startWait() {
try {
while(!tmp_wait) {
System.out.println(getName()+" : waiting");
wait();
}
}
catch(InterruptedException exc) {
System.out.println(getName()+"interrupted");
}
}
//注意:一定要同步,不同步不会编译报错,运行会出错,可以试试
synchronized void notice() {
tmp_wait = true;
notify();
} } public class kk {
public static void main(String args[]) {
System.out.println("hello world");
MyThread t = new MyThread();
try{
t.setName("thread1");
showThreadStatus(1, t);
t.start();
Thread.sleep(50);
showThreadStatus(2, t); t.notice(); //唤醒wait
Thread.sleep(50);
showThreadStatus(3, t); if(t.isAlive()){
showThreadStatus(4, t);
}
while(t.isAlive()){} showThreadStatus(5, t); }catch(InterruptedException e){
e.printStackTrace();
} } static void showThreadStatus(int num, Thread t){
System.out.println("num="+num+" ;name="+t.getName()+" ; status="+t.getState()+" ; live="+t.isAlive());
} }

结果:

hello world
Thread-0 : construct
num=1 ;name=thread1 ; status=NEW ; live=false
thread1 : running
thread1 : waiting
num=2 ;name=thread1 ; status=WAITING ; live=true
num=3 ;name=thread1 ; status=TIMED_WAITING ; live=true
num=4 ;name=thread1 ; status=TIMED_WAITING ; live=true
num=5 ;name=thread1 ; status=TERMINATED ; live=false

3. 中断线程

package tet;

public class kk implements Runnable {
public void run() {
try {
System.out.println("in run() - 将运行 work2() 方法");
work2();
System.out.println("in run() - 从 work2() 方法回来");
}
catch (InterruptedException x) {
System.out.println("in run() - 中断 work2() 方法");
return;
}
System.out.println("in run() - 休眠后执行");
System.out.println("in run() - 正常离开");
}
public void work2() throws InterruptedException {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("C isInterrupted()=" + Thread.currentThread().isInterrupted());
Thread.sleep(2000);
System.out.println("D isInterrupted()=" + Thread.currentThread().isInterrupted());
}
}
}
public void work() throws InterruptedException {
while (true) {
for (int i = 0; i < 100000; i++) {
int j = i * 2;
}
System.out.println("A isInterrupted()=" + Thread.currentThread().isInterrupted());
if (Thread.interrupted()) {
System.out.println("B isInterrupted()=" + Thread.currentThread().isInterrupted());
throw new InterruptedException();
}
}
}
public static void main(String[] args) {
kk si = new kk();
Thread t = new Thread(si);
t.start();
try {
Thread.sleep(2000);
}
catch (InterruptedException x) {
}
System.out.println("in main() - 中断其他线程");
t.interrupt();
System.out.println("in main() - 离开");
}
}

结果:

in run() - 将运行 work2() 方法
in main() - 中断其他线程
in main() - 离开
C isInterrupted()=true
in run() - 中断 work2() 方法

JAVA练手--线程(Thread)的更多相关文章

  1. 20个Java练手项目,献给嗜学如狂的人

    给大家推荐一条由浅入深的JAVA学习路径,首先完成 Java基础.JDK.JDBC.正则表达式等基础实验,然后进阶到 J2SE 和 SSH 框架学习.最后再通过有趣的练手项目进行巩固. JAVA基础 ...

  2. Java中的线程Thread总结

    首先来看一张图,下面这张图很清晰的说明了线程的状态与Thread中的各个方法之间的关系,很经典的! 在Java中创建线程有两种方法:使用Thread类和使用Runnable接口. 要注意的是Threa ...

  3. Java中的线程Thread方法之---interrupt()

    前几篇都介绍了Thread中的几个方法,相信大家都发现一个相似点,那就是sleep,join,wait这样的阻塞方法都必须捕获一个InterruptedException异常,顾名思义就是一个线程中断 ...

  4. Java中的线程Thread方法之---suspend()和resume()

    前篇说到了Thread中的join方法,这一篇我们就来介绍一下suspend()和resume()方法,从字面意义上可以了解到这两个方法是一对的,suspend()方法就是将一个线程挂起(暂停),re ...

  5. 去哪找Java练手项目?

    经常有读者在微信上问我: 在学编程的过程中,看了不少书.视频课程,但是看完.听完之后感觉还是不会编程,想找一些项目来练手,但是不知道去哪儿找? 类似的问题,有不少读者问,估计是大部分人的困惑. 练手项 ...

  6. Java中的线程Thread方法之---join()

    上一篇我们说到了Thread中的stop方法,这一篇我们再来看一下方法join的使用,那么方法Join是干啥用的? 简单回答,同步,如何同步? 怎么实现的? 下面将逐个回答. join方法从字面上的意 ...

  7. Java中的线程Thread方法之---stop()

    搞过Java线程的人都知道,stop这个方法是臭名昭著了,早就被弃用了,但是现在任然有很多钟情与他的人,永远都放不下他,因为从他的字面意思上我们可以知道他貌似可以停止一个线程,这个需求是每个搞线程开发 ...

  8. JAVA练手--集合

    集合框架体系如图所示 Collections:是一个工具类java.util.Collections(可以使用它对集合对象进行操作) Collection:除了map(键值对)其他集合的父类 1. S ...

  9. Java 中的线程 thread

    一.问:线程有哪些状态? new, runnable, running, waiting, dead 线程状态间的流转 二.问:线程实现方式? 实现 Runnable 接口,然后new Thread, ...

随机推荐

  1. Windows服务器管理与优化

    一.服务器自动重启windows服务器运行时间长了,内存会爆满,比如数据库会缓存大量的数量,IIS进程也会缓存数据而没有及时释放.这样需要定时重启服务器来释放内存. 创建任务计划,如在 每周一/周三/ ...

  2. 比特币的TxHash为什么会发生改变

    比特币中TxHash为什么会变化? 一直不理解比特币的Tx在被打包确认之前TxHash为什么会发生变化,这次终于找到了依据. 交易可延展性 虽然交易签名后,签名当前不会覆盖经过哈希处理以创建事务哈希的 ...

  3. iOS 使用 TestFlight 测试

    TestFlight 已经并入 Itunes connect. 测试方法: 1. itunes connect 上创建应用 2. xcode 里 archive 应用并 submit 到 itunes ...

  4. Oracle数据库exp和imp方式导数据

    这里导入导出路径都在D盘下,默认文件名为:example.dmpexp方式导出数据相关参数项如下: 关键字  说明  默认USERID                     用户名/口令FULL   ...

  5. Lora通讯

    Lora通讯 今年放弃了电源,踏入了物联网行业,也不知道算不算放弃吧,但我内心始终在呐喊,早晚会把你拿下,现在暂且放过你! 首先普及一下物联网,物联网是21世纪兴起的行业,最开始是由比尔盖茨在1995 ...

  6. luoguP5074 Eat the Trees

    https://www.luogu.org/problemnew/show/P5074 插头 $ dp $ 入门题 如果你还不会插头 $ dp $ 请右转 洛谷插头dp题解 虽然是入门题但还是逃不过分 ...

  7. django使用haystack来调用Elasticsearch搜索引擎

    如何使用django来调用Elasticsearch实现全文的搜索 环境:django ==1.11.11 Haystack为Django提供了模块化的搜索.它的特点是统一的,熟悉的API,可以让你在 ...

  8. Qt5学习笔记(控件)

    上面的程序仅仅可以显示一个 大概 的界面,对其进行单击等操作,界面仅有一些简单的反应,对应的程序不能得知界面有什么改变(是否进行单击选择,文本框中是否有文字输入) 下面对程序进行完善. T05Cont ...

  9. 【vim】插入模式与常用编辑操作

    vim不像很多编辑器那样一启动便可以直接编辑文本,需要在普通模式按下i, a等键才会进入插入模式进行文本编辑. 如何进入插入模式 以下的命令都会让vim从普通模式切换到插入模式,但命令执行后的字符插入 ...

  10. Lambda入门

    Lambda 来源于微积分数学中的 λ,其涵义是声明为了表达一个函数具体需要什么. Table of contents Introduction 使用 Introduction 什么是Lambda? ...