API解读:Thread

类别:Java 点击:0 评论:0 推荐:

线程是一个和平台关系比较密切的概念,这里我们也不能看出它的具体实现,只能看一下它的表现了.

public class Thread implements Runnable

public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
//以上三个是表示线程的优先级的,默认的都是和父线程具有相同的优先级
public static native Thread currentThread();
//获得当前运行线程的线程实例,注意这是个静态方法
public static native void yield();
//当前线程主动放弃执行,让其他线程可以运行,这个是雷锋精神
public static native void sleep(long millis) throws InterruptedException;
//当前线程自己休眠一会儿,过了这段时间后重新回到可执行状态,有时候为了等别人就自己先睡一会儿
//不过这一觉睡多长时间是比较难确定的
public static void sleep(long millis, int nanos) throws InterruptedException
//这个方法有点多余,把后面的纳秒四舍五入而已,最终的效果或者是sleep(millis)和sleep(millis+1).

构造函数
public Thread();
public Thread(Runnable target)//这个是最常用的构造函数
public Thread(ThreadGroup group, Runnable target)//不是很关心输入哪个线程组
public Thread(String name) //我们似乎不关心线程的名字
public Thread(ThreadGroup group, String name)
public Thread(Runnable target, String name)
public Thread(ThreadGroup group, Runnable target, String name)
public Thread(ThreadGroup group, Runnable target, String name,long stackSize)
public synchronized native void start();
//运行线程用start方法,不知道里面怎么实现了,不过会调用run方法
public void run() {
if (target != null) {
target.run();
}
}
可以看出,如果没有target参数,这个线程什么也不做.

//下面几个方法已经不用了,我也不是很清楚为什么,也不用去管这个了.
public final void stop()
public final synchronized void stop(Throwable obj)
public final void suspend()
public final void resume()


public void interrupt()
//中断这个线程,这个方法除了设置中断状态位,不知道还做了什么

public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
//是否被中断,重置中断状态
public boolean isInterrupted() {
return isInterrupted(false);
}
//是否被中断
private native boolean isInterrupted(boolean ClearInterrupted);

public void destroy() {
throw new NoSuchMethodError();
}
public final native boolean isAlive();
//线程是否还活着,出生了但还没死


public final void setPriority(int newPriority)
public final void setName(String name)
public final String getName()
public final ThreadGroup getThreadGroup()
public static int activeCount() //获得当前线程组获得线程数
public static int enumerate(Thread tarray[]) //将活动线程拷贝到一个数组
public native int countStackFrames();
public final synchronized void join(long millis) throws InterruptedException//等待这个线程死,这个家伙没有耐心,过了一段时间就不等了.
public final synchronized void join(long millis, int nanos) throws InterruptedException //没什么用的方法
 public final void join() throws InterruptedException //这个家伙很有耐心,为了拿遗产,一直等着他老爸死才肯罢休.

public final void setDaemon(boolean on)
public final boolean isDaemon()
public final void checkAccess() //这个好像不应该public
public String toString()
public ClassLoader getContextClassLoader() //我们很少自己用classloader

public void setContextClassLoader(ClassLoader cl)
public static native boolean holdsLock(Object obj)//该线程是否有指定对象的同步锁,用于断言比较多

本文地址:http://com.8s8s.com/it/it13663.htm