kvm在内部定义了很多方法来操作线程,如下:
/* 线程的构造 */
void InitializeThreading(INSTANCE_CLASS, ARRAY);
THREAD getVMthread(JAVATHREAD_HANDLE);
/* 线程操作 */
void initThreadBehavior(THREAD, METHOD, OBJECT);
void startThread(THREAD);
void stopThread(void);
void interruptThread(THREAD);
void handlePendingInterrupt(void);
void DismantleThread(THREAD);
void suspendThread(void);
void suspendSpecificThread(THREAD);
void resumeThread(THREAD);
void resumeSpecificThread(THREAD);
int activeThreadCount(void);
int isActivated(THREAD);
THREAD removeFirstRunnableThread(void);
// 异步操作
void asyncFunctionProlog(void);
void asyncFunctionEpilog(THREAD);
void RundownAsynchronousFunctions(void);
void RestartAsynchronousFunctions(void);
void enterSystemCriticalSection(void);
void exitSystemCriticalSection(void);
// Timer操作
void registerAlarm(THREAD thread, long64 delta, void (*)(THREAD));
void checkTimerQueue(ulong64 *nextTimerDelta);
bool_t inTimerQueue(THREAD thread);
// Monitor操作
enum MonitorStatusType monitorEnter(OBJECT object);
enum MonitorStatusType monitorExit(OBJECT object, char **exceptionName);
enum MonitorStatusType monitorWait(OBJECT object, long64 time);
enum MonitorStatusType monitorNotify(OBJECT object, bool_t wakeAll);
void clearObjectMonitor(OBJECT object);
long* monitorHashCodeAddress(OBJECT object);
以上这些方法会在后续文章中一一介绍的. 不过在这里,还是先看看kvm中java.lang.thread的实现吧.其代码位于api/src/java/lang/Thread.java.代码如下:
public
class Thread implements Runnable {
/* 线程的优先级 */
private int priority = NORM_PRIORITY;
/* 内部的线程队列 */
private Thread threadQ;
/* 要运行的对象*/
private Runnable target;
/* 线程名称 */
private char name[];
/**
* 最小的线程优先级
*/
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();
/* 线程number */
private static int threadInitNumber;
private static synchronized int nextThreadNum() {
return ++threadInitNumber;
}
// 暂停当前正在执行的线程,同时让其他线程执行
public static native void yield();
public static native void sleep(long millis) throws InterruptedException;
// 初始化线程
private void init(Runnable target, String name) {
Thread parent = currentThread();
this.target = target;
this.name = name.toCharArray();
this.priority = parent.getPriority();
setPriority0(priority);
}
public Thread() {
init(null, "Thread-" + nextThreadNum());
}
public Thread(String name) {
init(null, name);
}
public Thread(Runnable target) {
init(target, "Thread-" + nextThreadNum());
}
public Thread(Runnable target, String name) {
init(target, name);
}
// 启动线程
public synchronized native void start();
public void run() {
if (target != null) {
target.run();
}
}
public void interrupt() {
interrupt0();
}
public final native boolean isAlive();
public final void setPriority(int newPriority) {
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
setPriority0(priority = newPriority);
}
public final int getPriority() {
return priority;
}
public final String getName() {
return String.valueOf(name);
}
// 存活线程的数量
public static native int activeCount();
public synchronized final void join() throws InterruptedException {
while (isAlive()) {
wait(1000);
}
}
public String toString() {
return "Thread[" + getName() + "," + getPriority() + "]";
}
private native void setPriority0(int newPriority);
private native void interrupt0();
}
这部分的代码还是比较简单的.在这里定义了线程的优先级,而在kvm内部,也定义了线程的优先级,正好对应.如下:
#define MAX_PRIORITY 10 /* These constants must be the same */
#define NORM_PRIORITY 5 /* as those defined in Thread.java */
#define MIN_PRIORITY 1
而Thread类的父类-Runnable定义如下:
public
interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
Thread类的读取
以上介绍了在java语言层面,thread的定义.既然是个类,那么就肯定需要读取到kvm中.而对于thread类的读取,是在InitializeJavaSystemClasses中进行的.代码如下:
JavaLangThread = (INSTANCE_CLASS)getClass("java/lang/Thread");
关于getClass,可以参考如下链接:
初始化多线程系统
此步骤是在KVM_Start中进行的,代码如下:
InitializeThreading(mainClass, arguments);
关于这点可以参考如下文章:
关于此处,创建了主线程.如图所示:
Thread的initializeClass
以上只是在kvm构建了Thread所对应的class,还没有进行初始化.此步骤是在KVM_Start中进行的,代码如下:
initializeClass(JavaLangThread);
关于这点可以参考如下文章: