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() 方法

最新文章

  1. Azure PowerShell (6) 设置单个Virtual Machine Endpoint
  2. EMIS系统运行时提示【无法验证发行者,您确实要运行此软件吗? 】
  3. SQL Server里Grouping Sets的威力
  4. ExtJs 4.2 treePanel 点击树节点 传送参数到后台(多个参数)
  5. sqlserver 变量
  6. Block使用变量,让你的程序看起来清晰!
  7. Tomcat部署问题及解决方法
  8. Android5.0+(CollapsingToolbarLayout)
  9. JAVA中IO和异常处理练习
  10. STL非变易算法 - STL算法
  11. 【 js 基础 】 深浅拷贝
  12. HttpClient的巨坑
  13. python+appium 自动化2--元素定位uiautomatorviewer
  14. Windows Community Toolkit 4.0 - DataGrid - Part01
  15. Linux mount BSD disk partition
  16. UI自动化(十一)selenium框架
  17. gimp的使用笔记
  18. C语言中的多线程编程
  19. 【three.js练习程序】旋转物体自身
  20. js中拼接HTML方式方法及注意事项

热门文章

  1. pageadmin网站制作 怎么验证sql用户名和密码的正确性
  2. yield 学习
  3. git log 与 git reflog 的 区别
  4. jmeter进行https协议的测试
  5. [HTML] 模板的用法
  6. [科普] 借助 everything 扩展教你屏蔽网址或转发网址
  7. java程序向hdfs中追加数据,异常以及解决方案
  8. 51.RocketMQ 顺序消费
  9. 基础概念——理解IP地址和域名
  10. E - Guess the Root 拉格朗日差值法+交互