0
点赞
收藏
分享

微信扫一扫

【JAVA 多线程】

婉殇成长笔记 2022-04-29 阅读 31

目录

多线程

进程

  • 进程编号 PID:进程的身份标识。
  • 进程的状态:
  1. 新建状态
  2. 就绪状态
  3. 运行状态
  4. 阻塞状态
  5. 销毁状态
  • 执行优先级
  • 上下文:保存本次执行状态,以便下次继续执行,这个过程就是一个上下文。
  • 内存地址

线程

进程和线程的区别

区别1:

区别2:

区别3:

区别4:

区别5:

线程四种创建方式

  1. 继承 Thread 类创建线程
  2. 实现 Runnable 接口创建线程
  3. 实现 Callable 接口,通过 FutureTask 包装器来创建 Thread 线程
  4. 线程池:使用ExcutorService、Callable、Future 实现有返回结果的线程

使用继承 Thread 类创建线程

单线程与多线程

单线程

public class Thread02 {
    private String name;
    public Thread02() {
    }
    public Thread02(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private void run(){
        for (int i = 0; i < 5; i++) {
            // 获取当前线程的名字
            System.out.println(this.name+"执行了第"+i+"次");
        }
    }
    public void start(){
        run();
    }
    public static void main(String[] args) {
        Thread02 thread01 = new Thread02("线程01");
        Thread02 thread02 = new Thread02("线程02");
        thread01.start();
        thread02.start();
    }
}

线程01执行结束后线程02开始执行输出

线程01执行了第0次
线程01执行了第1次
线程01执行了第2次
线程01执行了第3次
线程01执行了第4次
线程02执行了第0次
线程02执行了第1次
线程02执行了第2次
线程02执行了第3次
线程02执行了第4

多线程

public class Thread01 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            // 获取当前线程的名字
            System.out.println(this.currentThread().getName()+"执行了第"+i+"次");
        }
    }
    public static void main(String[] args) {
        Thread01 thread01 = new Thread01();
        Thread01 thread02 = new Thread01();
        thread01.setName("线程01");
        thread02.setName("线程02");
        thread01.start();
        thread02.start();
    }
}

线程01和线程02交替执行输出

线程02执行了第0次
线程02执行了第1次
线程01执行了第0次
线程01执行了第1次
线程01执行了第2次
线程01执行了第3次
线程01执行了第4次
线程02执行了第2次
线程02执行了第3次
线程02执行了第4

主线程与子线程

主线程

public class Thread03 {
    public static void main(String[] args) {
        // 获取当前线程的名字
        System.out.println(Thread.currentThread().getName());
    }
}

输出

main

子线程

public class Thread03 extends Thread{
    @Override
    public void run() {
        // 获取当前线程的名字
        System.out.println(this.currentThread().getName());
    }
    public static void main(String[] args) {
        Thread03 thread03 = new Thread03();
        thread03.start();
    }
}

输出

Thread-0

操作线程的名字

public class Thread04 extends Thread{
    @Override
    public void run() {
        System.out.println(this.currentThread().getName());
    }
    public static void main(String[] args) {
        Thread.currentThread().setName("线程01");
        System.out.println(Thread.currentThread().getName());
        Thread04 thread = new Thread04();
        thread.setName("线程02");
        thread.start();
    }
}

输出

线程01
线程02

线程中断

public class Thread05 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                if(i == 5){
                    // 线程中断
                    Thread.currentThread().interrupt();
                }
                Thread.sleep(500);
                System.out.println(this.currentThread().getName()+">>> "+i);
            } catch (InterruptedException e) {
                // throw new RuntimeException(e);
                return;
            }
        }
    }
    public static void main(String[] args) {
        Thread05 thread05 = new Thread05();
        thread05.start();
    }
}

输出

Thread-0>>> 0
Thread-0>>> 1
Thread-0>>> 2
Thread-0>>> 3
Thread-0>>> 4

守护线程中断

public class Thread08 extends Thread{
    @SneakyThrows
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            Thread.sleep(500);
            System.out.println(this.currentThread().getName()+"今年"+(i+1)+"岁");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread.currentThread().setName("Jack");
        Thread08 thread08 = new Thread08();
        thread08.setName("Rose");
        // 守护线程
        thread08.setDaemon(true);
        thread08.start();
        for (int i = 0; i < 10; i++) {
            if (i == 5){
                break;
            }
            Thread.sleep(500);
            System.out.println(Thread.currentThread().getName()+"今年"+(i+1)+"岁");
        }
    }
}

输出

Rose今年1Jack今年1Jack今年2Rose今年2Jack今年3Rose今年3Jack今年4Rose今年4Jack今年5Rose今年5

join把指定的线程加入到当前线程

public class Thread10 extends Thread{
    private int num;
    @Override
    public void run() {
        System.out.println(this.getName()+"疯狂计算中");
        for (int i = 0; i < 50; i++) {
            num += i;
        }
        System.out.println(this.getName()+"疯狂计算结束");
    }
    public int getNum(){
        return num;
    }
    public static void main(String[] args) throws InterruptedException {
        Thread.currentThread().setName("经理");
        Thread10 thread10 = new Thread10();
        thread10.setName("小丽");
        System.out.println(Thread.currentThread().getName()+"说"+thread10.getName()+"呀!帮我算一个报表");
        thread10.start();
        // 把指定的线程加入到当前线程
        thread10.join();
        System.out.println(thread10.getName()+"计算结果>>> "+thread10.getNum());
    }
}

输出

经理说小丽呀!帮我算一个报表
小丽疯狂计算中
小丽疯狂计算结束
小丽计算结果>>> 1225

线程优先级

public class Thread11 extends Thread{
    public static void main(String[] args) {
        Thread11 thread01 = new Thread11();
        Thread11 thread02 = new Thread11();
        thread01.setName("线程01");
        thread02.setName("线程02");
        // 获取当前线程优先级别
        System.out.println(thread01.getName()+">>> "+thread01.getPriority());
        System.out.println(thread02.getName()+">>> "+thread02.getPriority());
        // 设置优先级
        thread01.setPriority(MAX_PRIORITY);
        thread02.setPriority(MIN_PRIORITY);
        System.out.println(thread01.getName()+">>> "+thread01.getPriority());
        System.out.println(thread02.getName()+">>> "+thread02.getPriority());
    }
}

输出

线程01>>> 5
线程02>>> 5
线程01>>> 10
线程02>>> 1

实现 Runnable 接口创建线程

Runnable接口

public class Thread03{
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println(Thread.currentThread().getName() + ">>> " + i);
                }
            }
        };
        Thread thread01 = new Thread(runnable);
        Thread thread02 = new Thread(runnable);
        Thread thread03 = new Thread(runnable);
        thread01.start();
        thread02.start();
        thread03.start();
    }
}

输出

Thread-2>>> 0
Thread-2>>> 1
Thread-2>>> 2
Thread-2>>> 3
Thread-2>>> 4
Thread-1>>> 0
Thread-1>>> 1
Thread-1>>> 2
Thread-1>>> 3
Thread-1>>> 4
Thread-0>>> 0
Thread-0>>> 1
Thread-0>>> 2
Thread-0>>> 3
Thread-0>>> 4
举报

相关推荐

0 条评论