線程組:ThreadGroupjava
把多個線程組合到一塊兒,能夠對一批線程進行分類處理,JAVA容許程序直接對線程進行控制ide
獲取線程組:public final ThreadGroup getThreadGroup()優化
獲取線程組的名稱:public final String getName()this
設置新的線程組:ThreadGroup(String name)線程
把線程弄到新線程組裏:Thread(ThreadGroup group,Runnable target,String name)blog
等待喚醒機制代碼優化get
package cn.idcast6; public class Student { private String name; private int age; private boolean flag; public synchronized void set(String name, int age) { if (this.flag) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(this.name+"----"+this.age); } this.flag = false; this.notify(); } }
package cn.idcast6; public class SetStudent implements Runnable { private Student s; public SetStudent(Student s) { this.s = s; } private int x = 0; @Override public void run() { // TODO Auto-generated method stub while (true) { if (x % 2 == 0) { s.set("林青霞", 19); } else { s.set("留意", 1); } x++; } } }
package cn.idcast6; public class GetStudent implements Runnable { private Student s; public GetStudent(Student s) { this.s=s; } @Override public void run() { // TODO Auto-generated method stub while(true) { s.get(); } } }
package cn.idcast6; public class StudentDemo { public static void main(String[] args) { Student s = new Student(); SetStudent ss = new SetStudent(s); GetStudent gs = new GetStudent(s); Thread t1 = new Thread(gs); Thread t2 = new Thread(ss); t1.start(); t2.start(); } }