(1) 继承Thread类,重写run方法
// (1)继承Thread类,重写run方法
public class ByThread extends Thread{
@Override
public void run() {
for(int i=0; i<100; i++){
System.out.println("新线程执行:" + i);
}
}
public static void main(String[] args) {
// 创建线程对象
ByThread thread1 = new ByThread();
// start方法开启线程
thread1.start();
// 主线程和新线程交替执行
for(int i=0; i<100; i++) {
System.out.println("主线程执行:" + i);
}
}
}
(2) 实现Runnable接口,重写run方法
多个线程操作同一个资源
/*
* (2) 实现Runnable接口,重写run方法
* 多个线程操作同一个资源
*/
public class ByRunnable implements Runnable {
private int tickectNums = 10;
@Override
public void run() {
while (true) {
if (tickectNums <= 0) {
break;
}
// 延时
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "->拿到了第" + tickectNums-- + "票");
}
}
public static void main(String[] args) {
// 构造Thread1类对象
ByRunnable thread = new ByRunnable();
// 构造Thread类对象,调用其start方法开启线程,设置线程的名字
// 方便多个线程使用
new Thread(thread, "Joey").start();
new Thread(thread, "Pheebs").start();
new Thread(thread, "Monica").start();
}
}
(3) 实现Callable接口,重写call方法
(3.1) 使用Executors.newFixedThreadPool创建线程池
(3.2) 直接使用new Thread来创建
参考https://www.cnblogs.com/guanbin-529/p/11784914.html
/*
* (3) 实现Callable接口,重写call方法
* 可以抛出异常
* 可以定义返回值
*/
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
/**
* @author: GuanBin
* @date: Created in 下午11:19 2019/10/31
*/
public class TwoCallable implements Callable<Object> {
private int taskNum;
public TwoCallable(int taskNum) {
this.taskNum = taskNum;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
test1();
test2();
}
/**
* 【1】使用Executors.newFixedThreadPool创建线程池
* @throws InterruptedException
* @throws ExecutionException
*/
private static void test1() throws InterruptedException, ExecutionException {
System.out.println("----程序开始运行----");
Date date1 = new Date();
// 5个线程数
int taskSize = 5;
// 创建执行服务,线程池
ExecutorService pool = Executors.newFixedThreadPool(taskSize);
// 结果储存在list中
List<Future> list = new ArrayList<Future>();
for (int i = 0; i < taskSize; i++) {
// 创建【目标】对象
Callable c = new TwoCallable(i);
// 服务提交线程并获取Future对象
Future f = pool.submit(c);
list.add(f);
}
// 关闭线程池
pool.shutdown();
// 获取所有并发任务的运行结果
for (Future f : list) {
// 从Future对象上获取任务的返回值,并输出到控制台
System.out.println(">>>" + f.get().toString()); // OPTION + return 抛异常
}
Date date2 = new Date();
System.out.println("----程序结束运行----,程序运行时间【" + (date2.getTime() - date1.getTime()) + "毫秒】");
}
/**
* 【2】线程直接使用new Thread来创建
* @throws ExecutionException
* @throws InterruptedException
*/
private static void test2() throws ExecutionException, InterruptedException {
System.out.println("----程序开始运行----");
Date date1 = new Date();
int taskSize = 5;
FutureTask[] randomNumberTasks = new FutureTask[5];
List<Future> list = new ArrayList<Future>();
for (int i = 0; i < randomNumberTasks.length; i++) {
Callable c = new TwoCallable(i);
// 执行任务并获取Future对象
randomNumberTasks[i] = new FutureTask(c);
Thread t = new Thread(randomNumberTasks[i]);
t.start();
}
// 获取所有并发任务的运行结果
for (Future f : randomNumberTasks) {
// 从Future对象上获取任务的返回值,并输
System.out.println(">>>" + f.get().toString()); // OPTION + return 抛异常
}
Date date2 = new Date();
System.out.println("----程序结束运行----,程序运行时间【" + (date2.getTime() - date1.getTime()) + "毫秒】");
}
/**
* call方法的实现,主要用于执行线程的具体实现,并返回结果
* @return
* @throws Exception
*/
@Override
public Object call() throws Exception {
System.out.println(">>>" + taskNum + "任务启动");
Date dateTmp1 = new Date();
Thread.sleep(1000);
Date dateTmp2 = new Date();
long time = dateTmp2.getTime() - dateTmp1.getTime();
System.out.println(">>>" + taskNum + "任务终止");
return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";
}
}