Java.lang包是Java中使用最广泛的一个包,它包含很多定义的类和接口。
java.lang包包括以下这些类:
Boolean | Byte | Character | Class | ClassLoader | Compiler | Double | Enum | Float |
InheritableThreadLocal | Integer | Long | Math | Number | Object | Package | Process | ProcessBuilder |
Runtime | RuntimePermission | SecurityManager | Short | StackTraceElement | StrictMath | String | StringBuffer | StringBuilder |
System | Thread | ThreadGroup | ThreadLocal | Throwable | void |
java.lang包括以下这些接口:
Appendalbe | Comparable | Runnable | CharSequence | Iterable | Cloneable | Readable |
1、在Float和Double中提供了isInfinite()和isNaN()方法,用来检验两个特殊的double和float值:无穷值和NaN(非数字)。
2、Process抽象类。抽象的Process类封装了一个进程 process, 即一个执行程序,它主要作为对象类型的超类,该对象由Runtime类中的exec()方法创建,或由ProcessBuilder类中的start()创建。
3、Runtime类。Runtime类封装运行时的环境。一般不能实例化一个Runtime对象,但是可以通过调用静态方法Runtime.getRuntime()得到一个当前Runtime对象的引用。一旦获得当前Runtime对象的引用,就可以调用几个方法来控制Java虚拟机的状态和行为。Runtime类中比较常用的几个方法:
Process exec(String progName) throws IOException 作为一个单独的进程执行progName指定的程序。返回一个描述新进程的Process类的对象。
long freeMemory() 返回Java运行时系统可以利用的空闲内存的近似字节数。
void gc() 开始垃圾回收。
long totalMemory() 返回程序可以利用的类存总字节数。
使用exec()执行其他程序:
1 package com.hujianjie.demo; 2 3 public class EcecDemo { 4 5 /** 6 * 利用exec()打开指定的程序 7 */ 8 public static void main(String[] args) { 9 Runtime r = Runtime.getRuntime(); 10 Process p = null; 11 try{ 12 p = r.exec("D:\Program Files\Dev-Cpp\devcpp.exe"); 13 }catch(Exception e){ 14 e.printStackTrace(); 15 System.out.println("Error"); 16 } 17 } 18 19 }
4、System类。System类比较常用,其中容易忽略的是currentTimeMillis()方法是为程序执行计时;arraycopy()方法可以迅速从一个地方将任何类型的数组复制到另一个地方,其与等价的循环相比,该方法快很多;getProperty()方法可以得到不同环境变量的值。
5、Runnable接口、Thread和ThreadGroup类支持多线程编程。Runnable接口必须由一个可以启动单独的执行线程的类实现,Runnable接口只定义了一个抽象方法run(),它是线程的入口点,创建线程必须实现该方法。 Thread 类创建一个新的执行线程,它定义了下面常用的构造函数:
1 Thread() 2 Thread(Runnable threadOb) 3 //threadOb是实现Runnable接口的类的一个实例,它定义了线程在何处开始执行 4 //线程的名称由threadName指定 5 Thread(Runnable threadOb, String threadName) 6 Thread(String threadName) 7 Thread(ThreadGroup groupOb, Runnable threadOb) 8 Thread(ThreadGroup groupOb, Runnable threadOb, String threadName) 9 Thread(ThreadGroup groupOb, String threadName)
下面的程序创建了两个具有线程的线程组,演示了线程组的用法:
1 package com.hujianjie.demo; 2 3 class NewThread extends Thread{ 4 boolean suspendFlag; 5 NewThread(String threadname, ThreadGroup tgob){ 6 super(tgob,threadname); 7 System.out.println("New thread:"+this); 8 suspendFlag =false; 9 start();//Start the thread 10 } 11 public void run(){ 12 try{ 13 for(int i =6;i>0;i--){ 14 System.out.println(getName()+": "+i); 15 Thread.sleep(1000); 16 synchronized(this){ 17 while(suspendFlag){ 18 wait(); 19 } 20 } 21 } 22 }catch(Exception e){ 23 System.out.println("Exception in "+getName()); 24 } 25 System.out.println(getName()+" exiting."); 26 } 27 void mysuspend(){ 28 suspendFlag = true; 29 } 30 synchronized void myresume(){ 31 suspendFlag = false ; 32 notify(); 33 } 34 35 } 36 37 public class ThreadGroupDemo { 38 39 /** 40 * @param args 41 */ 42 public static void main(String[] args) { 43 ThreadGroup groupA = new ThreadGroup("Group A"); 44 ThreadGroup groupB = new ThreadGroup("Group B"); 45 NewThread ob1 = new NewThread("One",groupA); 46 NewThread ob2 = new NewThread("Two",groupA); 47 NewThread ob3 = new NewThread("Three",groupB); 48 NewThread ob4 = new NewThread("Four",groupB); 49 System.out.println(" Here is output from list():"); 50 groupA.list(); 51 groupB.list(); 52 System.out.println(); 53 System.out.println("Suspending Group A"); 54 Thread tga[] = new Thread[groupA.activeCount()]; 55 groupA.enumerate(tga); 56 for(int i=0;i<tga.length;i++){ 57 ((NewThread)tga[i]).mysuspend(); //suspend threads in group 58 } 59 try{ 60 Thread.sleep(4000); 61 }catch(Exception e){ 62 System.out.println("Main thread interrupted."); 63 } 64 System.out.println("Resuming Group A"); 65 for(int i=0;i<tga.length;i++){ 66 ((NewThread)tga[i]).myresume(); //resume threads in group 67 } 68 try{ 69 System.out.println("Waiting for threads to finish."); 70 ob1.join(); 71 ob2.join(); 72 ob3.join(); 73 ob4.join(); 74 }catch(Exception e){ 75 System.out.println("Exception in Main thread!"); 76 } 77 System.out.println("Main thread exiting!"); 78 } 79 80 }
运行的结果如下:
1 New thread:Thread[One,5,Group A] 2 New thread:Thread[Two,5,Group A] 3 One: 6 4 New thread:Thread[Three,5,Group B] 5 Two: 6 6 New thread:Thread[Four,5,Group B] 7 Three: 6 8 9 Here is output from list(): 10 Four: 6 11 java.lang.ThreadGroup[name=Group A,maxpri=10] 12 Thread[One,5,Group A] 13 Thread[Two,5,Group A] 14 java.lang.ThreadGroup[name=Group B,maxpri=10] 15 Thread[Three,5,Group B] 16 Thread[Four,5,Group B] 17 18 Suspending Group A 19 Four: 5 20 Three: 5 21 Four: 4 22 Three: 4 23 Four: 3 24 Three: 3 25 Resuming Group A 26 Two: 5 27 Waiting for threads to finish. 28 One: 5 29 Four: 2 30 Three: 2 31 Two: 4 32 One: 4 33 Four: 1 34 Three: 1 35 Two: 3 36 One: 3 37 Four exiting. 38 Three exiting. 39 Two: 2 40 One: 2 41 Two: 1 42 One: 1 43 One exiting. 44 Two exiting. 45 Main thread exiting!