Hadoop源码学习笔记(3) ——初览DataNode及学习线程

Hadoop源码学习笔记(3)

——初览DataNode及学习线程

进入了main函数,我们走出了第一步,接下来看看再怎么走:

  1. public class DataNode extends Configured implements InterDatanodeProtocol,
  2.       ClientDatanodeProtocol, FSConstants, Runnable {
  3.  
  4.    public static DataNode createDataNode(String args[], Configuration conf)
  5.          throws IOException {
  6.       DataNode dn = instantiateDataNode(args, conf);
  7.       runDatanodeDaemon(dn);
  8.       return dn;
  9.    }
  10.  
  11.    public static void runDatanodeDaemon(DataNode dn) throws IOException {
  12.          dn.dataNodeThread = new Thread(dn, dnThreadName);
  13.          dn.dataNodeThread.setDaemon(true);
  14.          dn.dataNodeThread.start();
  15.       }
  16.    }
  17.  
  18.    public static DataNode instantiateDataNode(String args[], Configuration conf)
  19.          throws IOException {
  20.       return makeInstance(dataDirs, conf);
  21.    }
  22.  
  23.    public static DataNode makeInstance(String[] dataDirs, Configuration conf) {
  24.       return new DataNode(conf, dirs);
  25.    }
  26.  
  27.    DataNode(Configuration conf, AbstractList<File> dataDirs)
  28.          throws IOException {
  29.       startDataNode(conf, dataDirs);
  30.    }
  31. vid join(){
  32. dataNodeThread.join();
  33. }
  34.  
  35.    void startDataNode(Configuration conf, AbstractList<File> dataDirs)
  36.          throws IOException {
  37.    }
  38.  
  39.    public static void main(String args[]) {
  40.       DataNode datanode = createDataNode(args, null);
  41.       datanode.join();
  42.    }
  43. }

从上面的程序可以看到,程序从main函数中运行,然后一层创建了DataNode对象,然后在runDatanodeDaemon函数创建了一个线程,然后程序调用了线程就停留在哪里了。

换个写法,可能看起来更清晰点了:

  1. public static void main(String args[]) {
  2.    DataNode datanode = new DataNode();
  3.    Thread dataNodeThread = new Thread(dn, dnThreadName);
  4.    dataNodeThread.setDaemon(true);
  5.    dataNodeThread.start();
  6.    dataNodeThread.join();
  7. }

这样代码清晰了,换句话说,整个DataNode目前看来,就是一个大线程在跑。

整个来讲,线程,还是比较容易理解,整个类继承于Runnable接口,然后实现Run函数。 能过Thread类创建时传入对象实现,在Start后,线程就会运行,执行Run函数内的内容。

但这代码中有几行有点疑惑:

  1. SetDaemon(true):这个函数的意义,并非是简单的字面意义了(Daemon:守护),一般情况下,当一个线程还没运行完,主程序是卡住的,但设置为daemon为true时,当主程序结束,则线程也就自动结束。
  2. Join():这个函数就是等待线程结束后再往下走。就相当于我们常在主序中写的while(true){}一样。

 

创建线程,除了这种继承Runnable接口之外,还可以直接继承Thread类,然后直接调用对象的Start函数。但这样的话不太方便了,因为一个类只能继承一个类,但可继承多个接口,所以用接口更加灵活。

 

针对线程的这两个特性,我们可以写个简单的示例来试验一下:

  1. import java.io.IOException;
  2.  
  3. public class ThreadTest extends Thread {
  4.  
  5.    public class FirstThread extends Thread{
  6.  
  7.       @Override
  8.       public void run(){
  9.  
  10.       }
  11.    }
  12.  
  13.    public class SecondThread implements Runnable{
  14.  
  15.       @Override
  16.       public void run() {
  17.  
  18.       }
  19.    }
  20.  
  21.    public void run(){
  22.          for(int i = 1; i <= 50; i++){
  23.                try{
  24.                    Thread.sleep(100);
  25.                } catch (InterruptedException ex){
  26.                    ex.printStackTrace();
  27.                }
  28.                System.out.println(i);
  29.          }
  30.    }
  31.  
  32.    public static void main(String[] args) throws IOException {
  33.       ThreadTest test = new ThreadTest();
  34.       Thread thd = new Thread(test);
  35.  
  36.       // create and start 2mehtod
  37.       //1: FirstThread thd = new FirstThread();
  38.       // thd.Start();
  39.  
  40.       //2: SecondThread thdx = new SecondThread()
  41.       //Thread thd = new Thread(thd);
  42.       //thd.Start();
  43.  
  44.       // 如果不设置daemon,那么线程将输出50后才结束
  45.       thd.setDaemon(true);
  46.       thd.start();
  47.       System.out.println("isDaemon = " + thd.isDaemon());
  48.        System.in.read(); // 接受输入程序在此停顿,一旦有输入,main线程结束,守护线程自动结束
  49.       //thd.join();
  50.    }
  51. }

示例中涉及到了创建线程的方法,join函数和setDaemno函数,可分别释放注释来调试、观察。

原文地址:https://www.cnblogs.com/zjfstudio/p/4079888.html