国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java-多線程1

2019-11-15 01:14:10
字體:
來源:轉載
供稿:網友
java-多線程1

一、創建多線程的方式1--繼承Thread類

步驟:1自定義類MyThread繼承Thread類 2MyThread里面重寫run()方法 3創建對象 4啟動線程

 1 package test; 2  3 public class MyThread extends Thread{ 4     @Override 5     public void run() { 6         // TODO Auto-generated method stub 7         //super.run(); 8         for(int i=0;i<100;i++){ 9             System.out.
 1 package test; 2  3 public class Test01 { 4     public static void main(String[] args) { 5         MyThread mt=new MyThread(); 6         mt.start(); 7         MyThread mt2=new MyThread(); 8         mt2.start(); 9         10     }11 }

二、線程基礎

1獲得線程名稱

1 getName()

2設置線程名稱

my1.setName("線程1");

3返回當前正在執行的線程的名稱

Thread.currentThread().getName();

4線程優先級

1-10,默認是5

獲得優先級

my1.getPriority()

設置優先級

my1.setPriority(2);

三、線程控制

1線程睡眠

 1 package test; 2  3 public class Test01 { 4     public static void main(String[] args) { 5         MyThread mt=new MyThread();     6         MyThread mt2=new MyThread(); 7         MyThread mt3=new MyThread(); 8          9         mt.setName("進程1");10         mt2.setName("進程2");11         mt3.setName("進程3");12         13         mt.start();14         mt2.start();15         mt3.start();16     }17 }
 1 package test; 2 import java.util.Date; 3 public class MyThread extends Thread{ 4     @Override 5     public void run() { 6         // TODO Auto-generated method stub 7         //super.run(); 8         for(int i=0;i<100;i++){ 9             System.out.println(getName()+": "+i +"  " +new Date());10             //睡眠11             try {12                 Thread.sleep(1000);13             } catch (InterruptedException e) {14                 // TODO Auto-generated catch block15                 e.printStackTrace();16             }17         }18     }19 }

2線程加入

 1 package test; 2  3 public class Test01 { 4     public static void main(String[] args) { 5         MyThread mt=new MyThread();     6         MyThread mt2=new MyThread(); 7         MyThread mt3=new MyThread(); 8          9         mt.setName("進程1");10         mt2.setName("進程2");11         mt3.setName("進程3");12         13         mt.start();14         //線程加入    該線程執行完畢,其他線程才能執行15         try {16             mt.join();17         } catch (InterruptedException e) {18             // TODO Auto-generated catch block19             e.printStackTrace();20         }21         mt2.start();22         mt3.start();23     }24 }
 1 package test; 2 import java.util.Date; 3 public class MyThread extends Thread{ 4     @Override 5     public void run() { 6         // TODO Auto-generated method stub 7         //super.run(); 8         for(int i=0;i<100;i++){ 9             System.out.println(getName()+": "+i +"  " +new Date());10             //睡眠11             try {12                 Thread.sleep(100);13             } catch (InterruptedException e) {14                 // TODO Auto-generated catch block15                 e.printStackTrace();16             }17         }18     }19 }

3線程禮讓

 1 package test; 2  3 public class Test01 { 4     public static void main(String[] args) { 5         MyThread mt=new MyThread();     6         MyThread mt2=new MyThread(); 7         MyThread mt3=new MyThread(); 8          9         mt.setName("進程1");10         mt2.setName("進程2");11             12         mt.start();        13         mt2.start();14 15     }16 }
 1 package test; 2 import java.util.Date; 3 public class MyThread extends Thread{ 4     @Override 5     public void run() { 6         // TODO Auto-generated method stub 7         //super.run(); 8         for(int i=0;i<100;i++){ 9             System.out.println(getName()+": "+i +"  " +new Date());10             //線程禮讓11             Thread.yield();12         }13     }14 }

4守護線程 主線程掛了,守護線程都會掛掉

 1 package test; 2  3 import java.util.Date; 4  5 public class MyThread extends Thread { 6     @Override 7     public void run() { 8         // TODO Auto-generated method stub 9         // super.run();10         for (int i = 0; i < 20; i++) {11             System.out.println(getName() + ": " + i + "  " + new Date());12 13         }14     }15 }
 1 package test; 2  3 public class Test01 { 4     public static void main(String[] args) { 5         MyThread mt=new MyThread();     6         MyThread mt2=new MyThread(); 7         MyThread mt3=new MyThread(); 8          9         mt.setName("進程1");10         mt2.setName("進程2");11         12         //設置守護線程13         mt.setDaemon(true);14         mt2.setDaemon(true);15         16         mt.start();        17         mt2.start();18         19         //改主線程的名字為“劉備”20         Thread.currentThread().setName("劉備");21         for(int x=0;x<5;x++){22             System.out.println(Thread.currentThread().getName()+": "+x);23         }24 25     }26 }

5進程中斷

 1 package test; 2  3 public class Test01 { 4     public static void main(String[] args) { 5         MyThread mt = new MyThread(); 6  7         mt.start(); 8         //進程中斷 9         //超過3秒不醒,則中斷10         try {11             Thread.sleep(3000);12             mt.interrupt();13         } catch (InterruptedException e) {14             // TODO Auto-generated catch block15             e.printStackTrace();16         }17 18     }19 }
 1 package test; 2  3 import java.util.Date; 4  5 public class MyThread extends Thread { 6     @Override 7     public void run() { 8         System.out.println("開始執行:"+new Date()); 9         try {10             Thread.sleep(10000);11         } catch (InterruptedException e) {12             // TODO Auto-generated catch block13             //e.printStackTrace();14             System.out.println("線程被終止了");15         }16         System.out.println("結束執行:"+new Date());17     }18 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿克陶县| 新化县| 双峰县| 文化| 睢宁县| 启东市| 紫金县| 五河县| 秀山| 寿宁县| 青河县| 阜宁县| 泾阳县| 海门市| 驻马店市| 子长县| 陇西县| 呈贡县| 扎囊县| 西峡县| 永丰县| 万全县| 东乡族自治县| 新兴县| 永吉县| 称多县| 巴彦淖尔市| 巴中市| 彰化市| 芜湖县| 双峰县| 原阳县| 五莲县| 荆州市| 兴仁县| 汝城县| 永登县| 五寨县| 蓬溪县| 类乌齐县| 修武县|