zl程序教程

您现在的位置是:首页 >  Java

当前栏目

java多线程知识点总结

2023-04-18 12:30:47 时间

一、线程的三种创建方法

1-1 继承Thread

继承thread方法然后重写run方法,在用start开启线程。

代码实现:

Thread t = new Thread(){
            @Override
            public void run() {
                
            }
        };
        
        t.start();

1-2 实现Runnable接口(主要用于做共享任务)

实现Runnable接口重写run方法,将实现类的对象当参数传入创建Thread。

代码实现:

Thread t = new Thread(new Runnable() {
            public void run() {
                
            }
        });
        t.start();

1-3 实现Callable接口(用于获得线程任务的返回值配合FutureTask使用)

先实现Callable接口重写call方法,新建FutureTask将Callable的实现类当作参数传入,在创建Thread将FutureTask传入。FutureTask可以获得返回值。

代码实现:

FutureTask<String> stringFutureTask = new FutureTask<String>(new Callable<String>() {
            public String call() throws Exception {
                return "success";
            }
        });
        Thread t2 = new Thread(stringFutureTask);
        t2.start();
        String s = stringFutureTask.get();
        System.out.println(s);

1-4 线程的常用方法

设置线程优先级(提高概率,本质还是随机)范围1 - 10;

setPriority(int newPriority);

设置守护线程

setDaemon(true);

获取当前线程

Thread.currentThread();

二、创建线程池的俩种方法

1-1 Executors

创建默认线程池

Executors.newCachedThreadPool();

创建最大线程线程池

Executors.newFixedThreadPool(10);

1-2 ThreadPoolExecutors

构造方法

编辑

三、线程安全的三种方法

1-1 同步代码块

synchronized(锁对象){

}

1-2 同步代码方法

修饰符 synchronized 返回值类型 方法名(方法参数) { 
    方法体;
}

1-3 Lock对象

Lock lock = new Lock();

lock.lock();

lock.unlock();

四、线程的生命周期

编辑

 

五、消费者跟生产者

消费者:消费数据

生产者:生产数据

六、例题


public class ThreadDemo {
    public static void main(String[] args) {
        Cooker cooker = new Cooker();
        cooker.start();
        Footer footer = new Footer();
        footer.start();
    }
}

class Desk {

    public static boolean flag = false;

    public static final Object lock = new Object();

}

class Cooker extends Thread {

    @Override
    public void run() {
        int count = 0;
        while (true) {
            synchronized (Desk.lock) {
                if (!Desk.flag) {
                    System.out.println("生成了一个汉堡包");
                    Desk.flag = true;
                    Desk.lock.notifyAll();
                    count++;
                    if (count == 5) break;
                } else {
                    try {
                        Desk.lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

class Footer extends Thread {

    @Override
    public void run() {
        while (true) {
            synchronized (Desk.lock) {
                if (Desk.flag) {
                    System.out.println("吃了一个汉堡包");
                    Desk.flag = false;
                    Desk.lock.notifyAll();
                } else {
                    try {
                        Desk.lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

运行结果:

编辑