Java基礎__Java中多線程那些事

來源:互聯網
上載者:User

標籤:ack   ext   inter   method   測試   計時   ++   tar   建立   

(未完)

 

一、測試

查看線程的運行狀態

package Cynical_Gary;public class ThreadState implements Runnable{    public synchronized void waitForASecond() throws InterruptedException{        //使當前線程等待0.5秒或其它線程調用notify()或notifyAll()方法        wait(500);    }        public synchronized void waitForYears() throws InterruptedException{        //使當前現場永久等待,直到其他線程調用notify()或notifyAll()方法        wait();    }        public synchronized void notifyNow() throws InterruptedException{        //喚醒由調用wait()方法進入等待狀態的線程        notify();    }        @Override    public void run() {        // TODO Auto-generated method stub        try{            //在新線程中運行waitForASecond()方法            waitForASecond();            //在新線程中運行waitForYears();方法            waitForYears();        }catch(InterruptedException e){            e.printStackTrace();        }    }}
ThreadState類
package Cynical_Gary;public class Gary_Text {    public static void main(String[] args) throws InterruptedException{        //建立State對象        ThreadState state = new ThreadState();        //利用State對象建立Thread對象        Thread thread = new Thread(state);        //輸出線程狀態        System.out.println("建立線程:"+thread.getState());        //啟動新線程        thread.start();        //輸出線程狀態        System.out.println("啟動線程:"+thread.getState());        //當前線程休眠0.1秒,使新線程運行waitForASecond()方法        Thread.sleep(100);        //輸出線程狀態        System.out.println("計時等待:"+thread.getState());        //當前線程休眠1秒,使新線程運行waitForYears()方法        Thread.sleep(1000);        //輸出線程狀態        System.out.println("等待線程:"+thread.getState());        state.notifyNow();        System.out.println("喚醒線程:"+thread.getState());        //當前線程休眠1秒,使新線程結束        Thread.sleep(1000);        //輸出線程狀態        System.out.println("終止線程:"+thread.getState());    }}/* 輸出: 建立線程:NEW啟動線程:RUNNABLE計時等待:TIMED_WAITING等待線程:WAITING喚醒線程:RUNNABLE終止線程:TERMINATED */
Text類()測試

 

package Cynical_Gary;import java.util.ArrayList;import java.util.List;public class Gary_Text {    //獲得根線程組    private static ThreadGroup getRootThreadGroup(){        //獲得當前線程組        ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();        while(true){            //getParent()方法的傳回值非空則不是根線程組            if(rootGroup.getParent()!=null){            //獲得父線程組            rootGroup = rootGroup.getParent();                }else{            //如果達到根線程組則退出迴圈                break;            }        }        //返回根線程組        return rootGroup;    }        //獲得給定線程組中所有線程名    public static List<String> getThreads(ThreadGroup group){        //建立儲存線程名的列表        List<String> threadList = new ArrayList<String>();        //根據活動線程數穿件線程數組        Thread[] threads = new Thread[group.activeCount()];        //複製線程到線程數組        int count = group.enumerate(threads,false);        //遍曆線程數組將線程名及所在組儲存到列表中        for(int i=0;i<count;i++){            threadList.add(group.getName()+"線程組:"+threads[i].getName());        }        //返回列表        return threadList;    }        //獲得給定線程組中子線程    public static List<String> getThreadGroups(ThreadGroup group){        //建立儲存線程名的列表        List<String> threadList = getThreads(group);        //建立線程組數組        ThreadGroup[] groups = new ThreadGroup[group.activeGroupCount()];        //複製子線程數組到線程組資料        int count = group.enumerate(groups,false);        //遍曆所有子線程數組        for(int i=0;i<count;i++){            //利用getThreads()方法獲得線程名列表            threadList.addAll(getThreads(groups[i]));        }        //返回所有線程名        return threadList;    }        public static void main(String[] args){        for(String string:getThreadGroups(getRootThreadGroup())){            //遍曆輸出資料行表中的字串            System.out.println(string);        }    }}/* 輸出:system線程組:Reference Handlersystem線程組:Finalizersystem線程組:Signal Dispatchersystem線程組:Attach Listenermain線程組:main*/
查看JVM中的線程名

 

Java基礎__Java中多線程那些事

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.