Java多线程中如何创建线程

发布时间:2022-02-24 13:42:10 作者:小新
来源:亿速云 阅读:160

这篇文章主要介绍Java多线程中如何创建线程,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、三种创建方式

基于什么创建创建的方式
Thread类继承Thread
Runnable接口实现Runnable接口
callable接口实现callable接口

二、通过Thread类创建

2.1 步骤

2.2 案例

//主方法
public class Demo01 {
    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();

        thread1.start();
        thread2.start();
    }
}

//100以内的偶数
class Thread1 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i%2==0){
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}

//100以内的奇数
class Thread2 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i%2!=0){
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}
public class Demo02 {
    public static void main(String[] args) {
        //打印0~100内的偶数
        new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i%2==0){
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();
        //打印0~100内的奇数
        new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i%2!=0){
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();
    }
}
public class Test {
    public static void main(String[] args) {
        Window w1 = new Window("窗口 1 ");
        Window w2 = new Window("窗口 2 ");
        Window w3 = new Window("窗口 3 ");

        w1.start();
        w2.start();
        w3.start();
    }
}

class Window extends Thread{
    //这里票的数量应该是静态变量,否则每个对象创建后都有100张票,而不是总共100张票
    private static int tickets = 100;

    public Window(String name) {
        super(name);
    }

    @Override
    public void run() {
        while (tickets > 0){
            tickets--;
            System.out.println(getName() + "卖出了一张票,剩余票数:" + tickets);
        }
    }
}

Java多线程中如何创建线程 

2.3 注意的问题

三、Thread类中常用的方法

3.1 案例

Java多线程中如何创建线程

四、通过实现Runnable接口来创建线程

4.1 创建步骤

Java多线程中如何创建线程

五、继承Thread类和实现Runnable接口两种方式比较

开发中,优先选择实现Runnable接口的方式创建线程

原因:

联系:Thread类本身也实现了Runnable接口

Java多线程中如何创建线程

相同点:两种方式都需要重写run()方法,将线程要执行的逻辑声明在run()方法中

六、线程的优先级设置

调度策略

线程的优先级分为1~10十个档,其中:

注意:高优先级的线程要抢占低优先级线程CPU的执行权。但是只是从概率上来讲,高优先级的线程高概率的情况下被执行。并不意味着只有当高优先级的线程被执行完以后,低优先级的线程才会被执行。

以上是“Java多线程中如何创建线程”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Java中怎么创建线程
  2. 怎样在Java中创建线程

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:pyspark如何创建DataFrame

下一篇:适合做微信预约小程序的行业有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》