Java多线程

  1. 创建线程方式:
    ①自定义线程类继承Thread类,重写run()方法,编写线程执行体,创建线程对象,调用start()开启线程。
public class TestThread1 extends Thread{
    @Override
    public void run(){
        for (int i = 0; i < 200; i++) {
            System.out.println("run"+i);
        }
    }
    public static void main(String[] args){
        TestThread1 testThread1=new TestThread1();
        testThread1.start();
        for (int i = 0; i < 200; i++) {
            System.out.println("主方法"+i);
        }
    }
}

②自定义线程类实现Runable类,重写run()方法,编写线程执行体,创建线程对象,通过线程对象开启线程代理。
启动线程:传入目标对象+Thread对象.start()
优点:方便一个对象被多个线程使用

TestThread1 testThread1=new TestThread1();
        new Thread(testThread1, "小明").start();
        new Thread(testThread1, "小红").start();
        new Thread(testThread1, "小绿").start();
  1. 线程开启不一定立即执行,由CPU调度执行;
    (代码块1可体现此点,主次线程交替执行)
    main()方法中是主线程,run()方法中是次线程,调用start()方法时主次线程由cpu调度交替执行,调用run()方法时则是次方法中的线程先执行。
  2. 静态内部类、局部内部类、匿名内部类、lambda表达式
  3. 线程分为用户线程和守护线程(daemon),虚拟机必须确保用户线程执行完毕(main是用户线程),不用等待守护线程执行完毕(如后台记录操作日志,监控内存,垃圾回收等待)。
  4. 线程的优先级越高,越有可能先被执行。
  5. wait、notify、notifyAll的使用
class Message {
private String title;
private String content;
private boolean flag = true;
// flag == true:表示可以生产,但是不能取走
// flag == false:表示可以取走,但是不能生产
public synchronized void set(String title, String content) {
if (this.flag == false) { // 已经生产过了,不能生产
try {
super.wait();// 等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.title = title;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.content = content;
this.flag = false; // 已经生产完成,修改标志位
super.notify(); // 唤醒等待线程
}
public synchronized void get() {
if (this.flag == true) { // 未生产,不能取try {
super.wait(); // 等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.title + " --> " + this.content);
this.flag = true; // 已经取走了,可以继续生产
super.notify(); // 唤醒等待线程
}
// setter、getter略
}

super.wait()、super.notify().之所以要用super是因为wait、notify、notifyAll是Object类的方法,而Object类是所有类的父类。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值