Hadoop源码学习笔记(3)
——初览DataNode及学习线程
进入了main函数,我们走出了第一步,接下来看看再怎么走:
-
public class DataNode extends Configured implements InterDatanodeProtocol,
-
ClientDatanodeProtocol, FSConstants, Runnable {
-
-
public static DataNode createDataNode(String args[], Configuration conf)
-
throws IOException {
-
DataNode dn = instantiateDataNode(args, conf);
-
runDatanodeDaemon(dn);
-
return dn;
-
}
-
-
public static void runDatanodeDaemon(DataNode dn) throws IOException {
-
dn.dataNodeThread = new Thread(dn, dnThreadName);
-
dn.dataNodeThread.setDaemon(true);
-
dn.dataNodeThread.start();
-
}
-
}
-
-
public static DataNode instantiateDataNode(String args[], Configuration conf)
-
throws IOException {
-
return makeInstance(dataDirs, conf);
-
}
-
-
public static DataNode makeInstance(String[] dataDirs, Configuration conf) {
-
return new DataNode(conf, dirs);
-
}
-
-
DataNode(Configuration conf, AbstractList<File> dataDirs)
-
throws IOException {
-
startDataNode(conf, dataDirs);
-
}
-
vid join(){
-
dataNodeThread.join();
-
}
-
-
void startDataNode(Configuration conf, AbstractList<File> dataDirs)
-
throws IOException {
-
}
-
-
public static void main(String args[]) {
-
DataNode datanode = createDataNode(args, null);
-
datanode.join();
-
}
-
}
从上面的程序可以看到,程序从main函数中运行,然后一层创建了DataNode对象,然后在runDatanodeDaemon函数创建了一个线程,然后程序调用了线程就停留在哪里了。
换个写法,可能看起来更清晰点了:
-
public static void main(String args[]) {
-
DataNode datanode = new DataNode();
-
Thread dataNodeThread = new Thread(dn, dnThreadName);
-
dataNodeThread.setDaemon(true);
-
dataNodeThread.start();
-
dataNodeThread.join();
-
}
这样代码清晰了,换句话说,整个DataNode目前看来,就是一个大线程在跑。
整个来讲,线程,还是比较容易理解,整个类继承于Runnable接口,然后实现Run函数。 能过Thread类创建时传入对象实现,在Start后,线程就会运行,执行Run函数内的内容。
但这代码中有几行有点疑惑:
-
SetDaemon(true):这个函数的意义,并非是简单的字面意义了(Daemon:守护),一般情况下,当一个线程还没运行完,主程序是卡住的,但设置为daemon为true时,当主程序结束,则线程也就自动结束。
-
Join():这个函数就是等待线程结束后再往下走。就相当于我们常在主序中写的while(true){}一样。
创建线程,除了这种继承Runnable接口之外,还可以直接继承Thread类,然后直接调用对象的Start函数。但这样的话不太方便了,因为一个类只能继承一个类,但可继承多个接口,所以用接口更加灵活。
针对线程的这两个特性,我们可以写个简单的示例来试验一下:
-
import java.io.IOException;
-
-
public class ThreadTest extends Thread {
-
-
public class FirstThread extends Thread{
-
-
@Override
-
public void run(){
-
-
}
-
}
-
-
public class SecondThread implements Runnable{
-
-
@Override
-
public void run() {
-
-
}
-
}
-
-
public void run(){
-
for(int i = 1; i <= 50; i++){
-
try{
-
Thread.sleep(100);
-
} catch (InterruptedException ex){
-
ex.printStackTrace();
-
}
-
System.out.println(i);
-
}
-
}
-
-
public static void main(String[] args) throws IOException {
-
ThreadTest test = new ThreadTest();
-
Thread thd = new Thread(test);
-
-
// create and start 2mehtod
-
//1: FirstThread thd = new FirstThread();
-
// thd.Start();
-
-
//2: SecondThread thdx = new SecondThread()
-
//Thread thd = new Thread(thd);
-
//thd.Start();
-
-
// 如果不设置daemon,那么线程将输出50后才结束
-
thd.setDaemon(true);
-
thd.start();
-
System.out.println("isDaemon = " + thd.isDaemon());
-
System.in.read(); // 接受输入程序在此停顿,一旦有输入,main线程结束,守护线程自动结束
-
//thd.join();
-
}
-
}
示例中涉及到了创建线程的方法,join函数和setDaemno函数,可分别释放注释来调试、观察。