函数式接口lambda

函数式接口 Lambda表达式

  • 函数式接口定义

    • 任何接口,如果只包含唯一一个抽象方法,那么它就是函数式接口

    • public interface Runnable{
          public abstract void run();
      }
      
  • 对于函数式接口,可以通过 Lambda 表达式来创建该接口的对象

    • package com.fjbc.test;
      
      import java.util.ArrayList;
      import java.util.List;
      
      public class Test {
          public static void main(String[] args) {
              ILike like = null;
              like = () -> {
                  System.out.println("i like lambda5");
              };
              like.like();
          }
      }
      //1.定义一个函数式接口
      interface ILike{
          void like();
      }
      

线程状态

image-20221206084033027

线程休眠_sleep

package com.fjbc.test;

public class Test倒计时{
    public static void main(String[] args){
        try {
            tenDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void tenDown() throws InterruptedException {
            int num = 10;
            while(true){
                Thread.sleep(1000);
                System.out.println(num--);
                if(num <= 0){
                    break;
                }
            }
        }
}

线程礼让—yield(让CPU重新调用,礼让不一定成功!看CPU心情)

线程强制执行—join

观测线程状态

img

package com.fjbc.多线程;

/**
 * 观察测试线程状态
 */
public class Demo21_ThreadState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("//");
        });
        //观察状态
        Thread.State state = thread.getState();
        System.out.println(state);
        //观察启动后
        thread.start();
        state = thread.getState();
        System.out.println(state);//Run
        while (state != Thread.State.TERMINATED) {//只要现成不终止,就一直输出状态
            Thread.sleep(100);
            state = thread.getState();//更新线程状态
            System.out.println(state);
        }
        //死亡后的线程不能再启动了,启动会报异常
        //thread.start();
    }
}

线程的优先级

  • 优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用了,还是得看CPU心情
/**
 * 线程优先级
 */
public class Demo22_ThreadPriority{
    public static void main(String[] args) {
        //主线程默认优先级
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());

        MyPriority myPriority = new MyPriority();
        Thread thread1 = new Thread(myPriority);
        Thread thread2 = new Thread(myPriority);
        Thread thread3 = new Thread(myPriority);
        Thread thread4 = new Thread(myPriority);
        Thread thread5 = new Thread(myPriority);

        //先设置优先级,再启动
        thread1.start();

        thread2.setPriority(1);
        thread2.start();

        thread3.setPriority(4);
        thread3.start();

        thread4.setPriority(Thread.MAX_PRIORITY);//MAX_PRIORITY=10
        thread4.start();

        thread5.setPriority(8);
        thread5.start();
    }
}
class MyPriority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
    }
}

守护线程(daemon)

  • 线程分为用户线程和守护线程

  • 虚拟机必须确保用户线程执行完毕

  • 虚拟机不用等待守护线程执行完毕

  • 如,后台记录操作日志,监控内存,垃圾回收等待

    • package com.fjbc.多线程;
      
      /**
       * 测试守护线程
       * 上帝守护你
       */
      public class Demo23_DaemonThread {
          public static void main(String[] args) {
              God god = new God();
              You you = new You();
      
              Thread thread = new Thread(god);
              //默认false表示是用户线程,正常的线程都是用户线程...
              thread.setDaemon(true);
              //上帝守护线程启动
              thread.start();
              //你 用户线程启动
              new Thread(you).start();
          }
      }
      
      //上帝
      class God implements Runnable{
          @Override
          public void run() {
              while (true){
                  System.out.println("上帝保佑着你");
              }
          }
      }
      
      //你
      class You implements Runnable{
          @Override
          public void run() {
              for (int i = 0; i < 36500; i++) {
                  System.out.println("你一生都开心的活着");
              }
              System.out.println("====goodbye!world====");
          }
      }
      
      
posted @ 2022-12-07 08:20  醉读付人心  阅读(24)  评论(0)    收藏  举报