1.多线程快速入门

2020-09-04  本文已影响0人  okhoogh

线程与进程区别

总结:进程是所有线程的集合,每一个线程是进程中的一条执行路径。

为什么要使用多线程

多线程的好处提高程序的效率。

多线程应用场景?

答:主要能体现到多线程提高程序效率。
举例: 迅雷多线程下载、分批发送短信等。

多线程创建方式

1. 继承Thread类 重写run方法

class CreateThread extends Thread {
    // run方法中编写 多线程需要执行的代码
    public void run() {
        for (int i = 0; i< 10; i++) {
            System.out.println("i:" + i);
        }
    }
}
public class ThreadDemo {

    public static void main(String[] args) {
        System.out.println("-----多线程创建开始-----");
        // 1.创建一个线程
        CreateThread createThread = new CreateThread();
        // 2.开始执行线程 注意 开启线程不是调用run方法,而是start方法
        System.out.println("-----多线程创建启动-----");
        createThread.start();
        System.out.println("-----多线程创建结束-----");
    }

}

调用start方法后,代码并没有从上往下执行,而是有一条新的执行分枝

2. 实现Runnable接口,重写run方法

class CreateRunnable implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i< 10; i++) {
            System.out.println("i:" + i);
        }
    }

}

public class ThreadDemo2 {
    public static void main(String[] args) {
        System.out.println("-----多线程创建开始-----");
        // 1.创建一个线程
        CreateRunnable createThread = new CreateRunnable();
        // 2.开始执行线程 注意 开启线程不是调用run方法,而是start方法
        System.out.println("-----多线程创建启动-----");
        Thread thread = new Thread(createThread);
        thread.start();
        System.out.println("-----多线程创建结束-----");
    }
}

3. 使用匿名内部类方式

System.out.println("-----多线程创建开始-----");
Thread thread = new Thread(new Runnable() {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("i:" + i);
        }
    }
});
thread.start();
System.out.println("-----多线程创建结束-----");

使用继承Thread类还是使用实现Runnable接口好?

使用实现实现Runnable接口好,原因实现了接口还可以继续继承,继承了类不能再继承。

启动线程是使用调用start方法还是run方法?

注意开启线程不是调用run方法,而是start方法


常用线程api方法
start() 启动线程
currentThread() 获取当前线程对象
getID() 获取当前线程ID Thread-编号 该编号从0开始
getName() 获取当前线程名称
sleep(long mill) 休眠线程
Stop() 停止线程
常用线程构造函数
Thread() 分配一个新的 Thread 对象
Thread(String name) 分配一个新的 Thread对象,具有指定的 name
Thread(Runable r) 分配一个新的 Thread对象
Thread(Runable r, String name) 分配一个新的 Thread对象,具有指定的 name

守护线程

Java中有两种线程,一种是用户线程,另一种是守护线程。
用户线程是指用户自定义创建的线程,主线程停止,用户线程不会停止
守护线程当进程不存在或主线程停止,守护线程也会被停止。
使用setDaemon(true)方法设置为守护线程

public class DaemonThread {
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    System.out.println("我是子线程...");
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(100);
            } catch (Exception e) {

            }
            System.out.println("我是主线程");
        }
        System.out.println("主线程执行完毕!");
    }
}

多线程运行状态

线程从创建、运行到结束总是处于下面五个状态之一:新建状态、就绪状态、运行状态、阻塞状态及死亡状态。

新建状态

就绪状态

运行状态

阻塞状态

线程运行过程中,可能由于各种原因进入阻塞状态:

死亡状态

有两个原因会导致线程死亡:

join()方法作用

join作用是让其他线程变为等待

t1.join();// 让其他线程变为等待,直到当前t1线程执行完毕,才释放。

创建一个线程,子线程执行完毕后,主线程才能执行。

class JoinThread implements Runnable {

    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "---i:" + i);
        }
    }
}

public class JoinThreadDemo {

    public static void main(String[] args) {
        JoinThread joinThread = new JoinThread();
        Thread t1 = new Thread(joinThread);
        Thread t2 = new Thread(joinThread);
        t1.start();
        t2.start();
        try {
            //其他线程变为等待状态,等t1线程执行完成之后才能执行join方法。
            t1.join();
        } catch (Exception e) {

        }
        for (int i = 0; i < 100; i++) {
            System.out.println("main ---i:" + i);
        }
    }
}

