Thread 类的基本用法

创建线程

方法1 继承 Thread 类

继承 Thread 来创建⼀个线程类.

class MyThread extends Thread {
 @Override
 public void run() {
 System.out.println("这⾥是线程运⾏的代码");
 }
}

创建 MyThread 类的实例

MyThread t = new MyThread();

调⽤ start ⽅法启动线程

t.start();

方法2 实现 Runnable 接口

实现 Runnable 接口

class MyRunnable implements Runnable {
 @Override
 public void run() {
 System.out.println("这⾥是线程运⾏的代码");
 }
}

创建 Thread 类实例,调⽤ Thread 的构造⽅法时将 Runnable 对象作为 target 参数.

Thread t = new Thread(new MyRunnable());

调⽤ start ⽅法

t.start(); 

方法3 匿名内部类创建 Thread 子类对象

Thread t1 = new Thread() {
 @Override
 public void run() {
 System.out.println("使⽤匿名类创建 Thread ⼦类对象");
 }
};

方法4 匿名内部类创建 Runnable 子类对象

Thread t2 = new Thread(new Runnable() {
 @Override
 public void run() {
 System.out.println("使⽤匿名类创建 Runnable ⼦类对象");
 }
});

方法5 lambda 表达式创建 Runnable 子类对象

Thread t3 = new Thread(() -> System.out.println("使⽤匿名类创建 Thread ⼦类对象"));
Thread t4 = new Thread(() -> {
 System.out.println("使⽤匿名类创建 Thread ⼦类对象");
});

线程中断

李四⼀旦进到⼯作状态,他就会按照⾏动指南上的步骤去进⾏⼯作,不完成是不会结束的。但有时我 们需要增加⼀些机制,例如⽼板突然来电话了,说转账的对⽅是个骗⼦,需要赶紧停⽌转账,那张三 该如何通知李四停⽌呢?这就涉及到我们的停⽌线程的⽅式了。

public class Main {
    private static class MyRunnable implements Runnable {
        public volatile boolean isQuit = false;
        @Override
        public void run() {
            while (!isQuit) {
                System.out.println(Thread.currentThread().getName()
                        + ": 别管我,我忙着转账呢!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()
                    + ": 啊!险些误了⼤事");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable target = new MyRunnable();
        Thread thread = new Thread(target, "李四");
        System.out.println(Thread.currentThread().getName()
                + ": 让李四开始转账。");
        thread.start();
        Thread.sleep(10 * 1000);
        System.out.println(Thread.currentThread().getName()
                + ": ⽼板来电话了,得赶紧通知李四对⽅是个骗⼦!");
        target.isQuit = true;

    }
}

线程等待

有时,我们需要等待⼀个线程完成它的⼯作后,才能进⾏⾃⼰的下⼀步⼯作。例如,张三只有等李四 转账成功,才决定是否存钱,这时我们需要⼀个⽅法明确等待线程的结束。

public class ThreadDemo {
 public static void main(String[] args) throws InterruptedException {
     Runnable target = () -> {
         for (int i = 0; i < 10; i++) {
         try {
             System.out.println(Thread.currentThread().getName() 
         + ": 我还在⼯作!");
             Thread.sleep(1000);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
     System.out.println(Thread.currentThread().getName() + ": 我结束了!")
};
         Thread thread1 = new Thread(target, "李四");
         Thread thread2 = new Thread(target, "王五");
         System.out.println("先让李四开始⼯作");
         thread1.start();
         thread1.join();//等待thread1线程结束
         System.out.println("李四⼯作结束了,让王五开始⼯作");
         thread2.start();
         thread2.join();//等待thread2线程结束
         System.out.println("王五⼯作结束了");
     }
}

线程休眠

也是我们⽐较熟悉⼀组⽅法,有⼀点要记得,因为线程的调度是不可控的,所以,这个⽅法只能保证 实际休眠时间是⼤于等于参数设置的休眠时间的。

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

获取线程实例

public class ThreadDemo {
     public static void main(String[] args) {
         Thread thread = Thread.currentThread();
         System.out.println(thread.getName());
     }
}

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值