0
点赞
收藏
分享

微信扫一扫

【Java 线程常见方法】

眸晓 2022-04-05 阅读 179
java

设置优先级

package com.yuzhenc.thread;

/**
 * @author: yuzhenc
 * @date: 2022-04-05 19:55:59
 * @desc: com.yuzhenc.thread
 * @version: 1.0
 */
public class Test04 {
    public static void main(String[] args) {
        TestThread4 testThread4 = new TestThread4();
        //设置优先级 1-10 默认为5
        testThread4.setPriority(1);
        TestThread5 testThread5 = new TestThread5();
        Thread thread = new Thread(testThread5);
        //设置优先级 1-10 默认为5
        thread.setPriority(10);
        testThread4.start();
        thread.start();
    }
}

class TestThread4 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
    }
}
class TestThread5 implements Runnable {
    @Override
    public void run() {
        for (int i = 10000; i < 20000; i++) {
            System.out.println(i);
        }
    }
}

join

package com.yuzhenc.thread;

/**
 * @author: yuzhenc
 * @date: 2022-04-05 20:09:05
 * @desc: com.yuzhenc.thread
 * @version: 1.0
 */
public class Test06 {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            System.out.println("main----"+i);
            if (i == 6) {
                TestThread6 testThread6 = new TestThread6("子线程");
                //当一个线程调用了join方法,这个线程就会先被执行,它执行结束以后才可以去执行其余的线程
                testThread6.start();
                testThread6.join();//半路杀出个程咬金
            }
        }
    }
}

class TestThread6 extends Thread {
    public TestThread6(String name) {
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(this.getName()+"----"+i);
        }
    }
}

在这里插入图片描述

sleep

package com.yuzhenc.thread;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: yuzhenc
 * @date: 2022-04-05 20:43:37
 * @desc: com.yuzhenc.thread
 * @version: 1.0
 */
public class Test07 {
    public static void main(String[] args) {
        //定义一个时间格式
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        while(true) {
            Date d = new Date();
            System.out.println(df.format(d));
            try {
                //人为阻塞 1 秒
                Thread.sleep(1000);
            } catch (InterruptedException interruptedException) {
                System.out.println(interruptedException.getStackTrace());
            }
        }
    }
}

setDaemon

package com.yuzhenc.thread;

/**
 * @author: yuzhenc
 * @date: 2022-04-05 20:51:39
 * @desc: com.yuzhenc.thread
 * @version: 1.0
 */
public class Test08 {
    public static void main(String[] args) {
        TestThread7 thread7 = new TestThread7();
        //主线程结束后,伴随进程随之结束
        thread7.setDaemon(true);//设置伴随线程  注意:先设置,再启动
        thread7.start();
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}

class TestThread7 extends Thread{
    @Override
    public void run() {
        for (int i = 10; i < 1000; i++) {
            System.out.println(i);
        }
    }
}
举报

相关推荐

0 条评论