Java基础 -> 创建多线程(thread,runnable,callable,线程池)

方式一:

继承于thread类
1:创建一个继承于Thread的子类(大写)
2:重写thread类中run()方法–要操作的程序重写在run()方法中;
3:创建子类的对象(new)
4:通过对象调用start()方法,启动线程且start()方法会执行run()方法;

注:如果调用run()方法,就没有执行多线程,就只是在主线程里执行。
必须用start()方法,启动线程并且自动调用run()方法才是多线程。

public class day01 {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        //通过对象调用start()方法;运行MyThread
        myThread.start();

		//第二个线程,已经启动的1,不能在用,需要new一个新的启动
        MyThread myThread2 = new MyThread();
        myThread2.start();
    }
}
class MyThread extends Thread{
    public void run(){
//        重写程序,
    }
}
/**
 *  //匿名创建多线程
 *  new Thread(){}.start();
 */
public class day01 {
    public static void main(String[] args) {
        //匿名创建多线程
       new Thread(){
           @Override
           public void run() {
               super.run();
           }
       }.start();
    }
}

优先级:

MAX_PRIORITY:10
MIN_PRIORITY1
NORM_PRIORITY:5—默认优先级
getPriority(),获取优先级
setPriority(int t),设置优先级

对象名.Priority(Thread.MAX_PRIORITY);//1-10

方式二

实现runnable接口
1:创建一个实现了runnable接口的类
2:实现类重写run()方法;
3:创建实现类的对象
4:将实现类的对象作为参数传递到Thread类的构造器中,创建Thread类的对象
5:通过Thread类的对象调用start()

/*
实现runnable接口
1:创建一个实现了runnable接口的类
2:实现类重写run()方法;
3:创建实现类的对象
4:将实现类的对象作为参数传递到Thread类的构造器中,创建Thread类的对象
5:通过Thread类的对象调用start()
 */
public class day011 {
    public static void main(String[] args) {
        //3:创建实现类的对象
        MThread mThread = new MThread();
        //4:将实现类的对象作为参数传递到Thread类的构造器中,创建Thread类的对象
        Thread thread = new Thread(mThread);
        //5:通过Thread类的对象调用start()
        thread.start();
    }

}
/*
    1.2创建MThread类实现runnable接口,重写run方法。

 */
class MThread implements Runnable{

    @Override
    public void run() {

    }
}

线程:主线程(main),垃圾回收线程,异常处理线程

jdk5.0新增创建方式

新增一

新增接口callable
相比runnable更加强大


/**
 * 1有返回值
 * 2可以抛出异常
 * 3泛型返回值
 * 4用FutureTask类获取返回结果
 */
public class Test8262 {
    public static void main(String[] args) {
        //3 创建实现类对象
        NewThread newThread = new NewThread();
        //4 将实现类对象newThread传递到FutureTask类构造器中,且生成对象
        FutureTask futureTask = new FutureTask(newThread);
        //5 将FutureTask对象传递到Thread构造器中,且生成对象,开启线程
        new Thread(futureTask).start();
        try {
            Object o = futureTask.get();//获取call返回值
            System.out.println(o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 1 创建一个实现类
 */
class NewThread implements Callable{
//2 将操作的代码写在call中
    @Override
    public Object call() throws Exception {
        int aa = 10;
        System.out.println("asdf");
        //万物皆可object,自动转型integer
        return aa;
    }
}

新增二

使用线程池

package test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test8263 {
    public static void main(String[] args) {
        //1 创建一个线程池,线程数10
        ExecutorService service = Executors.newFixedThreadPool(10);
        //2 执行线程操作
        service.execute(new Test3());//3 适用于执行实现runnable接口的类
        service.submit(new Test4());//3 适用于执行实现callale接口的类

        //4 关闭线程池
        service.shutdown();
    }
}

/**
 * 线程池:提前创建多个线程,放入线程池中,使用时直接获取,使用完放回
 *  1 速度快,减少创建时间
 *  2 重复利用,降低资源
 *  3 便于管理
 *
 */
class Test3 implements Runnable{

    @Override
    public void run() {
        System.out.println("runnable");
    }
}
class Test4 implements Callable{

    @Override
    public Object call() throws Exception {
        System.out.println("callable");
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值