创建线程的方式
1.Class Thread
package com.lisus2000.style;
public class Test01 extends Thread{
@Override
public void run() {
System.out.println("继承Thread类,重写run方法实现线程.");
}
public static void main(String[] args) throws InterruptedException {
//创建线程
Test01 t1 = new Test01();
//启动线程 (一定要调start方法)
t1.start();
//创建线程
Test01 t2 = new Test01();
//启动线程
t2.start();
Thread.sleep(10L);
//main线程执行
System.out.println("main方法执行");
}
}
2.Interface Runnable
package com.lisus2000.style;
/**
* 线程创建方式二:实现Runnable接口,实现run方法
*
*/
public class Test02 implements Runnable {
@Override
public void run() {
System.out.println("实现Runnable接口,覆盖run方法实现线程.");
}
public static void main(String[] args) {
//创建线程
Thread t1 = new Thread(new Test02());
//启动线程
t1.start();
//创建线程
Thread t2 = new Thread(new Test02());
//启动线程
t2.start();
//main线程执行
System.out.println("main方法执行");
}
}
3.Interface Callable
package com.lisus.sytle;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* 线程创建方式三:实现Callable接口,实现call()方法
*/
public class Test03 implements Callable<String> {
@Override
public String call() throws Exception {
return "Thread execute.txt finished";
}
public static void main(String[] args) {
//创建线程
FutureTask<String> futureTask=new FutureTask<>(new Test03());
//启动线程
new Thread(futureTask).start();
try {
//等待任务执行完毕,并返回结果
String result = futureTask.get();
System.out.println(result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
//main线程执行
System.out.println("main方法执行");
}
}
4.Executors
package com.lisus.sytle;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 线程创建方式四:基于线程池的方式实现
*
*/
public class Test04 implements Runnable{
@Override
public void run() {
System.out.println("线程执行");
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i=0;i<10;i++){
Test04 thread=new Test04();
executorService.execute(thread);
}
//关闭线池程
executorService.shutdown();
//main线程执行
System.out.println("main方法执行");
}
}
5.ThreadPoolTaskExecutor
package com.bjpowernode.style;
import com.bjpowernode.service.MyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@ComponentScan(basePackages = {"com.lisus2000.service"})
@EnableAsync
public class Test05 {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Test05.class);
//<bean id="applicationTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
//ThreadPoolTaskExecutor applicationTaskExecutor = context.getBean(ThreadPoolTaskExecutor.class);
//applicationTaskExecutor.execute.txt(() -> System.out.println("异步线程执行1......"));
//applicationTaskExecutor.submit(() -> System.out.println("异步线程执行2......"));
MyService myService = context.getBean(MyService.class);
myService.test2();
System.out.println("主线程执行结束......");
}
}
标签:方式,创建,System,线程,println,import,main,public
From: https://blog.csdn.net/lisus2007/article/details/137432277