java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java线程创建写法

Java线程创建的5种写法详解

作者:ChaoYLuo

这段文章详细解释了线程与进程的关系、线程状态、线线程创建的几种写法、常见面试题以及高耦合与低耦合的概念,通过这些内容,读者可以全面了解线程的基础知识及其在编程中的应用,需要的朋友可以参考下

一、多线程基础概念

1. 线程与进程的关系

2. 线程状态:Thread.sleep(millis: 1000)

3. 方法重写要求

二、线程创建的几种写法

写法1:继承Thread,重写run

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);
            }
        }
    }
}

写法2:实现Runnable,搭配Thread执行

// 1. 定义 Runnable 实现类
class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (true) {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

// 2. 创建线程
public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        MyRunnable r = new MyRunnable();
        Thread t = new Thread(r);
        t.start(); // 启动新线程,执行 Runnable 内部的 run
        
        while (true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}

写法3:继承Thread,使用匿名内部类

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new 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(); // 记住:调用 start 才是真正创建线程
        
        while (true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}

步骤:

  1. 创建 Thread的子类(匿名内部类,无名字)。
  2. 重写 run
  3. 创建 Thread子类的实例,并用 t引用指向。

特点:方便、一次性使用。

写法4:实现Runnable,使用匿名内部类

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @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");
            Thread.sleep(1000);
        }
    }
}

写法5:【简化】lambda表达式

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        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");
            Thread.sleep(1000);
        }
    }
}

三、常见面试题:Thread对象与start()的关系

问题:多次调用start()会怎样?

public static void main(String[] args) {
    Thread thread = new Thread(() -> {
        System.out.println("hello");
    });
    thread.start();
    thread.start(); // 报错!
    thread.start();
}

四、补充:高耦合 vs 低耦合(生活类比)

五、包(package)的概念

以上就是Java线程创建的5种写法详解的详细内容,更多关于Java线程创建写法的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文