媒介
在Java编程里,线程是实现多使命处理的关键概念。本文会具体讲授Java中线程的创建、中断、等待以及休眠等操纵,通过丰富代码示例和具体解释,助力你理解相关知识。
1. 线程创建
1.1 继承Thread类
创建一个类继承Thread类,并重写其run方法,以此定义线程的执行逻辑。run方法是线程入口,线程启动时,run方法自动执行,无需手动调用。start方法用于真正创建并启动线程(由JVM调用系统API完成线程创建),且start方法只能调用一次(一个线程仅能创建一次)。
示例代码如下:
- class MyThread extends Thread {
- @Override
- public void run() {
- System.out.println("Thread类");
- }
- }
- public class Main {
- public static void main(String[] args) {
- // 创建线程对象
- Thread t = new MyThread();
- // 启动线程
- t.start();
- }
- }
复制代码 1.2 实现Runnable接口
创建一个实现Runnable接口的类,重写其run方法。这里创建的实现类对象实际上是一个使命,并非直接创建线程。把该使命对象传递给Thread类的构造函数,从而创建线程对象。这种方式符合解耦合原则,在实际开辟中更为常用,因为它将使命逻辑与线程本身分离,修改使命时不影响线程其他部分,提拔了代码可维护性。
示例代码如下:
- // 实现Runnable接口的类
- class MyRunnable implements Runnable {
- @Override
- public void run() {
- System.out.println("runnable接口");
- }
- }
- public class Demo1 {
- public static void main(String[] args) {
- // 创建线程对象
- Thread t = new Thread(new MyRunnable());
- // 启动线程
- t.start();
- }
- }
复制代码 1.3 匿名内部类
利用匿名内部类方式能更简便地创建线程,无需显式创建Thread子类或Runnable实现类。这种方式可直接在代码中定义线程执行逻辑,进步代码紧凑性。
示例代码如下:
- public class Demo2 {
- public static void main(String[] args) {
- // 匿名内部类——继承Thread类
- Thread t1 = new Thread() {
- @Override
- public void run() {
- System.out.println("匿名内部类——Thread类");
- }
- };
- t1.start();
- // 匿名内部类——实现Runnable接口
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- System.out.println("匿名内部类——Runnable接口");
- }
- };
- Thread t2 = new Thread(runnable);
- t2.start();
- }
- }
复制代码 1.4 lambda表达式
利用lambda表达式是对实现Runnable接口的匿名内部类的简化写法。它通过更简便语法定义线程执行逻辑,让代码更清晰易读。
示例代码如下:
- public class Demo3 {
- public static void main(String[] args) {
- // 使用lambda表达式实现Runnable接口
- Thread t = new Thread(() -> {
- System.out.println("lambda ");
- });
- t.start();
- }
- }
复制代码 2. 线程中断
在Java中,线程中断紧张涉及以下几个方法:
方法名描述interrupt()中断对象关联的线程。若线程正在阻塞,则以异常形式通知;否则设置中断标识位。isInterrupted()判定对象关联的线程的中断标识位是否设置,调用后不清除该标记位。interrupted()判定当前线程的中断标识位是否设置,调用后会清除该标识位。 以下是一个线程中断的示例代码:
- public class Demo5 {
- public static void main(String[] args) throws InterruptedException {
- Thread t = new Thread(() -> {
- for (int i = 0; i < 5; i++) {
- if (!Thread.currentThread().isInterrupted()) {
- System.out.println("线程执行中...");
- System.out.println("线程是否中断:" + Thread.currentThread().isInterrupted());
- Thread.currentThread().interrupt();
- }
- }
- });
- t.start();
- t.join();
- System.out.println("线程是否中断:" + t.isInterrupted());
- }
- }
复制代码
3. 线程等待
在Java中,可利用join()方法等待一个线程执行完毕。join()方法有以下几种重载形式:
方法名描述join()等待线程结束。join(long mills)等待线程结束,最多等待mills毫秒。join(long mills, int nanos)等待线程结束,提供更高时间精度,最多等待mills毫秒和nanos纳秒。 以下是不利用join()方法和利用join()方法的示例对比:
不利用join()方法时:
- public class Demo6 {
- public static void main(String[] args) {
- // 创建一个线程
- Thread t = new Thread(() -> {
- for (int i = 0; i < 10; i++) {
- System.out.println("t线程执行");
- }
- });
- // 启动线程
- t.start();
- System.out.println("主线程结束");
- }
- }
复制代码 利用join(long mills)方法时:
- public class Demo6 {
- public static void main(String[] args) {
- // 创建一个线程
- Thread t = new Thread(() -> {
- for (int i = 0; i < 10; i++) {
- System.out.println("t线程执行");
- }
- });
- // 启动线程
- t.start();
- // 等待t线程运行结束
- try {
- **t.join()**;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("主线程结束");
- }
- }
复制代码 利用join(long mills)方法并设置较短等待时间时:
- public class Demo6 {
- public static void main(String[] args) {
- // 创建一个线程
- Thread t = new Thread(() -> {
- for (int i = 0; i < 1000; i++) {
- System.out.println("t线程执行");
- }
- });
- // 启动线程
- t.start();
- // 等待t线程运行结束
- try {
- **t.join(1)**;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("主线程结束!!!!");
- }
- }
复制代码 4. 线程休眠
在Java中,可利用sleep()方法让当前线程停息执行一段时间。sleep()方法有以下两种重载形式:
方法名描述sleep(long mills)使当前线程休眠mills毫秒。sleep(long mills, int nanos)使当前线程休眠,提供更高精度,可休眠mills毫秒和nanos纳秒。 以下是一个线程休眠的示例代码:
- public class Demo7 {
- public static void main(String[] args) {
- Thread t = new Thread(() -> {
- for (int i = 0; i < 10; i++) {
- System.out.println("线程t");
- try {
- **Thread.sleep(1000)**;
- // 线程休眠1秒
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- });
- t.start();
- }
- }
复制代码 通过本文先容,希望你对Java中线程的基本操纵有更深入理解,能在实际开辟中机动运用这些知识实现多线程编程。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |