Thread 类的基本用法

本文介绍了Java中创建线程的多种方式,包括继承Thread类、实现Runnable接口、使用Lambda表达式及Callable接口等,并详细讲解了线程的中断、等待和休眠等基本操作。

1.线程创建

(1)继承Thread

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

(2)实现Runnable

(2.1)实现 Runnable 接⼝

public class ThreadDemo2 {
    static class MyThread implements Runnable{

        @Override
        public void run() {
            System.out.println("hello");
        }
    }
    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        Thread thread=new Thread(myThread);
        thread.start();
    }
}
(2.2)匿名 Runnable
public class ThreadDemo4 {
    public static void main(String[] args) {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello");
            }
        });
        thread.start();
    }
}
(3)使⽤ Lambda
public class ThreadDemo5 {
    public static void main(String[] args) {
        Thread thread=new Thread(()->{
            System.out.println("hello");
        });
        thread.start();
    }
}

(4)带返回值的 Callable

public class ThreadDemo6 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask=new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return 0;
            }
        });
        Thread thread=new Thread(futureTask);
        thread.start();
        int ret=futureTask.get();
    }
}

2.线程中断

(1)⾃定义标记符

public class ThreadDemo7 {
    private static volatile boolean flag = false;
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                while(!flag){
                    try {
                        Thread.sleep(TimeUnit.SECONDS.toMillis(1));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"子线程");
        thread.start();
        flag=true;
    }
}

(2)使⽤Interrupt()

public class ThreadDemo8 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                while(!Thread.interrupted()){
                    try {
                        Thread.sleep(TimeUnit.SECONDS.toMillis(1));
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        },"子线程");
        thread.start();
        Thread.sleep(3000);
        thread.interrupt();
    }
}

3.线程等待 join

public void join()  等待线程结束
public void join(long millis)  等待线程结束,最多等 millis 毫秒
public void join(long millis, int nanos)  同理,但可以更⾼精度
public class ThreadDemo9 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();
        thread.join();
        Thread thread1=new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread1.start();
    }
}

4.线程休眠

(1)使⽤ sleep 休眠

public static void sleep(long millis) throws InterruptedException   休眠当前线程 millis 毫秒
public static void sleep(long millis, int nanos) throws InterruptedException  可以更⾼精度的休眠
public class ThreadDemo10 {
    public static void main(String[] args) throws InterruptedException {
        System.out.println(System.currentTimeMillis());
        Thread.sleep(4*1000);
        System.out.println(System.currentTimeMillis());
    }
}

(2)使⽤ TimeUnit 休眠

TimeUnit.DAYS.sleep(1);//

TimeUnit.HOURS.sleep(1);//⼩时
TimeUnit.MINUTES.sleep(1);//
TimeUnit.SECONDS.sleep(1);//

5.获取线程实例

public class ThreadDemo {
    public static void main(String[] args) {
        Thread thread = new Thread();
   }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值