Thread creation
Method 1: Inherit Thread
public class MyThread extends Thread{
@Override
public void run(a) {
System.out.println("thread ........"); }}public class Test {
public static void main(String[] args) {
newMyThread().start(); }}Copy the code
Method 2. Implement the Runnable interface
1. Common way
public class Test {
public static void main(String[] args) {
new Thread(newMyThread()).start(); }}class MyThread implements Runnable{
@Override
public void run(a) {
System.out.println("thread ........"); }}Copy the code
2. Anonymous inner classes
public class Test1 {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run(a) {
System.out.println("thread ........"); } }).start(); }}Copy the code
3. Lambda mode (recommended)
public class Test1 {
public static void main(String[] args) {
new Thread(()->{
System.out.println("thread ........"); }).start(); }}Copy the code
Method 3. Implement Callable interface
01. Common mode
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<String> callable=new MyThread();
FutureTask<String> task=new FutureTask<>(callable);
newThread(task).start(); String result=task.get(); System.out.println(result); }}class MyThread implements Callable<String> {
@Override
public String call(a) throws Exception {
return "callable"; }}Copy the code
02. Anonymous inner classes
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> task=new FutureTask<>(new Callable<String>() {
@Override
public String call(a) throws Exception {
return "callable"; }});newThread(task).start(); String result=task.get(); System.out.println(result); }}Copy the code
03. Lambda Method (recommended)
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> task=new FutureTask<>(()->{
System.out.println("...");
return "callable";
});
newThread(task).start(); String result=task.get(); System.out.println(result); }}Copy the code
Mode 4: Thread pool
1. Common way
// Returns a value,Callable
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService=Executors.newFixedThreadPool(2);
Future<String> future=executorService.submit(newMyThread()); System.out.println(future.get()); }}class MyThread implements Callable<String>{
@Override
public String call(a) throws Exception {
return "callable"; }}Copy the code
2. Anonymous inner classes
// No return value,Runnable
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(a) {
System.out.println("..."); }}); }}Copy the code
3. Lambda mode (recommended)
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()); }}Copy the code
Thread creation process
SequenceDiagram JVM->>OS: Applied for Thread creation OS-->>OS: opened memory, created Thread object OS-->>OS: scheduled Thread object OS->>JVM: Executed successfully