1)继承 Thread类
创建线程类;
2)实现 Runnable接口
创建线程类;
3)使用 Callable
和 Future
创建线程。
Thread类
的子类,并重写该类的 run()
方法,该 run()
方法的方法体就代表线程需要完成的任务。 run()
即为线程执行体; Thread子类
的实例,即创建线程对象; start()
方法来启动该线程。 Thread currentThread() void setName() String getName() void start() run()
public class ThreadTest extends Thread {
private int i;
@Override
public void run() {
for(; i < 2; i++) {
System.out.println("继承Thread启动线程:" + getName() + " : " + i);
}
setName("Thread-new");
for(; i < 4; i++) {
System.out.println("重命名后的新线程名:" + Thread.currentThread().getName() + " : " + i);
}
}
public static void main(String[] args) {
System.out.println("main线程:" +Thread.currentThread().getName());
new ThreadTest().start();
}
}
复制代码
main线程:main 继承Thread启动线程:Thread-0 : 0 继承Thread启动线程:Thread-0 : 1 重命名后的新线程名:Thread-new : 2 重命名后的新线程名:Thread-new : 3 复制代码
Runnable接口
的实现类,并重写该接口的 run()
方法,该 run()
是线程执行体; Runnable实现类
的实例,并以此实例作为 Thread
的target来创建 Thread对象
,该Thread对象才是真正的线程对象; start()
方法来启动该线程。 注意: 实现Runable接口和继承Thread的方式区别 :继承Thread创建的线程是创建的Thread子类即可代表线程对象;而实现Runable接口的创建的Runnable对象只能作为线程对象的target。
public class RunnableTest implements Runnable {
private int i;
@Override
public void run() {
//不能直接调用getName()和setName()方法,Runnable只有run方法
for(; i < 5; i++) {
System.out.println("实现Runnable接口创建线程:" + Thread.currentThread().getName() + " : " + i);
}
}
public static void main(String[] args) {
System.out.println("main线程:" +Thread.currentThread().getName());
RunnableTest runnableTest = new RunnableTest();
new Thread(runnableTest).start();
//指定线程名称
RunnableTest runnableTestWithNewName = new RunnableTest();
new Thread(runnableTestWithNewName, "Runnable-Thread-new").start();
}
}
复制代码
public abstract void run();
:Runnable接口中只包含一个抽象方法,Runnable接口是函数式接口,可使用Lambda表达式创建Runnable对象。 main线程:main 实现Runnable接口创建线程:Thread-0 : 0 实现Runnable接口创建线程:Thread-0 : 1 实现Runnable接口创建线程:Thread-0 : 2 实现Runnable接口创建线程:Runnable-Thread-new : 0 实现Runnable接口创建线程:Thread-0 : 3 实现Runnable接口创建线程:Runnable-Thread-new : 1 实现Runnable接口创建线程:Thread-0 : 4 实现Runnable接口创建线程:Runnable-Thread-new : 2 实现Runnable接口创建线程:Runnable-Thread-new : 3 实现Runnable接口创建线程:Runnable-Thread-new : 4 复制代码
public class CallableFutureTest implements Callable<Integer>{
private int i;
@Override
public Integer call(){
for (i = 0; i < 2; i++) {
System.out.println("实现Callable接口创建线程: " + Thread.currentThread().getName() + " : " + i);
}
return i;
}
public static void main(String[] args) {
System.out.println("main线程:" +Thread.currentThread().getName());
long begin = System.currentTimeMillis();
ExecutorService executorService = Executors.newCachedThreadPool();
CallableFutureTest callableFutureTest1 = new CallableFutureTest();
CallableFutureTest callableFutureTest2 = new CallableFutureTest();
FutureTask<Integer> futureTask1 = new FutureTask<>(callableFutureTest1);
FutureTask<Integer> futureTask2 = new FutureTask<>(callableFutureTest2);
executorService.submit(futureTask1);
executorService.submit(futureTask2);
try {
System.out.println("futureTask1: "+ futureTask1.get() + "-futureTask2: " + futureTask2.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}finally {
executorService.shutdown();
}
System.out.println("executor pool: " + executorService.isShutdown());
System.out.println("time: " + (System.currentTimeMillis() - begin));
}
}
复制代码
main线程:main 实现Callable接口创建线程: pool-1-thread-1 : 0 实现Callable接口创建线程: pool-1-thread-2 : 0 实现Callable接口创建线程: pool-1-thread-1 : 1 实现Callable接口创建线程: pool-1-thread-2 : 1 futureTask1: 2-futureTask2: 2 executor pool: true time: 5 复制代码
并行:parallelism,物理上同时执行;多个处理器同时处理多条指令;(单线程永远无法达到并行状态) 并发 :concurrency,逻辑上多个任务交织执行;多个进程指令交替执行,同一时刻只有一条指令执行。(宏观上给人一种错觉是多个进程同时执行)