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是下面几个类的超类,它本身是一个抽象类
1.3)Character
1.4)Boolean
2.Process Vs. Runtime
实验要求: 获得你当前的空闲内存大小;
在java程序中调用其它的应用程序;
3. System – 标准的输入,输出和Java运行时错误输出存储在变量int, out, err中。而System类就是用来保存这这些变量和静态方法的
long start, end;
start=System.currentTimeMillis();
for(int i=0;i<10000;i++);
end=System.currentTimeMillis();
System.out.println(“Elapsed time:”+(end-start));
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");
}
#
本文介绍Java.lang包中的重要类,包括简单类型包装器、Runtime与Process的区别及应用,System类的功能,Math类提供的数学运算方法,以及Runnable接口和Thread类支持的多线程编程技巧。

被折叠的 条评论
为什么被折叠?



