java多线程(二) sleep(),yield(),wait(), interrupt()方法

Thread对象调用start方法后,线程处于可运行状态,等待cpu分配给该线程。


sleep(),yield()方法都可以让正在运行的线程,放弃cpu

sleep(),在睡眠指定的时间后,自动变为可运行状态

yield(),让当前运行线程回到可运行性状态,使得有相同优先级的线程有机会执行。

 wait( ): 类似sleep( ), 不同的是,wait( )会先释放锁住的对象,然后再执行等待的动作。注意,这个函数属于Object类。另外,由于wait( )所等待的对象必须先锁住,因此,它只能用在同步化程序段或者同步化方法内,否则,会抛出异常IllegalMonitorStateException.

在看一个网上找到的例子

package lock;
public class Pool {
    public Connection get(){
        synchronized (this) {
            if(free>0){
                free--;
            }else{
                this.wait();
            }
            return cacheConnection.poll();
 
        }
    }
    public void close(Connection conn){
        synchronized (this) {
            free++;
            cacheConnection.offer(conn);
            this.notifyAll();
        }
    }
}

interrupt()也是线程调用的

这里参考网上别人写的一段代码

class ATask implements Runnable{  
  
    private double d = 0.0;  
      
    public void run() {  
        //死循环执行打印"I am running!" 和做消耗时间的浮点计算  
        try {  
            while (true) {  
                System.out.println("I am running!");  
                  
                for (int i = 0; i < 900000; i++) {  
                    d =  d + (Math.PI + Math.E) / d;  
                }  
                //休眠一断时间,中断时会抛出InterruptedException  
                Thread.sleep(50);  
            }  
        } catch (InterruptedException e) {  
            System.out.println("ATask.run() interrupted!");  
        }  
    }  
}  
在这儿需要捕获InterruptedException异常,就是线程要去捕获中断异常

也可以去检查是否有中断

class ATask implements Runnable{  
  
    private double d = 0.0;  
      
    public void run() {  
          
        //检查程序是否发生中断  
        while (!Thread.interrupted()) {  
            System.out.println("I am running!");  
  
            for (int i = 0; i < 900000; i++) {  
                d = d + (Math.PI + Math.E) / d;  
            }  
        }  
  
        System.out.println("ATask.run() interrupted!");  
    }  
} 

可以结合两个一起用

class ATask implements Runnable{  
  
    private double d = 0.0;  
      
    public void run() {  
          
        try {  
        //检查程序是否发生中断  
        while (!Thread.interrupted()) {  
            System.out.println("I am running!");  
            //point1 before sleep  
            Thread.sleep(20);  
            //point2 after sleep  
            System.out.println("Calculating");  
            for (int i = 0; i < 900000; i++) {  
                d = d + (Math.PI + Math.E) / d;  
            }  
        }  
          
        } catch (InterruptedException e) {  
            System.out.println("Exiting by Exception");  
        }  
          
        System.out.println("ATask.run() interrupted!");  
    }  
}  

主函数为

public class InterruptTaskTest {  
      
    public static void main(String[] args) throws Exception{  
        //将任务交给一个线程执行  
        Thread t = new Thread(new ATask());  
        t.start();  
          
        //运行一断时间中断线程  
        Thread.sleep(100);  
        System.out.println("****************************");  
        System.out.println("Interrupted Thread!");  
        System.out.println("****************************");  
        t.interrupt();  
    }  
}   




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值