您的位置:首页 > 编程语言 > Java开发

Java学习(4)-Java.Lang包

2007-07-04 09:58 260 查看
Introduction: This article describes Java.lang and some important classes which be included.
1.简单类型包装器
1.1)为什么要简单类型包装器(wrapper)如Number, Byte, Double, Long?
答:因为简单类型int, char不是对象层次上封装,简单类型只能处理传值,不能处理引用这有性能上的原因。所以要对这些简单的类型进行封装。
1.2)Number是下面几个类的超类,它本身是一个抽象类


Byte, Double, Float, Integer, Long, Short


认识isInfinite(), IsNaN(),try to consider following code snippet (d1是无穷大,d2是非数字)


Double d1= new Double(1/0.);


Double d2=new Double(0/0.);


数字与字符串的转换
1.3)Character
1.4)Boolean
2.Process Vs. Runtime


抽象类Process 封装了一个进程 ____也就是说一个正在执行的程序。它主是用来保存由Runtime类中的exec()方法返回的类型.常见方法: void destroy() Int waitFor() throws InterrupedException;


Runtime类封装了运行时的环境,它的用法是:一般一实例化一个Runtime对象,而是通过调用静态方法Runtime.getRuntime()而获得对当前Runtime对象的引用。注意:小应用程序(Applets)和其它不可信赖的编码由于因为安全原因不能调用任何Runtime()方法。


这两个对象的最普遍用法是:内存管理和执行进程
实验要求: 获得你当前的空闲内存大小;
在java程序中调用其它的应用程序;
3. System – 标准的输入,输出和Java运行时错误输出存储在变量int, out, err中。而System类就是用来保存这这些变量和静态方法的


System.currentTimeMillis() 可以用来统计出你的程序片断所运行的时间,单位是毫秒
long start, end;
start=System.currentTimeMillis();
for(int i=0;i<10000;i++);
end=System.currentTimeMillis();
System.out.println(“Elapsed time:”+(end-start));


System.arraycopy() 快速复制任意类型的数组


System.getProperty()-得到Java系统所用的环境变量
System.getProperty(“user.dir”)//得到当前用户目录的路径
4. Math


三角函数:
static double sin(double arg) //返回由以弧度为单位由arg指定的角度的正弦值
static double cos(double arg)
static double tan(double arg)
static double asin(double arg)
static double acos(double arg)
static double atan(double arg) / static double atan2(double x, double y)


指数函数
static double exp(double arg) // 返回arg为底的e次幂
static double log(double arg)
static double pow(double y, double x)
static double sqrt(double arg)


舍入函数
abs()
ceil()/floor()
max()/min()
rint()/round()


其它函数
static double IEEEremainder(double dividend, double divisor) // 返回dividend/divisor的余数
static double random() //返回一个伪随机函数
static double toRadians(double angle)
static double toDegree(double angle)

5.Runnable接口/Thread 类,ThreadGroup类—支持多线程编程
下面的代码向你展示了如何生成一个新thread类,因为它摒弃了java2中的suspend()和
resume()的方法,所以我们要着重看一下,它是如何实现这两个方法的.
public class NewThread extends Thread{
boolean suspendFlag;
/** Creates a new instance of NewThread */
public NewThread(String threadname,ThreadGroup tgOb) {
super(tgOb,threadname);
System.out.println("New thread:"+this);
suspendFlag=false;
start();
}

public void run(){
try{
for(int i=5;i>0;i--){
System.out.println(getName()+";"+ i+"runhere");
Thread.sleep(1000);
synchronized(this){
while(suspendFlag){
wait();
}
}
}
}catch (Exception e){
System.out.println("Exception in "+ getName()) ;
}
System.out.println(getName()+"exiting.");
}
void mysuspend(){
System.out.println("suspendHere") ;
suspendFlag=true;
}
synchronized void myresume(){
suspendFlag=false;
notify();
}
}
下面的代码向你显示了使用这个thread类,以及如何从一个线程组中得到相应的类的使用
ThreadGroup groupA=new ThreadGroup("Group A");
ThreadGroup groupB=new ThreadGroup("Group B");
//创建4个线程,并且给它们分类到不同的线程组中
NewThread ob1=new NewThread("One",groupA);
NewThread ob2=new NewThread("Two",groupA);
NewThread ob3=new NewThread("Three",groupB);
NewThread ob4=new NewThread("Four",groupB);
//ThreadGroup.list()方法并无太大作用仅能输出一些ThreadGroup的信息
groupA.list();
//创建一个线程数组用来存放该组内的线程,注意activeCount()方法能返回线程组及该线程组的所有子类的活动线程的数量
Thread tga[]=new Thread[groupA.activeCount()];
//把线程组中所有活动的线程都copy 到一个指定的数组中去,函数原型是
//public int enumerate(Thread[] list)
//Copies into the specified array every active thread in this thread group and its subgroups.
groupA.enumerate(tga);
//suspend each thread
for(int i=0;i<tga.length;i++){
((NewThread)tga[i]).mysuspend(); }
//resume each thread
for(int i=0;i<tga.length;i++){
((NewThread)tga[i]).myresume(); }
//等待所有的线程结束
try{
System.out.println("Waiting for threads to finish.");
ob1.join();
ob2.join();
ob3.join();
ob4.join();
}catch (Exception e){
System.out.println("Exception in Main thread");
}
#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: