1、继承Thread类
public class Demo1 extends Thread{
public Demo1(String name){
super(name);
}
@Override
public void run() {
while(!interrupted()) {
System.out.println(getName()+"线程执行了。。。");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Demo1 d1 = new Demo1("first-thread");
Demo1 d2 = new Demo1("second-thread");
d1.start();
d2.start();
d1.interrupt();
}
}
2、实现Runnable接口
public class Demo2 implements Runnable{
@Override
public void run() {
while(true) {
System.out.println("线程执行了。。。");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Demo2 d1 = new Demo2();
Thread thread = new Thread(d1);
thread.start();
}
}
3、匿名内部类的方式
public class Demo3{
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
System.out.println("线程执行了。。。runnable");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}) {
public void run() {
while(true) {
System.out.println("线程执行了。。。sub");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
4、带返回值的线程
public class Demo4 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("正在进行紧张的计算。。。");
Thread.sleep(2000);
return 1;
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
Demo4 d4 = new Demo4();
FutureTask<Integer> task = new FutureTask<>(d4);
Thread t = new Thread(task);
t.start();
System.out.println("我先干点别的");
Integer result = task.get();
System.out.println("线程执行的结果为:"+result);
}
}
5、定时器(quartz)
public class Demo5{
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("TimerTask is run....");
}
},0,1000);
}
}
6、线程池的实现
public class Demo6{
public static void main(String[] args) {
Executor threadPool2 = Executors.newCachedThreadPool();
for(int i=0;i<100;i++) {
threadPool2.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
});
}
}
}
7、Lambda表达式实现
public class Demo7{
public int add(List<Integer> values){
values.parallelStream().forEach(System.out::print);
return 0;
}
public static void main(String[] args) {
List<Integer> values = Arrays.asList(10,20,30,40);
int result = new Demo7().add(values);
System.out.println("计算结果为:"+result);
}
}
8、Spring实现多线程
@Configuration
@ComponentScan("com.syntops.xtprm.common.utils")
@EnableAsync
public class Config {
}
@Service
public class DemoService {
@Async
public void a() {
while(true) {
System.out.println("a");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Async
public void b() {
while(true) {
System.out.println("b");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
DemoService ds = ac.getBean(DemoService.class);
ds.a();
ds.b();
}
}