多线程的实现方式

继承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);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值