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

JAVA 线程实现/创建方式

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

JAVA 线程实现/创建方式

一、继承Thread类

Thread 类本质上是实现了Runnable 接口的一个实例,代表一个线程的实例。启动线程的唯一方法就是通过Thread 类的start()实例方法。start()方法是一个native 方法,它将启动一个新线程,并执行run()方法

public class TestThread extends Thread {
	@Override
	public void run() {
		System.out.println("MyThread.run()");
	}
	public static void main(String[] arg) {
		TestThread myThread = new TestThread();
		myThread.start();
	}

}
二、实现Runnable接口

如果自己的类已经extends 另一个类,就无法直接extends Thread,此时,可以实现一个Runnable 接口。

public class TestThread implements Runnable {
	public static void main(String[] args) {
		TestThread myThread = new TestThread();
		Thread thread = new Thread(myThread);
		thread.start();
	}
	@Override
	public void run() {
		System.out.println("TestThread");
	}
}
三、ExecutorService、Callable、Future 有返回值的线程

有返回值的任务必须实现Callable 接口,类似的,无返回值的任务必须Runnable 接口。执行Callable 任务后,可以获取一个Future 的对象,在该对象上调用get 就可以获取到Callable 任务返回的Object 了,再结合线程池接口ExecutorService 就可以实现有返回结果的多线程了。


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestThread implements Callable {
	private String name;

	TestThread(String name) {
		this.name = name;
	}
	@Override
	public String call() throws Exception {
		Thread.sleep(30);
		System.out.println(this.name+":callable方法执行了");
		return this.name;
	}
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// 1.创建一个线程池
		int taskSize = 10;
		ExecutorService executorService = Executors.newFixedThreadPool(taskSize);
		List> list = new ArrayList>();
		for (int i = 0; i < taskSize; i++) {
			// 创建多个callable对象
			TestThread callable = new TestThread("callable" + i);
			// 执行任务并把返回的Future对象添加到list中
			Future f = executorService.submit(callable);
			list.add(f);
		}
		// 关闭线程池
		executorService.shutdown();
		// 获取所有并发任务的运行结果
		for (Future f : list) {
			// 从Future 对象上获取任务的返回值,并输出到控制台
			System.out.println("res:" + f.get());
		}
	}
}

基于线程池的方式

线程和数据库连接这些资源都是非常宝贵的资源。那么每次需要的时候创建,不需要的时候销
毁,是非常浪费资源的。那么我们就可以使用缓存的策略,也就是使用线程池。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestThread {
	public static void main(String[] args) {
		ExecutorService executorService = Executors.newFixedThreadPool(10);
		while (true) {
			executorService.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName());
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		}
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704788.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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