栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 创建线程的方式?停止线程的方式?

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 创建线程的方式?停止线程的方式?

创建线程 方式1 - 继承Thead类,重写run()方法
public class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("线程操作");
    }
}
public static void main(String[] args) throws Exception {
    new MyThread().start();
}
方式2 - 实现Runnable类,重写run()方法
public class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println("线程操作");
    }
}
public static void main(String[] args) throws Exception {
    new Thread(new MyRunnable()).start();
}
方式3 - 使用lombda
public static void main(String[] args) throws Exception {
    new Thread(() -> {
        System.out.println("线程操作");
    }).start();
}
方式4 - 实现Callable类,重写call()方法
public class MyCallable implements Callable{
    @Override
    public String call() throws Exception {
        System.out.println("线程操作");
        return "call";
    }
}
public static void main(String[] args) throws Exception {
    FutureTask futureTask = new FutureTask<>(new MyCallable());
    new Thread(futureTask).start();
    // 获取call()的返回值,此方法会阻塞
    futureTask.get();
}
方式5 - 使用线程池
public class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println("线程操作");
    }
}
public class MyCallable implements Callable{
    @Override
    public String call() throws Exception {
        System.out.println("线程操作");
        return "call";
    }
}
public static void main(String[] args) throws Exception {
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.execute(new MyRunnable());
    Future future = executorService.submit(new FutureTask<>(new MyCallable()));
	future.get();
}
停止线程 方式1 - 使用stop()方法
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (true) {
            System.out.println("线程操作");
        }
    }
}
public static void main(String[] args) throws Exception {
    Thread thread = new Thread(new MyRunnable());
    thread.start();
    Thread.sleep(500);
    // 停止线程
    thread.stop();
}
方式2 - 使用suspend()和resume()方法
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (true) {
            System.out.println("线程操作");
        }
    }
}
public static void main(String[] args) throws Exception {
    Thread thread = new Thread(new MyRunnable());
    thread.start();
    Thread.sleep(500);
    // 暂停线程
	thread.suspend();
	// 恢复线程执行
	thread.resume();
}
方式3 - 配合volatile
private static volatile boolean isStop = false;

public static class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (!isStop) {
            System.out.println("线程操作");
        }
    }
}

public static void main(String[] args) throws Exception {
    Thread thread = new Thread(new MyRunnable());
    thread.start();
    Thread.sleep(500);
    // 修改变量
    isStop = true;
}
方式4 - 使用interrupt()方法
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (!Thread.interrupted()) {
            System.out.println("线程操作");
        }
    }
}
public static void main(String[] args) throws Exception {
    Thread thread = new Thread(new MyRunnable());
    thread.start();
    Thread.sleep(500);
    // 打断线程
    thread.interrupt();
}
方式5 - 什么都不做,让线程自然结束
public static class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("线程操作");
    }
}
public static void main(String[] args) throws Exception {
    Thread thread = new Thread(new MyRunnable());
    thread.start();
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号