第一部分 Java并发编程基础篇
第1章 并发编程线程基础
1.1 线程的三种创建方法及优缺点
总结:
继承Thread类的好处:方便传参,可以在子类中添加成员变量。缺点:不支持多继承。
实现Runnable的好处:可以实现多继承。缺点:不能添加成员变量,只能使用主线程里面被声明为final 的变量。
Thread、Runnable都有的缺点是任务结束不能返回结果。FutureTask可以。
Runnable 和 Collable接口,唯一的区别的Collable接口里的方法有返回值,其他的两者基本相同,所以两者可以归为一种方式。
采用Runnable和Collable接口的优缺点:
- 实现接口,还可以继承其他类。
- 多个线程可以共享一个target对象,非常适合多个线程处理同一份资源的情况。
- 如果访问当前线程,必须使用Thread.currentThread()方法。
继承Thread类的方式优缺点: - 继承了Thread类,不能再继承其他类。
- 访问当前线程可以直接使用this。
所以,一般推荐使用Runnable、Collable接口方式创建多线程。
(1) 继承Thread
优点:
- (当前线程)在Run方法内获取当前线程直接使用this即可。this.getName()为获取当前线程的名字。
- 方便传参。可以在Thread的子类中添加成员变量。
缺点:
- (继承)使用Thread,不能进行多继承。
- 任务(run方法)没有返回值。
package com.p1_2;
/*
缺点:
(1)(继承)使用Thread,不能进行多继承。
(2)(任务代码逻辑)任务与代码没有分离,多个线程执行一样的任务时需要多份任务代码逻辑。
*
*/
class MyThread extends Thread{
private static int value = 0;
private String time;
//优点:方便传参,可以在Thread的子类中添加成员变量。
public MyThread(String time) {
// TODO Auto-generated constructor stub
this.time = time;
}
//缺点:run方法没有返回值
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (MyThread.class) {
this.value ++;
//优点:(当前线程)在Run方法内获取当前线程直接使用this即可。this.getName()为获取当前线程的名字
System.out.println(this.getName() + " 's id is " + this.value + " time: " + this.time);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
MyThread thread1 = new MyThread("2019-07-30");
thread1.setName("FirstThread");
thread1.start();
MyThread thread2 = new MyThread("2019-07-31");
thread2.setName("SecondThread");
thread2.start();
}
}
输出:
FirstThread 's id is 1 time: 2019-07-30
SecondThread 's id is 2 time: 2019-07-31
(2)实现Runnable接口
优点:若各个线程的任务相同,所有线程可以共用一份代码逻辑。
缺点:任务(run方法)没有返回值。
package com.p1_2;
class MyRunnable implements Runnable{
public static int value = 0;
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (MyRunnable.class) {
this.value ++;
System.out.println(Thread.currentThread().getName() + "'s id is " + this.value);
}
}
}
public class RunnableTest {
public static void main(String[] args) {
//优点:thread1、thread2执行相同的任务,共用一份代码逻辑。
MyRunnable runnable = new MyRunnable();
Thread thread1 = new Thread(runnable);
thread1.setName("FirstThread");
thread1.start();
Thread thread2 = new Thread(runnable);
thread2.setName("FirstThread");
thread2.start();
}
}
(3)FutureTask
上述Runnable、Thread两种方法,有一个共同的缺点,即任务不能返回值。
package com.p1_2;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
class MyCallable implements Callable<String>{
public static int value = 0;
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
synchronized (MyCallable.class) {
this.value ++;
System.out.println(Thread.currentThread().getName() + "'s id is " + this.value);
//优点:可以返回值
return Thread.currentThread().getName() + " done successfully!";
}
}
}
public class FutureTaskTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
// TODO Auto-generated method stub
//多个FutureTask可以共享同一个myCallable的代码逻辑。
MyCallable myCallable = new MyCallable();
FutureTask<String> futureTask1 = new FutureTask<String>(myCallable);
Thread thread1 = new Thread(futureTask1);
thread1.setName("FirstThread");
thread1.start();
System.out.println(futureTask1.get());
FutureTask<String> futureTask2 = new FutureTask<String>(myCallable);
Thread thread2 = new Thread(futureTask2);
thread2.setName("SecondThread");
thread2.start();
System.out.println(futureTask2.get());
}
}
输出
FirstThread's id is 1
FirstThread done successfully!
SecondThread's id is 2
SecondThread done successfully!