前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >创建线程的五种方式

创建线程的五种方式

作者头像
用户10921393
发布2024-07-05 15:18:11
790
发布2024-07-05 15:18:11
举报
文章被收录于专栏:Y.Y.

一.继承Thread ,重写run

代码语言:javascript
复制
class MyThread extends Thread{
    @Override
    public void run() {
        //这里的内容就是该线程要完成的工作
        while(true) {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new MyThread();
        t.start();//执行run方法中的逻辑

        while(true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}

运行结果: 

每秒中输出一次可能是main开头,也可能是thread开头,说明多个线程的调度顺序是"无序的",在操作系统内部也称为"抢占执行".

二.实现Runable,重写run,把Runable实例传入Thread中

代码语言:javascript
复制
class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("hello thread");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new MyRunnable());
        t.start();

        while (true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}

三.针对一的变形,使用匿名内部内,继承Thread 并重写run.

代码语言:javascript
复制
public class Demo3 {
    public static void main(String[] args) {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }.start();
        //t.start();
        while (true) {
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

1.创建一个子类(没有名字).

2.同时也创建了这个子类的实例 .

3.重写run方法.

四.匿名内部类,针对Runnable

代码语言:javascript
复制
public class Demo4 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {//此处匿名内部类,只针对Runnable和Thread没有关系,只是把Runnable作为参数传入到Thread的构造方法中
            @Override
            public void run() {
                while(true) {
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        t.start();
        
        while (true) {
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

1.创建新的类,实现Runnable,名字是匿名的

2.创建了这个类的实例(一次性的)

3.重写run方法.

五.使用lambda 表达式

代码语言:javascript
复制
public class Demo5 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            while (true) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();
        
        while (true) {
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.继承Thread ,重写run
  • 二.实现Runable,重写run,把Runable实例传入Thread中
  • 三.针对一的变形,使用匿名内部内,继承Thread 并重写run.
  • 四.匿名内部类,针对Runnable
  • 五.使用lambda 表达式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档