线程的控制

文章介绍了Java中线程控制的几个关键方法,包括sleep()用于让线程进入睡眠状态,join()使线程进入阻塞等待,interrupt()中断线程,isAlive()检查线程是否存活,以及yield()进行线程让步。同时,通过示例代码展示了如何在实践中使用这些方法,特别是join()方法的使用,使得主线程等待某个线程执行完毕后再继续执行。
摘要由CSDN通过智能技术生成
线程的控制
sleep(Long millis):使当前线程进入睡眠状态(是一个静态方法)

join() / join(Long millis):是当前线程进入阻塞状态(是一个实例方法)

interrupt():中断阻塞状态的线程

isAlive():判断当前线程是否处于存活状态

yield():线程让步

线程的调度:

  

package Thread;

public class HelloThread2 implements Runnable{
    int i=0;
    @Override
    public void run() {
        /*try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
*/
        while (i<=100){
            try {
                Thread.sleep(1000); 
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+i++);
        }
    }
}
package Thread;
/*
一、线程的控制
sleep(Long millis):使当前线程进入睡眠状态(是一个静态方法)
 */
public class ThreadTest5 {
    public static void main(String[] args) {
        HelloThread2 ht=new HelloThread2();
        Thread t=new Thread(ht);
        t.start();

    }
}

 

package Thread;
/*
一、线程的控制
sleep(Long millis):使当前线程进入睡眠状态(是一个静态方法)

join() / join(Long millis):是当前线程进入阻塞状态(是一个实例方法)

interrupt():中断阻塞状态的线程

isAlive():判断当前线程是否处于存活状态


 */
public class ThreadTest5 {
    public static void main(String[] args) {
        HelloThread2 ht=new HelloThread2();
        Thread t=new Thread(ht);
        t.start();
        //t.interrupt();
        /*while (t.isAlive()){
            t.interrupt();
        }*/

        /*try {
            Thread.sleep(3000);//主线程main睡了3000
        } catch (InterruptedException e) {
        }*/
        try {
            t.join();//main阻塞了;t向main申请执行权,优先执行,若无参数则当前线程执行完再执行其它,有参数则是先执行  时间,再执行main线程
        } catch (InterruptedException e) {
        }
        for (int i = 100; i <200 ; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}
package Thread;

public class HelloThread2 implements Runnable{
    int i=0;
    @Override
    public void run() {
        /*try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
           // e.printStackTrace();//一般不作异常处理,异常信息就不会被打印
        }
*/
        while (i<=100){
            /*try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }*/
            System.out.println(Thread.currentThread().getName()+":"+i++);
        }
    }
}

cpu谁抢到谁执行 

线程让步:主动释放cpu资源,让其他线程有更多的机会执行

 main线程执行的效率降低了

Exer:

编写程序,在main方法中创建一个线程。线程每隔一定时间(200ms以内的随机时间)产生一个0-100之间的随机整数,打印后将该整数放到集合中;
共产生100个整数,全部产生后,睡眠30秒,然后将集合内容打印输出;
在main线程中,唤醒上述睡眠的线程,使其尽快打印集合内容。

package Exer;
/*
编写程序,在main方法中创建一个线程。线程每隔一定时间(200ms以内的随机时间)产生一个0-100之间的随机整数,打印后将该整数放到集合中;
共产生100个整数,全部产生后,睡眠30秒,然后将集合内容打印输出;
在main线程中,唤醒上述睡眠的线程,使其尽快打印集合内容。
 */
public class ThreadExer3 {
    public static void main(String[] args) {
        HelloThread5 ht=new HelloThread5();
        Thread t=new Thread(ht);
        t.start();
        while (t.isAlive()){
            t.interrupt();
        }
    }
}
package Exer;

import java.util.ArrayList;
import java.util.List;

public class HelloThread5 implements Runnable{

    @Override
    public void run() {
        List<Integer> list=new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            int millis = (int) Math.random() * 200;

            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
            }
            int num = (int) Math.random() * 100;
            System.out.println(num);
            list.add(num);
        }
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
        }
        for (Integer num:list) {
            System.out.println("--"+num);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值