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();
- }
- }
- }
- }
2.实现Runnable接口
/**
* (1): 创建一个类,让该类实现Runnable接口
* (2): 重写run方法
* (3): 创建该类的对象
* (4): 创建Thread类的对象,然后把3中的对象作为参数传递给Thread
* (5): 启动线程
*/
- 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();
- }
- }
- }
- }
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;
- }
- }
4.匿名内部类
/** 使用匿名内部类的方式实现 很少见
* new 类名/接口名() {
* 方法重写 ;
* } ;
*/
public class Thread4 {
public static void main(String[] args) {
//匿名实现多线程
//继承thread类
new Thread(){
public void run(){
for(int x=0;x<111;x++){
System.out.println(getName()+":"+x);
}
}
}.start();
//实现runable
new Thread(new Runnable() {
@Override
public void run() {
for(int x=0;x<100;x++){
System.out.println("wwww");
}
}
}).start();
}
}