Java新建线程的3种方法
来源:互联网 发布:淘宝助手官方 编辑:程序博客网 时间:2024/06/07 06:48
Java新建线程的3种方法
===================
Java创建线程有3种方法:
(1)继承Thread;
(2)实现Runnable接口;
(3)实现Callable接口;
由于Java只支持单继承,所以用继承的方式创建线程,比较死板,不够灵活;用实现接口的方式创建线程,可以实现多个接口,比较灵活。
Runnable和Callable接口的区别:
(1)Callable重写的方法是call(),Runnable重写的方法是run();
(2)Callable的任务执行后可返回值,而Runnable不能返回值;
(3)call方法可以抛出异常,run()不可以;
(4)运行Callable任务可以拿到一个future对象,表示异步计算的结果,
它供检查计算是否完成的方法,以等待计算完成,并检索计算的结果。通过Future对象可以了解任务的执行情况,可取消任务的执行,还可以获取执行的结果。
1.继承Thread
- package com.java.thread;
-
- public class ThreadClient {
-
- public static void main(String[] args) {
- Print p1 = new Print();
- Print p2 = new Print();
- p1.start();
- p2.start();
-
- }
-
- }
-
- class Print extends Thread{
- @Override
- public void run(){
- for(int i=0;i<10;i++){
- System.out.println(Thread.currentThread().getName()+":"+i);
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
执行结果:- Thread-0:0
- Thread-1:0
- Thread-0:1
- Thread-1:1
- Thread-0:2
- Thread-1:2
- Thread-0:3
- Thread-1:3
- Thread-0:4
- Thread-1:4
- Thread-0:5
- Thread-1:5
- Thread-0:6
- Thread-1:6
- Thread-0:7
- Thread-1:7
- Thread-0:8
- Thread-1:8
- Thread-0:9
- Thread-1:9
2.实现Runnable接口- package com.java.thread;
-
- public class ThreadClient1 {
-
- public static void main(String[] args) {
- Runnable p1 = new Salesman("Jack");
- Runnable p2 = new Salesman("Iris");
-
- Thread t1 = new Thread(p1);
- Thread t2 = new Thread(p2);
-
- t1.start();
- t2.start();
- }
-
- }
-
- class Salesman implements Runnable{
-
- private int ticket=100;
- private String name;
- Salesman(String name){
- this.name=name;
- }
-
- @Override
- public void run(){
- while(ticket>0){
- System.out.println(ticket--+" is saled by "+name+","+Thread.currentThread().getName());
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
执行结果:- 10 is saled by Jack,Thread-0
- 10 is saled by Iris,Thread-1
- 9 is saled by Jack,Thread-0
- 9 is saled by Iris,Thread-1
- 8 is saled by Jack,Thread-0
- 8 is saled by Iris,Thread-1
- 7 is saled by Iris,Thread-1
- 7 is saled by Jack,Thread-0
- 6 is saled by Iris,Thread-1
- 6 is saled by Jack,Thread-0
- 5 is saled by Iris,Thread-1
- 5 is saled by Jack,Thread-0
- 4 is saled by Iris,Thread-1
- 4 is saled by Jack,Thread-0
- 3 is saled by Iris,Thread-1
- 3 is saled by Jack,Thread-0
- 2 is saled by Iris,Thread-1
- 2 is saled by Jack,Thread-0
- 1 is saled by Iris,Thread-1
- 1 is saled by Jack,Thread-0
3.实现Callable接口
- package com.java.thread;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
-
- public class CallType {
-
- public static void main(String[] args) {
- ExecutorService es = Executors.newCachedThreadPool();
- List<Future<String>> results = new ArrayList<Future<String>>();
- for(int i=0;i<5;i++){
- results.add(es.submit(new TaskWithResult(i)));
- }
-
- for(Future<String> fs : results){
- try {
- System.out.println(fs.get());
- } catch (InterruptedException | ExecutionException e) {
-
- e.printStackTrace();
- }
- }
- }
- }
-
- class TaskWithResult implements Callable<String>{
- private int id;
- public TaskWithResult(int id){
- this.id = id;
- }
- @Override
- public String call() throws Exception {
- return "result of TaskWithResult" + id;
- }
- }
执行结果:- result of TaskWithResult0
- result of TaskWithResult1
- result of TaskWithResult2
- result of TaskWithResult3
- result of TaskWithResult4