创建线程的三种方式是啥?
文章目录
写在前面
继承Thread类实现多线程
一个类只要继承了Thread就是多线程的实现类,必须重写run方法;
public class TestExtendsThreads {
public static void main(String[] args) {
//创建线程对象
MyThread1 t1 = new MyThread1();
t1.start();//线程启动(JVM调用run方法)
for(int i = 1; i<50; i++){
System.out.println("Main:"+i);
}
}
}
//线程类 自定义线程
class MyThread1 extends Thread{
public void run(){
//线程的执行具有随机性,存储"耗时代码"
for(int i = 1; i<50; i++){
System.out.println("MyThread1:"+i);
}
}
}
实现Runnable接口方式实现多线程
只需要实现一个抽象方法:public void run();
public class TestImplementsRunnable {
public static void main(String[] args) {
//创建资源类对象象
MyRunnable mr1 = new MyRunnable();
//创建线程类对象
Thread t1 = new Thread(mr1);
Thread t2 = new Thread(mr1);
//分别启动线程
t1.start();
t2.start();
for(int i = 1; i<50; i++){
System.out.println("Main:"+i);
}
}
}
class MyRunnable implements Runnable{
public void run(){
for(int i = 1; i<50; i++){
//Thread.currentThread()正在运行的线程
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
- Runnable接口必须要定义一个run的无参数方法
- 实现接口,只是将当前类编成任务类,本身不是个线程
- 更灵活、提供了能力、不影响继承
使用实现Callable接口的方式
public class MyCallable implements Callable {
@Override
public Object call() throws Exception{
for(int i = 0 ;i < 200; i++){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
return null;
}
}
public static void main(String[] args) {
//创建线程池对象
ExecutorService pool = Executors.newFixedThreadPool(2);
//提交异步任务
pool.submit(new MyCallable());
pool.submit(new MyCallable());
//关闭资源
pool.shutdown();
}