java wait join_JAVA多线程---wait() & join()

题外话:

interrupt()方法  并不能中断一个正常运行的线程!!!

class myThread extends Thread{

@Override

public void run(){

for (int i = 0; i < 1000; i++) {

System.out.println(i);

}

}

}

public class waitTest {

public static void main(String[] args) throws Exception{

Thread t =new myThread();

t.start();

t.interrupt();

System.out.println("mark");

}

}

输出:

mark

......

i=999

public class waitTest {

public static void main(String[] args) throws Exception{

Thread.currentThread().interrupt();

System.out.println("是否停止1?" + Thread.interrupted());

System.out.println("是否停止2?" + Thread.interrupted());

System.out.println("end!");

}

}

此时的输出为

是否停止1?true

是否停止2?false

end!

interrupt虽然不能中断一个正常执行的线程,但是会将目标线程的中断状态置为true   而interrupted用于测试当前线程的中断状态,执行后将状态表示置为false

而且interrupt只能中断一个正处于‘‘异常’’状态的线程

class myThread extends Thread{

@Override

public void run(){

for (int i = 0; i < 1000; i++) {

System.out.println(i);

if(i==800){

try{

Thread.sleep(1000);

}catch (Exception e){

System.out.println(i+"$$");

e.printStackTrace();

System.out.println(i+"^^");

}

}

}

}

}

public class waitTest {

public static void main(String[] args) throws Exception{

Thread t =new myThread();

t.start();

t.interrupt();

System.out.println("end");

}

}

输出为:

end    //说明interrupt已经发送

0

...

800

800$$   //说明确实是在i=800 处抛出异常  不过异常信息打印的稍有延迟???

800^^

801

912

java.lang.InterruptedException: sleep interrupted

913

914

at java.lang.Thread.sleep(Native Method)

915

at luyudepackage.myThread.run(waitTest.java:22)

...

999

说明interrupt方法可以提前发送 将目标线程中断状态置为true 当目标线程进入sleep状态时 抛出异常

join()方法

具体用法http://www.cnblogs.com/luyu1993/p/7017927.html

这里看下它的源码

public final synchronized void join(long millis)

throws InterruptedException {

long base = System.currentTimeMillis();

long now = 0;

if (millis < 0) {

throw new IllegalArgumentException("timeout value is negative");

}

if (millis == 0) {

while (isAlive()) {

wait(0);

}

} else {

while (isAlive()) {

long delay = millis - now;

if (delay <= 0) {

break;

}

wait(delay);

now = System.currentTimeMillis() - base;

}

}

}

join()内部调用的是wait()  注意 在调用wait()之前,线程必须获得该对象的锁,因此只能在同步方法/同步代码块中调用wait()方法。可以看见join()方法是synchronized的 所以没问题

看下面这段代码

1 class myThread extendsThread{2 @Override3 public voidrun(){4 for (int i = 0; i < 1000; i++) {5 System.out.println(i);6 }7 }8 }9 public classwaitTest {10

11 public static void main(String[] args) throwsException{12 Thread t =newmyThread();13 t.start();14 Thread.currentThread().wait();15 System.out.println("mark");16 }17

18 }

此时程序会报错  不在同步方法/同步代码块 中调用wait()方法的错误!!!

6e53845756acd0efbcbbfe91535f73bc.png

多次start同一个线程

b3ff781fb7c0ff14588672c3cc9fff69.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值