继承Thread类
第一步:创建一个普通的java类并继承Thread 类
public class MyThread extends Thread {
}
第二步:重写run()方法
public class MyThread extends Thread {
@Override
public void run() {
System.out.println("MyThread线程执行……");
}
}
测试:
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
注意:
1. 新线程被创建并启动后,不会立即执行!!!
测试如下:
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
for (int i = 0; i < 400; i++) {
System.out.println("主线程执行……");
}
}
2. 调用start()方法和调用run()方法效果不同:调用start()方法会启动一个新的线程,然后在这个新的线程中执行run()方法中的代码;调用run()方法不会启动新的线程,而是在原来的线程中执行run()方法中的代码!!!
测试如下:
public class MyThread extends Thread {
@Override
public void run() {
// 获取当前正在执行任务的线程
Thread thread = Thread.currentThread();
// 获取线程的名称
String threadName = thread.getName();
// 打印线程的名称
System.out.println("当前正在执行任务的线程是:" + threadName);
}
}
public static void main(String[] args) {
// 调用start()方法
MyThread thread = new MyThread();
thread.start();
// 调用run()方法
thread.run();
}
实现Runnable接口
第一步:创建一个普通的java类并实现Runnable接口
public class MyRunable implements Runnable {
}
第二步:实现run()方法
public class MyRunable implements Runnable {
@Override
public void run() {
System.out.println("MyRunable线程执行……");
}
}
测试:
public static void main(String[] args) {
MyRunable runable = new MyRunable();
Thread thread = new Thread(runable);
thread.start();
}
注意:
1. 新线程被创建并启动后,不会立即执行!!!
测试如下:
public static void main(String[] args) {
MyRunable runable = new MyRunable();
Thread thread = new Thread(runable);
thread.start();
for (int i = 0; i < 400; i++) {
System.out.println("主线程执行……");
}
}
2. 调用start()方法和调用run()方法效果不同:调用start()方法会启动一个新的线程,然后在这个新的线程中执行run()方法中的代码;调用run()方法不会启动新的线程,而是在原来的线程中执行run()方法中的代码!!!
测试如下:
public class MyRunable implements Runnable {
@Override
public void run() {
// 获取当前正在执行任务的线程
Thread thread = Thread.currentThread();
// 获取线程的名称
String threadName = thread.getName();
// 打印线程的名称
System.out.println("当前正在执行任务的线程是:" + threadName);
}
}
public static void main(String[] args) {
MyRunable runable = new MyRunable();
// 调用start()方法
Thread thread = new Thread(runable);
thread.start();
// 调用run()方法
thread .run();
}
实现Callable接口
第一步:创建一个普通的java类并实现Callable接口
public class MyCallable implements Callable<String> {
}
第二步:实现call()方法
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("MyCallable线程执行……");
return "MyCallable线程执行结果";
}
}
测试:
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable callable = new MyCallable();
FutureTask<String> task = new FutureTask<>(callable);
Thread thread = new Thread(task);
thread.start();
}
注意:
1. 新线程被创建并启动后,不会立即执行!!!
测试如下:
public static void main(String[] args) {
MyCallable callable = new MyCallable();
FutureTask<String> task = new FutureTask<>(callable);
Thread thread = new Thread(task);
thread.start();
for (int i = 0; i < 400; i++) {
System.out.println("主线程执行……");
}
}
2. 调用start()方法和调用run()方法效果不同:调用start()方法会启动一个新的线程,然后在这个新的线程中执行run()方法中的代码;调用run()方法不会启动新的线程,而是在原来的线程中执行run()方法中的代码!!!
测试如下:
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
// 获取当前正在执行任务的线程
Thread thread = Thread.currentThread();
// 获取线程的名称
String threadName = thread.getName();
// 打印线程的名称
System.out.println("当前正在执行任务的线程是:" + threadName);
}
}
public static void main(String[] args) {
MyCallable callable = new MyCallable();
FutureTask<String> task = new FutureTask<>(callable);
// 调用start()方法
Thread thread = new Thread(task);
thread.start();
// 调用run()方法
thread.run();
}
3. Callable需要借助FutureTask类执行线程创建和任务!!!
4. FutureTask类中的get()方法会返回执行的结果,但是会阻塞主线程,如果不调用get()方法,就不会阻塞主线程!!!
测试如下:
public static void main(String[] args) {
MyCallable callable = new MyCallable();
FutureTask<String> task = new FutureTask<>(callable);
Thread thread = new Thread(task);
thread.start();
// 获取执行结果
System.out.println(task.get());
System.out.println("主线程执行……");
}
线程池
缓存线程池
创建方式:
ExecutorService cached= Executors.newCachedThreadPool();
测试:
public static void main(String[] args) {
ExecutorService cached = Executors.newCachedThreadPool();
cached.execute(() -> {
System.out.println("cached线程池执行……");
});
}
工作线程池
创建方式:
ExecutorService fixed = Executors.newFixedThreadPool(3);
测试:
public static void main(String[] args) {
ExecutorService fixed = Executors.newCachedThreadPool();
fixed.execute(() -> {
System.out.println("fixed线程池执行……");
});
}
单线程线程池
创建方式:
ExecutorService single = Executors.newSingleThreadExecutor();
测试:
public static void main(String[] args) {
ExecutorService single = Executors.newCachedThreadPool();
single.execute(() -> {
System.out.println("single线程池执行……");
});
}
定长线程池
创建方式:
ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(5);
测试:
public static void main(String[] args) {
ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 10; i++) {
scheduled .schedule(() -> {
System.out.println("3秒后执行");
}, 3, TimeUnit.SECONDS);
}
}