java多线程-初探(二)

java多线程-初探(一)

常见的线程函数

sleep

当前线程暂停

join

加入到当前线程中

setPriority

线程优先级

yield

临时暂停

setDaemon

守护线程

sleep

当前线程休眠具体的毫秒值后唤醒。

InterruptedException异常:当线程休眠时异常中断将抛出此异常。

class Main

package com.thread.two;

public class Main {

    /**
     * 吃饭
     * @param args
     */
    public static void main(String[] args) {
        new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("执行前毫秒数:" + beginTime);
                // 执行sleep函数。当前调用的线程休眠固定毫秒数
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("执行结束毫秒数:" + endTime);
                System.out.println("执行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();
    }
}

执行结果

join

将当前线程加入到主线程。当前线程执行完成,主线程才能接着往下执行。

主线程:执行main函数时,整个进程是一个主线程,主线程从上往下依次执行,遇到Thread类开启多线程时,线程类独立执行,不影响主线程的执行步骤,就是说子线程独立执行,主线程还是往下继续执行。但是当子线程调用join函数加入到主线程当中,则主线程必须等子线程执行完成才能往下继续执行。

class Main

package com.thread.two;

public class Main {

    /**
     * 吃饭
     * @param args
     */
    public static void main(String[] args) {
        /*new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("执行前毫秒数:" + beginTime);
                // 执行sleep函数。当前调用的线程休眠固定毫秒数
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("执行结束毫秒数:" + endTime);
                System.out.println("执行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();*/

        // join
        System.out.println("我是主线程-begin");
        Thread thread = new Thread() {
            public void run(){
                // 每个一秒打印一次:我是子线程
                for(int i = 0 ; i < 3 ; i++){
                    System.out.println("我是子线程");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
//        thread.join();
        System.out.println("我是主线程-end");
    }
}

注释thread.join();的执行结果

打开thread.join();的执行结果

setPriority

设置线程的优先级,取值范围:1-10

线程优先级越高,该线程越容易获得cpu资源,从而执行自身线程,相较于其他低优先级的线程优先执行。

class Main

package com.thread.two;

public class Main {

    /**
     * 吃饭
     * @param args
     */
    public static void main(String[] args) {
        /*new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("执行前毫秒数:" + beginTime);
                // 执行sleep函数。当前调用的线程休眠固定毫秒数
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("执行结束毫秒数:" + endTime);
                System.out.println("执行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();*/

        /*// join
        System.out.println("我是主线程-begin");
        Thread thread = new Thread() {
            public void run(){
                // 每个一秒打印一次:我是子线程
                for(int i = 0 ; i < 3 ; i++){
                    System.out.println("我是子线程");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是主线程-end");*/

        // setPriority线程优先级
        // 线程1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是线程----1");
                }
            }
        };
        // 线程二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    System.out.println("我是线程----2");
                }
            }
        };
        // 设置优先级 取值:1-10
        thread1.setPriority(Thread.MIN_PRIORITY);// 最小
        thread2.setPriority(Thread.MAX_PRIORITY);// 最大
        thread1.start();
        thread2.start();
    }
}

数据量小无法体现,这里只截部分执行结果

yield

临时暂停当前线程,使得其余线程能更容易获得cpu资源执行。

class Main

package com.thread.two;

public class Main {

    /**
     * 吃饭
     * @param args
     */
    public static void main(String[] args) {
        /*new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("执行前毫秒数:" + beginTime);
                // 执行sleep函数。当前调用的线程休眠固定毫秒数
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("执行结束毫秒数:" + endTime);
                System.out.println("执行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();*/

        /*// join
        System.out.println("我是主线程-begin");
        Thread thread = new Thread() {
            public void run(){
                // 每个一秒打印一次:我是子线程
                for(int i = 0 ; i < 3 ; i++){
                    System.out.println("我是子线程");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是主线程-end");*/

        /*// setPriority线程优先级
        // 线程1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是线程----1");
                }
            }
        };
        // 线程二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    System.out.println("我是线程----2");
                }
            }
        };
        // 设置优先级 取值:1-10
        thread1.setPriority(Thread.MIN_PRIORITY);// 最小
        thread2.setPriority(Thread.MAX_PRIORITY);// 最大
        thread1.start();
        thread2.start();*/

        // yield 临时暂停
        // 线程1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是线程----1");
                }
            }
        };
        // 线程二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    // 执行一半临时暂停
                    if (i == 50) Thread.yield();
                    System.out.println("我是线程----2");
                }
            }
        };
        thread1.start();
        thread2.start();
    }
}

执行结果

setDaemon

守护线程,当一个进程里全部都是守护线程时,结束当前整个进程。

守护线程一般用作业务附加的辅助功能,如进程执行的记录日志、检测当前用户状态。

class Main

package com.thread.two;

public class Main {

    /**
     * 吃饭
     * @param args
     */
    public static void main(String[] args) {
        /*new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("执行前毫秒数:" + beginTime);
                // 执行sleep函数。当前调用的线程休眠固定毫秒数
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("执行结束毫秒数:" + endTime);
                System.out.println("执行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();*/

        /*// join
        System.out.println("我是主线程-begin");
        Thread thread = new Thread() {
            public void run(){
                // 每个一秒打印一次:我是子线程
                for(int i = 0 ; i < 3 ; i++){
                    System.out.println("我是子线程");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是主线程-end");*/

        /*// setPriority线程优先级
        // 线程1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是线程----1");
                }
            }
        };
        // 线程二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    System.out.println("我是线程----2");
                }
            }
        };
        // 设置优先级 取值:1-10
        thread1.setPriority(Thread.MIN_PRIORITY);// 最小
        thread2.setPriority(Thread.MAX_PRIORITY);// 最大
        thread1.start();
        thread2.start();*/

        /*// yield 临时暂停
        // 线程1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是线程----1");
                }
            }
        };
        // 线程二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    // 执行一半临时暂停
                    if (i == 50) Thread.yield();
                    System.out.println("我是线程----2");
                }
            }
        };
        thread1.start();
        thread2.start();*/

        // setDaemon 守护线程
        System.out.println("主线程开始");
        Thread thread = new Thread(){
            public void run(){
                while (true){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("我是守护线程!");
                }
            }
        };
        thread.setDaemon(true);
        thread.start();
        System.out.println("主线程结束");
    }
}

执行结果

主线程结束,守护线程也相应结束,不会继续执行。

当所有线程都是守护线程时,进程结束执行。

java多线程-初探(三)

猜你喜欢

转载自blog.csdn.net/wkh___/article/details/84787653