java 创建线程的四种方式

网友投稿 344 2022-11-13


java 创建线程的四种方式

1、继承Thread类方式

这种方式适用于执行特定任务,并且需要获取处理后的数据的场景。

举例:一个用于累加数组内数据的和的线程。

public class AdditionThread extends Thread {

private int sum = 0;

private int[] nums;

public AdditionThread(int[] nums, String threadName) {

super(threadName);

this.nums = nums;

}

@Override

public void run() {

for (int num : nums) {

sum += num;

}

}

public int getSum() {

return sum;

}

}

调用方式:

public class Main {

public static void main(String[] args) throws InterruptedException {

int[] nums = {10, 12, 15, 200, 100};

AdditionThread thread = new AdditionThread(nums, "AdditionThread");

thread.start();

thread.join();

System.out.println("sum=" + thread.getSum());

}

}

2、Runnable 接口方式

定http://义一个实现Runnable接口的类,或者直接创建一个匿名内部类,并覆盖 run() 方法。最后作为参数传给Thread的构造函数。

public class Main {

public static void main(String[] args) {

// 自定义的 Runnable

Runnable runnable = new MyRunnable();

Thread thread = new Thread(runnable, "Runnable-Thread");

thread.start();

// 自定义匿名内部类

new Thread(() -> {

System.out.println("Inner class");

}).start();

}

static class MyRunnable implements Runnable {

@Override

public void run() {

System.out.println("MyRunnable");

}

}

}

3、 Callable 接口方式

Callable 接口与 Runnable 接口的区别:

(1)Callable 的方法为call(),Runnable的方法为run()。

(2)Callable 的方法由返回值,Runnable 没有。

(3)Callable 的方法声明的Exception,Runnable的没有。

public class Main {

public static void main(String[] args) {

MyCallable myCallable = new MyCallable();

FutureTask(myCallable);

Thread thread = new Thread(task, "FutureTask");

thread.start();

try {

// 通过get方法获取返回值

String result = task.get();

System.out.println(result);

} catch (InterruptedException | ExecutionException e) {

e.printStackTrace();

}

}

static class MyCallable implements Callable {

@Override

public String call() throws Exception {

// 模拟超时操作

Thread.sleep(1000);

return "OK";

}

}

}

4、线程池方式

我们可以通过 ThreadPoolExecutor 类的构造函数来创建线程池,也可以通过Executors工厂方法来创建,如

// 创建固定线程数的线程池

Executors.newFixedThreadPool();

// 创建只有一个核心线程的线程池

Executors.newSingleThreadExecutor();

// 创建一个没有核心线程,但可以缓存线程的线程池

Executors.newCachedThreadPool();

// 创建一个适用于执行定时任务的线程池

Executors.newScheduledThreadPool();

在创建线程池时,最好传入 ThreadFactory 参数,指定线程池所创建线程的名称。这样有利于分析定位可能存在的问题。

public class Main {

private static final ExecutorService SERVICE =

Executors.newFixedThreadPool(5, new BasicThreadFactory("My-Thread"));

public static void main(String[] args) {

// 打印线程的名字

System.out.println("main thread name:" + Thread.currentThread().getName());

SERVICE.execute(() -> {

System.out.println("Hello thread pool.");

// 打印线程池里的线程的名字

System.out.println("thread name:" + Thread.cUGMIjPkuurrentThread().getName());

});

}

static class BasicThreadFactory implements ThreadFactory {

private final AtomicInteger threadNumber = new AtomicInteger(0);

private final String basicName;

public BasicThreadFactory(String basicName) {

this.basicName = basicName;

}

@Override

public Thread newThread(Runnable runnable) {

Thread thread = new Thread(runnable);

String name = this.basicName + "-" + threadNumber.incrementAndGet();

thread.setName(name);

return thread;

}

}

}

以上就是java 创建线程的四种方式的详细内容,更多关于java 创建线程的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java8如何使用Lambda表达式简化代码详解
下一篇:浅析Java中Apache BeanUtils和Spring BeanUtils的用法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~