1. 程式人生 > 實用技巧 >執行緒建立的方式

執行緒建立的方式

------------恢復內容開始------------

程式執行起來就產生了程序 一個程序有多個執行緒

java預設有main執行緒(使用者執行緒) gc執行緒(守護執行緒)

三種建立方式

  1. 繼承thread類-- 實現了Runnable介面
  2. 實現Runnable介面
  3. 實現callable介面
 1 //建立執行緒的方式1:繼承Thread類,重寫run()方法,呼叫start()開啟執行緒
 2 
 3 //執行緒開啟不一定立即執行由cpu排程執行
 4 public class TestThread extends Thread {
 5     @Override
 6     public
void run() { 7 //run方法執行緒體 8 for (int i = 0; i < 200; i++) { 9 System.out.println("run方法----"+i); 10 } 11 } 12 13 public static void main(String[] args) { 14 //main方法主執行緒 15 16 //建立執行緒物件 17 TestThread testThread1 =new TestThread(); 18
19 //呼叫start()方法 20 testThread1.start(); 21 22 for (int i = 0; i < 1000; i++) { 23 System.out.println("main方法======"+i); 24 } 25 } 26 }

 1 //建立執行緒的方式2: 是西安Runnable介面,重寫run()方法,執行執行緒需要先丟入runnable介面實現類,呼叫start()方法
 2 public class TestThread03 implements Runnable {
3 @Override 4 public void run() { 5 //run方法執行緒體 6 for (int i = 0; i < 200; i++) { 7 System.out.println("run方法----"+i); 8 } 9 } 10 11 public static void main(String[] args) { 12 //main方法主執行緒 13 14 //建立Runnable介面的實現類物件 15 TestThread03 testThread03 =new TestThread03(); 16 //建立執行緒物件,通過執行緒物件開啟執行緒 17 Thread thread = new Thread(testThread03); 18 //呼叫start()方法 19 thread.start(); 20 // new Thread(testThread03).start();---簡寫 21 22 23 for (int i = 0; i < 1000; i++) { 24 System.out.println("main方法======"+i); 25 } 26 } 27 }
 1 //建立執行緒的方式3: 實現Callable介面
 2 public class TestThread05 implements Callable<Boolean> {
 3     private String name;
 4     TestThread05(String name){
 5         this.name = name;
 6     }
 7     @Override
 8     public Boolean call() {
 9         for (int i = 0; i < 1000; i++) {
10             if (i == 999) {
11                 System.out.println(name + "完成");
12             }
13         }
14         return true;
15     }
16     public static void main(String[] args) throws ExecutionException, InterruptedException {
17         TestThread05 t1 = new TestThread05("老大");
18         TestThread05 t2 = new TestThread05("老二");
19         TestThread05 t3 = new TestThread05("老三");
20         //建立執行服務
21         ExecutorService ser = Executors.newCachedThreadPool();
22         //提交執行
23         Future<Boolean> r1 = ser.submit(t1);
24         Future<Boolean> r2 = ser.submit(t2);
25         Future<Boolean> r3 = ser.submit(t3);
26         //獲取結果
27         boolean rs1 = r1.get();
28         boolean rs2 = r2.get();
29         boolean rs3 = r3.get();
30         //關閉服務
31         ser.shutdown();
32     }
33 }

 1 //多個執行緒同時操作一個物件    ----買火車票
 2  2 public class TestThread04 implements Runnable {
 3  3 
 4  4     private int ticketNumber = 10;
 5  5 
 6  6     @Override
 7  7     public void run() {
 8  8 
 9  9         while (true) {
10 10             if (ticketNumber <= 0) break;
11 11 
12 12             System.out.println(Thread.currentThread().getName() + "拿到了第" + ticketNumber-- + "張票");
13 13         }
14 14     }
15 15 
16 16     public static void main(String[] args) {
17 17         //main方法主執行緒
18 18 
19 19         //建立Runnable介面的實現類物件
20 20         TestThread04 testThread04 = new TestThread04();
21 21         new Thread(testThread04, "老大").start();
22 22         new Thread(testThread04, "老二").start();
23 23         new Thread(testThread04, "老三").start();
24 24     }
25 25 }