(not finished)
First, testing
To view the running state of a thread
Packagecynical_gary; Public classThreadStateImplementsrunnable{ Public synchronized voidWaitforasecond ()throwsinterruptedexception{//causes the current thread to wait 0.5 seconds or other threads to call the Notify () or Notifyall () methodWait (500); } Public synchronized voidWaitforyears ()throwsinterruptedexception{//causes the current field to wait forever until another thread calls the Notify () or Notifyall () methodwait (); } Public synchronized voidNotifynow ()throwsinterruptedexception{//Wake up the thread that calls the wait () method into the waiting statenotify (); } @Override Public voidrun () {//TODO auto-generated Method Stub Try{ //Run the Waitforasecond () method in a new threadWaitforasecond (); //run Waitforyears () in a new thread;Waitforyears (); }Catch(interruptedexception e) {e.printstacktrace (); } }}ThreadState class
Packagecynical_gary; Public classGary_text { Public Static voidMain (string[] args)throwsinterruptedexception{//Create a state objectThreadState state =NewThreadState (); //Create a thread object with the state objectThread thread =NewThread (state); //Output Thread StatusSYSTEM.OUT.PRINTLN ("New Thread:" +thread.getstate ()); //start a new threadThread.Start (); //Output Thread StatusSYSTEM.OUT.PRINTLN ("Boot thread:" +thread.getstate ()); //The current thread sleeps for 0.1 seconds, allowing the new thread to run the Waitforasecond () methodThread.Sleep (100); //Output Thread StatusSystem.out.println ("Timed wait:" +thread.getstate ()); //The current thread sleeps for 1 seconds, allowing the new thread to run the Waitforyears () methodThread.Sleep (1000); //Output Thread StatusSystem.out.println ("Waiting thread:" +thread.getstate ()); State.notifynow (); System.out.println ("Wake-up Thread:" +thread.getstate ()); //The current thread sleeps for 1 seconds so that the new thread endsThread.Sleep (1000); //Output Thread StatusSYSTEM.OUT.PRINTLN ("Terminating thread:" +thread.getstate ()); }}/*output: New thread: Start thread: runnable timed wait: timed_waiting waiting thread: Waiting wakeup Thread: runnable terminating thread: TERMINATED*/text Class () test
Packagecynical_gary;Importjava.util.ArrayList;Importjava.util.List; Public classGary_text {//get root Thread group Private StaticThreadgroup Getrootthreadgroup () {//get current Thread groupThreadgroup Rootgroup =Thread.CurrentThread (). Getthreadgroup (); while(true){ //The return value of the GetParent () method is non-null and is not a root thread group if(Rootgroup.getparent ()! =NULL){ //Get parent Thread GroupRootgroup =rootgroup.getparent (); }Else{ //Exit loop If the root thread group is reached Break; } } //returns the root thread group returnRootgroup; } //get all the thread names in a given thread group Public StaticList<string>getthreads (threadgroup group) {//Create a list of Save thread namesList<string> threadlist =NewArraylist<string>(); //Threading An array of threads based on the number of active threadsthread[] Threads =NewThread[group.activecount ()]; //copy thread to thread array intCount = group.enumerate (threads,false); //iterate through the thread array to save the thread name and group to the list for(inti=0;i<count;i++) {Threadlist.add (Group.getname ()+ "Thread Group:" +threads[i].getname ()); } //Return to list returnthreadlist; } //Get a child thread in a given thread group Public StaticList<string>getthreadgroups (threadgroup group) {//Create a list of Save thread namesList<string> threadlist =getthreads (group); //creating an array of thread groupsThreadgroup[] Groups =NewThreadgroup[group.activegroupcount ()]; //copying child thread arrays to thread group data intCount = group.enumerate (groups,false); //iterate through all the child thread arrays for(inti=0;i<count;i++){ //using the Getthreads () method to obtain a list of thread namesThreadlist.addall (Getthreads (groups[i)); } //return all thread names returnthreadlist; } Public Static voidMain (string[] args) { for(String string:getthreadgroups (Getrootthreadgroup ())) {//traversing strings in the output listSystem.out.println (string); } }}/*Output: System thread Group: Reference handlersystem Thread Group: Finalizersystem thread Group: Signal dispatchersystem Thread Group: Attach Listenermain Thread Group: main*/to view the name of a thread in the JVM
Threads in Java Foundation __java