java线程之一:线程的生命周期

1: 先说基本概念:线程状态 :新建状态 ------》就绪状态-----》运行状态----》死亡状态

   新建状态 :使用 new 关键字和 Thread 类或其子类建立一个线程对象后,该线程对象就处于新建状态

   就绪状态 :当线程对象调用了start()方法之后,该线程就进入就绪状态。就绪状态的线程处于就绪队列中,要等待JVM里线程调    度器的调度

   运行状态:如果就绪状态的线程获取 CPU 资源,就可以执行 run(),此时线程便处于运行状态。处于运行状态的线程最为复杂,它可以变为阻塞状态、就绪状态和死亡状态

    阻塞线程 : 线程调用 :sleep()  join()  wait()会变成阻塞状态

   死亡状态:任务执行完毕,或者调用线程stop()函数

2: 实际代码 (待完成)

     1: join函数的实现 :中断当前线程,将Cpu控制权交给,join线程,jion线程业务执行完毕,继续执行当前线程逻辑

public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("thread name  = " + Thread.currentThread().getName());
        Thread thread = new Thread(new MyRunnable(), "thread1");
        thread.start();
        for(int i = 0; i< 5 ; i++) {
            System.out.println("thread name  = " + Thread.currentThread().getName() + "--i = " + i);
            if (i == 2) {
                thread.join();
            }
        }
    }

    private static class MyRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println("thread name  = " + Thread.currentThread().getName());
            for (int j = 0 ; j<3;j++) {
                System.out.println("thread name  = " + Thread.currentThread().getName() + "--j = " + j);
            }
        }
    }
}

  sleep函数:阻塞当前线程,阻塞期间不释放锁,释放CPU资源

public class ThreadDemoForSleep {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("thread name  = " + Thread.currentThread().getName());
        Thread thread = new Thread(new MyRunnable(), "thread1");
        for (int i = 0; i < 100; i++) {
            System.out.println("thread name  = " + Thread.currentThread().getName() +
                    "--i = " + i + ", system time = " +
                    DateFormatUtils.formatDate(System.currentTimeMillis()));
            if (i == 2) {
                sleep(5000); // 休眠20s (主线程阻塞20s后自动唤醒,阻塞期间不释放cpu)
                thread.start();
            }
        }
    }
    private static class MyRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println("thread name  = " + Thread.currentThread().getName() +
                    ", system time = " + DateFormatUtils.formatDate(System.currentTimeMillis()));
            for (int j = 0; j < 3; j++) {
                System.out.println("thread name  = " + Thread.currentThread().getName() + "--j = " + j);
            }
        }
    }
}

wait : 因为wait需释放锁,所以必须在synchronized中使用(没有锁定可以释放吗?答 :没有锁时使用会抛出IllegalMonitorStateException , 不可用代用wait())

public class ThreadDemoForWait {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("main thread name  enter = " + Thread.currentThread().getName());
        MyThread thread = new MyThread( "waitThread");
        synchronized (thread) {
            // 启动 waitThread 线程
            thread.start();
            // 阻塞主线程,等待其他线程唤醒
            System.out.println("main thread name is waited");
            thread.wait();
            for (int j = 0; j < 3; j++) {
                System.out.println(" main thread notifyed = " +
                        Thread.currentThread().getName() + "--j = " + j);
            }
        }
    }

    private static class MyThread extends Thread {
        public MyThread(String threadName) {
            super(threadName);
        }
        @Override
        public void run() {
            synchronized (this) {
                System.out.println("waitThread name enter = " + Thread.currentThread().getName());
                for (int j = 0; j < 3; j++) {
                    System.out.println("thread name  = " + Thread.currentThread().getName() + "--j = " + j);
                }
                System.out.println("waitThread start sleep  = " +
                        DateFormatUtils.formatDate(System.currentTimeMillis()));
                try {
                    // 当前线程休眠2s
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 休眠时间结束,唤醒执行wait()函数的线程(主线程)
                System.out.println("waitThread sleep ended = " +
                        DateFormatUtils.formatDate(System.currentTimeMillis()));
                notify();
            }

        }
    }

3:流程图 (待完成)

4:项目实际用到了没有 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值