优先级

现代操作系统基本采用时分的形式调度运行的线程,线程分配得到的时间片的多少决定了线程使用处理器资源的多少,也对应了线程优先级这个概念。在JAVA线程中,通过一个int priority来控制优先级,范围为1-10,其中10最高,默认值为5。下面是源码(基于1.8)中关于priority的一些量和方法。

class PrioritytThread implements Runnable {

    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().toString() + "---i:" + i);
        }
    }
}

public class ThreadDemo4 {

    public static void main(String[] args) {
        PrioritytThread prioritytThread = new PrioritytThread();
        Thread t1 = new Thread(prioritytThread);
        Thread t2 = new Thread(prioritytThread);
        t1.start();
        // 注意设置了优先级, 不代表每次都一定会被执行。 只是CPU调度会有限分配
        t1.setPriority(10);
        t2.start();

    }
}

yield方法

结论:大多数情况下,yield()将导致线程从运行状态转到可运行状态,但有可能没有效果。

多线程分批处理数据

需求:目前有10万个用户,现在需要做活动,给每一个用户发送一条祝福短信。
为了提高数程序的效率,请使用多线程技术分批发送据。
每开一个线程,都会占用CPU资源
服务器(电脑)配置 CPU 核数

实体类
public class UserEntity {
    private String userId;
    private String userName;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
建立多线程UserThread 执行发送短信&&初始化数据
class UserThread extends Thread {
    private List<UserEntity> list;

    /**
     * 通过构造函数 传入每个线程需要执行的发送短信内容
     *
     * @param list
     */
    public UserThread(List<UserEntity> list) {
        this.list = list;
    }

    public void run() {
        for (UserEntity userEntity : list) {
            System.out.println("threadName:" + Thread.currentThread().getName() 
                                              + "-学员编号:" + userEntity.getUserId()
                                              + "---学员名称:" + userEntity.getUserName());
            // 调用发送短信具体代码
        }
    }

    public static List<UserEntity> init() {
        List<UserEntity> list = new ArrayList<UserEntity>();
        for (int i = 1; i <= 140; i++) {
            UserEntity userEntity = new UserEntity();
            userEntity.setUserId("userId" + i);
            userEntity.setUserName("userName" + i);
            list.add(userEntity);
        }
        return list;

    }
}
计算分页工具类
public class ListUtils {
    static public<T> List<List<T>> splitList(List<T> list, int pageSize) {
        int listSize = list.size();
        int page = (listSize + (pageSize - 1)) / pageSize;
        List<List<T>>listArray = new ArrayList<List<T>>();
        for (int i = 0; i<page; i++) {
            List<T>subList = new ArrayList<T>();
            for (int j = 0; j<listSize; j++) {
                int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
                if (pageIndex == (i + 1)) {
                    subList.add(list.get(j));
                }
                if ((j + 1) == ((j + 1) * pageSize)) {
                    break;
                }
            }
            listArray.add(subList);
        }
        return listArray;
    }
}
实现发送短信
class test {
    public static void main(String[] args) {
        // 1.初始化用户数据
        List<UserEntity> listUserEntity = init();
        // 2.计算创建创建多少个线程并且每一个线程需要执行“分批发送短信用户”
        // 每一个线程分批跑多少
        intuserThreadPage = 50;
        // 计算所有线程数
        List<List<UserEntity>> splitUserList = ListUtils.splitList(listUserEntity, userThreadPage);
        intthreadSaze = splitUserList.size();
        for (inti = 0; i < threadSaze; i++) {
            List<UserEntity> list = splitUserList.get(i);
            UserThread userThread = new UserThread(list);
            // 3.执行任务发送短信
            userThread.start();
        }

    }
}

总结

1.进程与线程的区别?
答:进程是所有线程的集合,每一个线程是进程中的一条执行路径,线程只是一条执行路径。
2.为什么要用多线程?
答:提高程序效率
3.多线程创建方式?
答:继承Thread或Runnable 接口。
4.是继承Thread类好还是实现Runnable接口好?
答:Runnable接口好,因为实现了接口还可以继续继承。继承Thread类不能再继承。
5.你在哪里用到了多线程?
答:主要能体现到多线程提高程序效率。
举例:分批发送短信、迅雷多线程下载等。

上一篇 下一篇

猜你喜欢

热点阅读