javase-thread-210312-01
- Lamda表达式
- 线程状态
- 线程停止 线程休眠 线程暂停
- 线程强制执行 线程状态 线程优先级 守护线程
Lamda表达式
可以避免匿名内部类定义过多
可以让代码看起来更简洁
去掉了一些没有意义的代码,只留下核心的逻辑
Lamda表达式的使用必须是一种函数式接口(Functional Interface),,
Java8才有Lamda表达式
函数式接口的定义:
任何接口,,接口中只包含唯一的一个抽象方法,(只有一个抽象的方法的接口称为函数式接口)
对于函数式接口,我们可以通过Lamda表达式来创建该接口的对象
Demo01
public class DemoLamda01 {
// 2.方式二 静态内部类
static class ILike02 implements Like{
@Override
public void lamda() {
System.out.println("I like you!!!----静态内部类");
}
}
public static void main(String[] args) {
// 方式一 实现接口
Like like = new ILike01();
like.lamda();
// 方式二 静态内部类
like = new ILike02();
like.lamda();
// 3.方式三 局部内部类
class ILike03 implements Like{
@Override
public void lamda() {
System.out.println("I like you!!!----局部内部类");
}
}
like = new ILike03();
like.lamda();
// 4.方式四 匿名内部类,,没有类名,,必须依靠父类或者是接口
like = new Like() {
@Override
public void lamda() {
System.out.println("I like you!!!----匿名内部类");
}
};
like.lamda();
// 5.方式五 lamda表达式
like = () -> {
System.out.println("I like you!!!----lamda表达式");
};
like.lamda();
}
}
// 定义一个函数式接口(只有唯一的一个抽象方法)
interface Like{
void lamda();
}
// 1.方式一 实现接口
class ILike01 implements Like{
@Override
public void lamda() {
System.out.println("I like you!!!----实现接口方式");
}
}
Demo02
package bgy01_lamda;
/**
注意:
1. lamda表达式只能有一行代码的情况下才能简化为一行(简化3),,,如有多行,必须加上花括号
2. 使用lamada表达式的前提,接口为函数式接口(接口中只有一个抽象方法)
3. 多个参数的也可以去掉参数类型,要去都去掉,,,必须加上括号
*/
public class DemoLamda02 {
public static void main(String[] args) {
Love love = null;
Love01 love01 = null;
Love02 love02 = null;
// 简化1,,阐述类型
love = (a) -> {
System.out.println("哈哈哈哈哈"+a);
};
love01 = (a,b) -> {
System.out.println(a + "+" + b + "=" + (a+b));
};
love02 = (int a,int b,int c) -> {
System.out.println(a + "+" + b + "+" +c + "=" + (a+b+c));
};
love.iLove(999);
love01.iLove(1,2);
love02.iLove(1,2,3);
// 简化2,,简化括号
love = a -> {
System.out.println("哈哈哈哈哈"+a);
};
love.iLove(888);
// 简化3,,去掉花括号
love = a -> System.out.println("哈哈哈哈哈"+a);
love.iLove(777);
}
}
interface Love{
void iLove(int a);
}
interface Love01{
void iLove(int a,int b);
}
interface Love02{
void iLove(int a,int b,int c);
}
线程状态
- 创建状态
- 就绪状态
- 阻塞状态
- 运行状态
- 死亡状态
线程停止
package bgy02;
/*
JDK 提供的方法都过时了,,,还是自己定义的比较好
*/
public class Demo01_ThreadStop implements Runnable{
// 设置表示符
private boolean flag = true;
@Override
public void run() {
int i = 0;
while(flag){
System.out.println(Thread.currentThread().getName()+" is run....."+i++);
}
}
// 停止线程
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
Demo01_ThreadStop demo = new Demo01_ThreadStop();
new Thread(demo,"会停止线程").start();
for (int i = 0; i < 150; i++) {
System.out.println(Thread.currentThread().getName()+" is run....."+i);
if(i == 100){
// 调用stop方法,让线程停止
demo.stop(); // 让 “会停止线程” 这个线程停止
System.out.println("线程停止");
}
}
}
}
线程休眠
1000毫秒=1秒
sleep() 方法,让线程休眠,,(毫秒)
阻塞当前线程
存在异常,,InterruptedException
到达时间后线程进入就绪状态
每一个对象都有一把锁,,,sleep方法不会释放锁
sleep方法可以模拟网络延时,倒计时等。。。
倒计时
public class Demo02_ThreadSleep01 implements Runnable{
@Override
public void run() {
try {
daoJiShi();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void daoJiShi() throws InterruptedException {
int num = 10;
while (true){
Thread.sleep(1000);
System.out.println("倒计时:"+num--);
if(num < 0){
break;
}
}
}
public static void main(String[] args) {
// try {
// daoJiShi();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
Demo02_ThreadSleep01 demo=new Demo02_ThreadSleep01();
new Thread(demo).start();
}
}
获取当前时间
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo02_ThreadSleep02 implements Runnable{
@Override
public void run() {
}
public static void main(String[] args) {
Date date = new Date(System.currentTimeMillis()); // 获取当前时间
while (true){
try {
Thread.sleep(1000);
System.out.println("当前时间为:" + new SimpleDateFormat("HH:mm:ss").format(date));
date = new Date(System.currentTimeMillis()); // 更新时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程暂停
yield()
礼让线程,让当前正在执行的线程暂停,但不阻塞
将线程从运行状态转为就绪状态
让CPU重新调度,礼让不一定成功
public class Demo03_ThreadYield {
public static void main(String[] args) {
TestYield test01=new TestYield();
new Thread(test01,"a").start();
new Thread(test01,"b").start();
}
}
class TestYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" is run");
Thread.yield();
System.out.println(Thread.currentThread().getName()+" is yield");
}
}
礼让不成功:
礼让成功:
线程强制执行
join()
join方法合并线程,等此线程执行完成后,在执行其他线程,其他线程阻塞
(------------插队---------------)
public class Demo04_ThreadJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("大佬来了哟!!!");
}
}
public static void main(String[] args) throws InterruptedException {
Demo04_ThreadJoin demo=new Demo04_ThreadJoin();
Thread thread = new Thread(demo);
thread.start();;
for (int i = 0; i < 50; i++) {
if (i == 20){
thread.join();
}
System.out.println(i);
}
}
}
观察线程状态
public class Demo05_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("结束");
});
// 启动前: NEW
Thread.State state = thread.getState();
System.out.println(state);
// 启动后: RUNNABLE
thread.start();
state = thread.getState();
System.out.println(state);
// 线程不终止就一直执行
while (state != Thread.State.TERMINATED){
Thread.sleep(1000); // 每隔1秒刷新一下状态
state = thread.getState(); // 更新线程状态
System.out.println(state);
}
}
}
线程优先级
java中有一个线程调度器来监控程序中启动都进入就绪状态的所有线程,
线程调度器按优先级决定应该调度那个线程来执行。
优先级只是意味着调度的概率低,并不是优先级低的就不会被调用了(看CPU调度)
优先级的设定在 start() 方法前
线程优先级,用数字表示,范围:1~10
Thread.MIN_PRIOPITY = 1
Thread.NORM_PRIORITY = 5
Thread.MAX_PRIORITY = 10
public class Demo06{
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + "---->" + Thread.currentThread().getPriority());
Test06 test06 = new Test06();
Thread t1 = new Thread(test06);
Thread t2 = new Thread(test06);
Thread t3 = new Thread(test06);
Thread t4 = new Thread(test06);
Thread t5= new Thread(test06);
// 默认为5
t1.start();
//
t2.setPriority(1);
t2.start();
//
t3.setPriority(3);
t3.start();
// 设置最大,,Thread.MAX_PRIORITY
t4.setPriority(Thread.MAX_PRIORITY);
t4.start();
//
t5.setPriority(8);
t5.start();
}
}
class Test06 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "---->" + Thread.currentThread().getPriority());
}
}
守护线程
daemon 守护线程
线程分为,,用户线程(eq:main()),,守护线程(eq:gc())
虚拟机必须确保用户线程执行完毕,,虚拟机不用等待守护线程执行完成
public class Demo07 {
public static void main(String[] args) {
God god = new God();
People people = new People();
Thread thread = new Thread(god);
// 默认为false,,即用户线程,,普通的线程都是用户线程
thread.setDaemon(true);
thread.start();
new Thread(people).start();
}
}
// 充当守护线程 虚拟机不用等待守护线程执行完成
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝一直保护着你");
}
}
}
// 充当用户线程 虚拟机必须确保用户线程执行完毕
class People implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36000; i++) {
System.out.println("已出生"+i+"天");
}
System.out.println("you over!!!");
}
}