多线程(1)多线程的创建方法

1.java创建多线程的几种方法

在 Java 中的线程是由 Java 虚拟机来管理和调度的,它们并不直接映射到操作系统的原生线程。Java 线程是由 JVM 在后台使用一种称为“轻量级进程”的概念来实现的。

1.通过MyThread继承Thread

class MyThread extends Thread{
    @Override
    public void run(){
        while (true){
            System.out.println("Hello Thread");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Demo1 {


    public static void main(String[] args) {
        Thread thread1=new MyThread();
        thread1.start();
        while (true){
            System.out.println("Hello main");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
  • 这里创建了一个MyThrea类继承Thread类并且重写run方法,并且每次休眠两秒(Thread.sleep() 方法用于让当前线程暂停执行指定的时间(以毫秒为单位)。当调用 Thread.sleep() 方法时,当前线程会进入阻塞状态,并暂停执行指定的时间长度,然后重新进入就绪状态等待 CPU 时间片。),并且打开了main方法的线程,同时也不断打印Hello Main。
  • 这里就是main和mythread两个线程同时执行。但是执行的时候并不一定谁先谁后

2.通过实现Runnable接口

class MyRunnable implements Runnable{
    @Override
    public void run(){
        while (true){
            System.out.println("Hello Thread");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Demo1 {


    public static void main(String[] args) {
        Thread thread1=new Thread(new MyRunnable());
        thread1.start();
        while (true){
            System.out.println("Hello main");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

这段代码定义了一个实现了 Runnable 接口的MyRunnable类重写了run方法

3.使用匿名内部类实际上和1方法一样

public class Demo2 {
    public static void main(String[] args) {
        Thread thread=new Thread(){
            public void run(){
                while (true) {
                    System.out.println("Hello Thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        thread.start();
        while (true){
            System.out.println("Hello Main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

这种情况我们通常在只使用一次的的类中。

4.使用匿名内部类创建new Runnable对象

public class Demo3 {
    public static void main(String[] args) {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("Hello Thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        thread.start();
        while (true){
            System.out.println("Hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

5.最后一个也是最常用的Lambda表达式

public class Demo4 {
    public static void main(String[] args) {
        Thread thread=new Thread(()->{
            while (true){
                System.out.println("Hello Thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        while (true){
            System.out.println("Hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

2.Thread类以及常见的方法

1. Thread类的常见构造方法

2. Thread类的几个常见属性

  • 一个线程只有一个id
  • 线程通常有(就绪,阻塞,运行态)
  • 优先级别更高的就会被先调用到
  • 后台线程是一种在后台运行的线程。它的主要特点是当程序中所有非守护线程都结束运行时,守护线程会自动结束,不管守护线程的任务是否完成。守护线程就像是为其他线程提供服务的线程,它的存在是为了辅助其他线程的执行,而不是程序的核心部分。
1.getName()方法
public class Demo5 {
    public static void main(String[] args) {
        Thread thread=new Thread(()->{
            System.out.println(Thread.currentThread().getName());
        },"线程0");
        thread.start();
    }
}

实际上我们的getname方法就是获得线程名字,线程名字也可以自己设置

2. setDaemon() 和 isDaemon()

设置守护线程

public class Demo5 {
    public static void main(String[] args) {
        Thread thread=new Thread(()->{
            for (int i = 0; i <5 ; i++) {
                System.out.println(Thread.currentThread().getName());
            }

        });
        thread.setDaemon(true);
        thread.start();
        



    }
}

此时我们就会发现这个线程被我们设置为了守护线程,而main线程作为主线程,main线程跑完了thread线程一定会结束。因为thread是守护线程。


public class Demo3 {
    public static void main(String[] args) {
        //isAlive() 判断当前线程是否存活
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
 
        t.start();
 
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
 
        System.out.println(t.isAlive());
    }

在这段代码中,通过 t.isAlive() 方法可以判断线程 t 是否处于存活状态。具体来说:

创建了一个线程 t,让它在启动后休眠 2000 毫秒(2秒)。主线程休眠 3000 毫秒(3秒),等待线程 t 执行完成。在主线程休眠结束后,通过 t.isAlive() 方法检查线程 t 是否仍然存活,即线程是否仍在执行中。最后返回false。

3.中断线程
public class Demo6 {
    public static void main(String[] args) {
        Thread thread=new Thread(()->{
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("我是一个线程 ");
            }
        });
        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        thread.interrupt();
    }
}

在这里我们更建议使用线程自带的方法,通过修改thread的interrupt方法来修改线程是否进行,通过interrupt方法来通知线程结束。如果是正在睡眠的情况下,就会抛出异常,使得程序终止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值