Java 线程的创建和使用

本文详细介绍了Java中的线程与进程概念,以及如何通过继承Thread类、实现Runnable接口、匿名内部类等方式创建多线程。还涵盖了Thread类的构造方法、常用属性、中断机制、线程状态管理和同步操作等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程与进程之间的区别

进程: 进程是指一个正在运行的程序的实例, 是操作系统进行资源分配的基本单位.

线程: 线程是进程中的一个执行单元, 是程序执行的基本单位.

两者间的区别:

  1. 进程是系统分配资源的基本单位, 线程是程序执行的基本单位.
  2. 进程具有独立的内存空间和资源, 线程则共享进程的内存和资源.
  3. 进程间通信较为复杂, 但是线程之间可以直接共享数据.
  4. 线程之间的切换代价比较大, 需要保存上下文和状态, 但是线程之间的切换代价比较小, 因为他们共享进程的资源.

Java 操作多线程

创建线程
1. 继承 Thread 类
class MyThread extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println("hello thread");
        }
    }
}

public class ThreadDemo1 {
    public static void main(String[] args) {
        Thread t = new MyThread();
        t.start();
        
        while (true) {
            System.out.println("hello main");
        }
    }
}

执行结果如上所示, 可以看出 hello thread 和 hello main 在控制台之间交替打印

其中 start() 方法是创建了一个线程, 由新的线程来执行 run() 方法. 上述使用创建线程来打印的操作与 main 方法中直接打印的区别就是: 如果只是 main 方法直接打印, 那么打印的操作就是 Java 进程中调用 main 方法的进程来打印的. 通过 t.start(), 本质就是主线程调用了 t.start() 创建出了一个新线程, 新的线程调用了 t.run() 中的打印. 当 run 方法执行完毕, 这个线程就自动销毁了.

start 和 run 的区别: start 是真正创建线程的方法, 而 run 是描述了线程要干的活是什么

2. 实现 Runnable 接口
// Runnable 的作用是描述一个要执行的任务, run 方法就是任务要执行的细节
class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("hello thread");
    } 
}

public class ThreadDemo2 {
    public static void main(String[] args) {
        // 描述了一个任务
        Runnable runnable = new MyRunnable();
        // 把任务交给线程执行
        Thread t = new Thread(runnable);
        t.start();
    }
}
3. 使用匿名内部类, 继承 Thread
// 使用匿名内部类来创建线程
public class ThreadDemo3 {
    public static void main(String[] args) {
        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println("hello thread");
            }
        };
        t.start();
    }
}
4. 使用匿名内部类, 实现 Runnable
public class ThreadDemo4 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello thread");
            }
        });
        t.start();
    }
}
5. 使用 Lambda 表达式
public class ThreadDemo5 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("hello thread"); 
        });
        t.start();
    }
}
Thread 类
Thread 的常见构造方法
方法类型
Thread()创建线程对象
Thread(Runnable target)使用 Runnable 对象创建线程对象
Thread(Runnable target, String name)使用 Runnable 对象创建线程对象,并命名
Thread(ThreadGroup group, Runnable target)线程可以被用来分组管理,分好的组即为线程组
Thread(Runnable target, String name)
public class ThreadDemo6 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }, "myThread");
        t.start();
    }
}

上述构造方法使用 Runnable 对象创建线程, 并命名, run 方法中的 Thread.currentThread().getName() 可以得到这个线程的名字, 运行结果如下:

可以看出上述的代码真实的创建了一个线程给予其任务, 并且成功给这个线程起了一个名字.

Thread 的几个常见属性
属性获取方法
IDgetId()
名称getName()
状态getState()
优先级getPriority()
是否后台线程isDaemon()
是否存活isAlive()
是否被中断isInterrupted()
isAlive()
public class ThreadDemo6 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "myThread");
        System.out.println(t.isAlive());
        t.start();

        while (true) {
            try {
                Thread.sleep(1000);
                System.out.println(t.isAlive());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


可以看出, 当线程还没执行的时候, isAlive 返回的是 false, 执行中的时候, 返回的是 true, 执行结束之后, 返回的是false.

中断一个线程

这里中断的意思是, 不是让线程立即就停止, 而是通知线程应该要停止了. 但是是否真的要停止, 取决于代码的写法.

使用标志位来控制线程是否要停止
public class ThreadDemo7 {
    private static boolean flag = true;

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
           while (flag) {
               System.out.println("hello thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        t.start();

        Thread.sleep(3000);
        // 在主线程里就可以随时通过 flag 变量的取值来操作 t 线程是否要结束
        flag = false;
    }
}

上述代码使用了一个变量 flag 来操控线程是否要结束, 这个代码之所以能起到修改 flag 的值线程就结束, 完全取决于线程内部的代码如何实现.

使用 Thread 自带的标志位进行判定
public class ThreadDemo8 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        });
        t.start();
        Thread.sleep(3000);

        t.interrupt(); // 终止线程
    }
}

上述代码中 Thread.currentThread() 是 Thread 类中的静态方法, 通过这个方法可以获取到当前线程, 哪个线程调用这个方法, 就能得到那个线程对象的引用 (类似于 this).

使用自带的标志位对比于上面自己创建的标志位的好处在于:
如果线程在 sleep 中休眠(就是线程正好走到了 sleep() 这个方法, 处于休眠状态), 此时调用 interrupt 方法就会把t线程唤醒, 从 sleep 中提前返回. 其实现是 interrupt 方法会触发 sleep 中的一个异常, 导致 sleep 提前返回.

等待一个线程

线程执行是抢占式执行, 随机调度. 所以线程的执行顺序是一个随机调度的过程, 等待线程所做的事情, 就是在控制两个线程结束的顺序.

public class ThreadDemo9 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

        System.out.println("join 之前");
        // 此处的 join 是让当前的 main 线程来等待 t 线程执行结束.
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("join 之后");
    }
}


执行完 start 之后, main 线程和 t 线程会并发执行, 分头行动, 但是当 main 线程遇到 join 的时候, 会发生阻塞(block), 一直阻塞到 t 线程执行结束之后, main 才会从 join 中恢复过来继续往下执行.(这里就人为的让 t 线程比 main 线程先结束).

获取当前线程的引用

想要获取当前线程的引用, 可以使用 Thread 类的一个类方法: public static Thread currentThread();

获取当前线程的引用, 可以

Thread t = new Thread();
Thread.cuurentThread();

或者

Thread t = new Thread();
t.cuurentThread();

这个方法的返回值就是当前线程的引用. 在哪个线程中调用, 就能获取到哪个线程的实例.

休眠当前的线程

想要把当前的线程置于休眠状态, 可以使用以下方法

Thread.sleep(long millis);

可以让当前调用这个方法的线程处于休眠状态.

在操作系统的内核中大概是以下情况:

上图中, 就绪队列是参与 CPU 调度执行的队列, 处于随叫随到的状态. 阻塞队列不参与 CPU 的调度, 处于阻塞状态.

当线程 A 调用 Thread.sleep(); 时候, 线程 A 就会从就绪状态变为阻塞状态. 就暂时无法参与调度.

例如, 当 A 线程 执行了以下代码:

Thread.sleep(1000);

会将 A 线程 放入阻塞队列中 1000ms

但是并不是 1000ms 一到, A 线程 就会被调度, 而是 1000ms 时间一到, A 线程 就会参与到与其他线程一起随机调度的过程, 往往一个线程休眠之后再次被调度之间的时间往往大于休眠的时间.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值