线程创建
方式一,继承Thread
public class MyThread extends Thread{
@Override
public void run() {
System.out.println("thread ........");
}
}
public class Test {
public static void main(String[] args) {
new MyThread().start();
}
}
方式二、实现Runnable接口
1、普通方式
public class Test {
public static void main(String[] args) {
new Thread(new MyThread()).start();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println("thread ........");
}
}
2、匿名内部类
public class Test1 {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread ........");
}
}).start();
}
}
3、lambda方式(推荐)
public class Test1 {
public static void main(String[] args) {
new Thread(()->{
System.out.println("thread ........");
}).start();
}
}
方式三、实现Callable接口
01、普通方式
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<String> callable=new MyThread();
FutureTask<String> task=new FutureTask<>(callable);
new Thread(task).start();
String result=task.get();
System.out.println(result);
}
}
class MyThread implements Callable<String> {
@Override
public String call() throws Exception {
return "callable";
}
}
02、匿名内部类
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> task=new FutureTask<>(new Callable<String>() {
@Override
public String call() throws Exception {
return "callable";
}
});
new Thread(task).start();
String result=task.get();
System.out.println(result);
}
}
03、lambda方式(推荐)
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> task=new FutureTask<>(()->{
System.out.println("........");
return "callable";
});
new Thread(task).start();
String result=task.get();
System.out.println(result);
}
}
方式四、线程池
1、普通方式
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService=Executors.newFixedThreadPool(2);
Future<String> future=executorService.submit(new MyThread());
System.out.println(future.get());
}
}
class MyThread implements Callable<String>{
@Override
public String call() throws Exception {
return "callable";
}
}
2、匿名内部类
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService=Executors.newFixedThreadPool(2);
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println("................");
}
});
}
}
3、lambda方式(推荐)
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService=Executors.newFixedThreadPool(2);
final String[] result = {"test result"};
Future<String> future=executorService.submit(()->{
System.out.println("......");
result[0] ="sddssdf";
}, result[0]);
System.out.println(future.get());
}
}
线程创建流程
sequenceDiagram
JVM->>OS: 申请创建线程
OS-->>OS: 开辟内存,创建Thread对象
OS-->>OS: 调度Thread对象
OS->>JVM: 执行成功