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();
}
}