继承Thread类
子类继承Thread类具备多线程能力
启动线程:子类对象.star();
不建议使用,避免oop单继承的局限性
创建的方法:
1.创建一个类,继承Thread类
2.重写run()方法
3.在main()方法里面创建一个线程对象
4.在main()方法里面,调用Thread.star()进行启动
实现Runnable接口
实现runnable具备多线程能力;
启动线程:传入目标对象+Thraad对象.start();
推荐使用,避免单继承的局限性,灵活方便,方便同一个对象被多个线程使用。
创建的方法:
1.创建一个类,实现Runnable接口
2.重写run()方法
3.在main()方法里面创建一个对象
4.传入目标对象到 Thread thread 1= new Thread (new demo1() );
可以多个线程调用一个对象
5.Thread.star()方法进行启动
实现Callable接口:
1.实现Callable接口需要返回值
2.重写call方法需要抛出异常
3.创建目标对象
4.创建执行服务executorservice ser=executor.newfixedThreadpool(1);
5.提交执行Future<boolean> result =ser.submit();
6.获取结果 Boolean r1=result.get();
7.关闭服务:ser.shutdownNow();