線程組: 把多個線程組合到一起。 * 它可以對一批線程進行分類管理,Java允許程式直接對線程組進行控制。 package cn.itcast_06; public class MyRunnable implements Runnable { @Override public void run() ...
線程組: 把多個線程組合到一起。
* 它可以對一批線程進行分類管理,Java允許程式直接對線程組進行控制。
package cn.itcast_06; public class MyRunnable implements Runnable { @Override public void run() { for (int x = 0; x < 100; x++) { System.out.println(Thread.currentThread().getName() + ":" + x); } } }
package cn.itcast_06; /* * 線程組: 把多個線程組合到一起。 * 它可以對一批線程進行分類管理,Java允許程式直接對線程組進行控制。 */ public class ThreadGroupDemo { public static void main(String[] args) { // method1(); // 我們如何修改線程所在的組呢? // 創建一個線程組 // 創建其他線程的時候,把其他線程的組指定為我們自己新建線程組 method2(); // t1.start(); // t2.start(); } private static void method2() { // ThreadGroup(String name) //public ThreadGroup(String name)構造一個新線程組 //name - 新線程組的名稱。 ThreadGroup tg = new ThreadGroup("這是一個新的組"); MyRunnable my = new MyRunnable(); // Thread(ThreadGroup group, Runnable target, String name) //將線程t1放到線程組tg中並給線程分別取名為"林青霞","劉意" Thread t1 = new Thread(tg, my, "林青霞"); Thread t2 = new Thread(tg, my, "劉意"); //public final ThreadGroup getThreadGroup()返回該線程所屬的線程組並獲取此線程組的名稱 System.out.println(t1.getThreadGroup().getName()); System.out.println(t2.getThreadGroup().getName()); //通過組名稱設置後臺線程,表示該組的線程都是後臺線程 tg.setDaemon(true); } private static void method1() { MyRunnable my = new MyRunnable(); //public Thread(Runnable target,String name)分配新的 Thread 對象 //target - 其 run 方法被調用的對象。 //name - 新線程的名稱 Thread t1 = new Thread(my, "林青霞"); Thread t2 = new Thread(my, "劉意"); // 我不知道他們屬於那個線程組,我想知道,怎麼辦 //public final ThreadGroup getThreadGroup()返回該線程所屬的線程組 // 線程類裡面的方法:public final ThreadGroup getThreadGroup() ThreadGroup tg1 = t1.getThreadGroup(); ThreadGroup tg2 = t2.getThreadGroup(); // 線程組裡面的方法:public final String getName() String name1 = tg1.getName(); String name2 = tg2.getName(); System.out.println(name1); System.out.println(name2); // 通過結果我們知道了:線程預設情況下屬於main線程組 // 通過下麵的測試,你應該能夠看到,默任情況下,所有的線程都屬於同一個組 System.out.println(Thread.currentThread().getThreadGroup().getName()); } }
生產者和消費者之等待喚醒機制的代碼優化
package cn.itcast_07; public class Student { private String name; private int age; private boolean flag; // 預設情況是沒有數據,如果是true,說明有數據 public synchronized void set(String name, int age) { // 如果有數據,就等待 if (this.flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 設置數據 this.name = name; this.age = age; // 修改標記 this.flag = true;
//喚醒等待的單個線程 this.notify(); } public synchronized void get() { // 如果沒有數據,就等待 if (!this.flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 獲取數據 System.out.println(this.name + "---" + this.age); // 修改標記 this.flag = false; this.notify(); } }
線程實現類---設置和獲取信息
package cn.itcast_07; public class SetThread implements Runnable { private Student s; private int x = 0; public SetThread(Student s) { this.s = s; } @Override public void run() { while (true) { if (x % 2 == 0) { s.set("林青霞", 27); } else { s.set("劉意", 30); } x++; } } }
package cn.itcast_07; public class GetThread implements Runnable { private Student s; public GetThread(Student s) { this.s = s; } @Override public void run() { while (true) { s.get(); } } }
測試
package cn.itcast_07; /* * 分析: * 資源類:Student * 設置學生數據:SetThread(生產者) * 獲取學生數據:GetThread(消費者) * 測試類:StudentDemo * * 問題1:按照思路寫代碼,發現數據每次都是:null---0 * 原因:我們在每個線程中都創建了新的資源,而我們要求的時候設置和獲取線程的資源應該是同一個 * 如何實現呢? * 在外界把這個數據創建出來,通過構造方法傳遞給其他的類。 * * 問題2:為了數據的效果好一些,我加入了迴圈和判斷,給出不同的值,這個時候產生了新的問題 * A:同一個數據出現多次 * B:姓名和年齡不匹配 * 原因: * A:同一個數據出現多次 * CPU的一點點時間片的執行權,就足夠你執行很多次。 * B:姓名和年齡不匹配 * 線程運行的隨機性 * 線程安全問題: * A:是否是多線程環境 是 * B:是否有共用數據 是 * C:是否有多條語句操作共用數據 是 * 解決方案: * 加鎖。 * 註意: * A:不同種類的線程都要加鎖。 * B:不同種類的線程加的鎖必須是同一把。 * * 問題3:雖然數據安全了,但是呢,一次一大片不好看,我就想依次的一次一個輸出。 * 如何實現呢? * 通過Java提供的等待喚醒機制解決。 * * 等待喚醒: * Object類中提供了三個方法: * wait():等待 * notify():喚醒單個線程 * notifyAll():喚醒所有線程 * 為什麼這些方法不定義在Thread類中呢? * 這些方法的調用必須通過鎖對象調用,而我們剛纔使用的鎖對象是任意鎖對象。 * 所以,這些方法必須定義在Object類中。 * * 最終版代碼中: * 把Student的成員變數給私有的了。 * 把設置和獲取的操作給封裝成了功能,並加了同步。 * 設置或者獲取的線程裡面只需要調用方法即可。 */ public class StudentDemo { public static void main(String[] args) { //創建資源 Student s = new Student(); //設置和獲取的類 SetThread st = new SetThread(s); GetThread gt = new GetThread(s); //線程類 Thread t1 = new Thread(st); Thread t2 = new Thread(gt); //啟動線程 t1.start(); t2.start(); } }