如下內容是今日敲的代碼,今日所學習的是多線程,在與多線程類似的是多進程,可是多進程基本是進程與進程中很難有交流的,可是多線程容易交流,並且是並行運行(多線程運行),在一個Thread對象裏,只能調用一個start方法,須要再加一個線程必須再新建一個對象。今日的學習遇到的問題和經驗基本都寫在註釋裏面了。多線程
附代碼:ide
1 package com.wsy.test; 2 3 public class Thread1 extends Thread { 4 public void run() 5 { 6 while(true) 7 { 8 9 10 try 11 { 12 System.out.println("Hello"); 13 Thread.sleep(1000); 14 } catch (InterruptedException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } 18 } 19 } 20 }
1 package com.wsy.test; 2 3 public class ThreadDemo1 { 4 5 public static void main(String[] args) { 6 Thread1 t = new Thread1(); 7 new Thread(t).start(); 8 new Thread(t).start(); 9 new Thread(t).start(); 10 new Thread(t).start(); 11 new Thread(t).start(); 12 } 13 14 }
1 package com.wsy.test; 2 3 public class RunnableTest1 implements Runnable { 4 @Override 5 public void run() 6 { 7 while(true) 8 { 9 System.out.println("Hello"); 10 try { 11 Thread.sleep(2000); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 } 16 } 17 18 public static void main(String[] args) { 19 //在老師的視頻裏出現的直接是27行的那個代碼,因此我想嘗試可否使用這個方法調用start方法 20 //結果編譯器回報錯,顯示start方法未定義 21 // new RunnableTest().start(); 22 // new Thread(new RunnableTest()).run(); 23 //在這裏我還遇到了一個錯誤 我直接調用的run方法,而不是調用的start方法去觸發的run方法 24 //因此它一直在執行run方法裏的死循環 永遠沒法執行到main方法裏面的代碼 25 //通過學習得知當直接調用run方法時,這時候會是串行運行,而使用start方法觸發run方法時並行運行 26 //並行運行(多線程運行) 27 new Thread(new RunnableTest1()).start(); 28 while(true) 29 { 30 System.out.println("World"); 31 try { 32 Thread.sleep(2000); 33 } catch (InterruptedException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 } 38 } 39 40 }
1 package com.wsy.test; 2 3 public class RunnableTest2 implements Runnable { 4 @Override 5 public void run() 6 { 7 while(true) 8 { 9 System.out.println("Hello"); 10 try { 11 Thread.sleep(2000); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 } 16 } 17 public static void main(String[] args) { 18 new Thread(new RunnableTest2()).run(); 19 //在這裏用於沒法執行到21行的循環,由於在run中有個死循環,這是一個串行運行 20 //只有執行完一條語句才能執行下一條語句 21 while(true) 22 { 23 System.out.println("World"); 24 try { 25 Thread.sleep(2000); 26 } catch (InterruptedException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 } 31 32 } 33 34 }
1 package com.wsy.test; 2 3 public class RunnableTest3 implements Runnable { 4 5 @Override 6 public void run() 7 { 8 while(true) 9 { 10 try 11 { 12 System.out.println("Hello"); 13 Thread.sleep(1000); 14 } catch (InterruptedException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } 18 } 19 20 } 21 22 public static void main(String[] args) { 23 new Thread(new RunnableTest3()).start(); 24 for(int i =0; i<10; i++) 25 { 26 try 27 { 28 System.out.println("World"); 29 Thread.sleep(1000); 30 } catch (InterruptedException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 } 35 System.out.println("main end "); 36 } 37 38 }
1 package com.wsy.test; 2 3 public class RunnableTest4 { 4 5 public static void main(String[] args) { 6 RunnableTest3 tt = new RunnableTest3(); 7 //實現了Runnable接口的類沒法直接使用start函數,須要去將該類包裝成一個Thread類去使用start函數 8 // tt.start(); 9 Thread t = new Thread(tt); 10 t.start();//實際上和RunnableTest3使用start方法同樣 11 12 } 13 14 }
1 package com.wsy.test; 2 3 public class RunnableTest5 { 4 public static void main() 5 { 6 Thread1 t1 = new Thread1(); 7 t1.start(); 8 t1.start(); 9 t1.start(); 10 t1.start(); 11 // Thread1 t2 = new Thread1(); 12 // t2.start(); 13 } 14 }