Hadoop Source Learning Notes (3)
--first glance datanode and learning threads
into the main function, we went out the first step, and then we look at how to go:
- 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 ();
- }
- }
As you can see from the program above, the program runs from the main function and creates a Datanode object on the first layer, then creates a thread in the Rundatanodedaemon function, and then the program calls the thread to stay where it is.
In other words, it may seem clearer:
- Public Static void Main (String args[]) {
- DataNode DataNode = new DataNode ();
- Thread datanodethread = new thread (DN, dnthreadname);
- Datanodethread.setdaemon (true);
- Datanodethread.start ();
- Datanodethread.join ();
- }
So the code is clear, in other words, the whole datanode now looks like a big thread running.
For the whole thread, it is easier to understand that the entire class inherits from the Runnable interface and then implements the run function. Can pass through the object implementation when the thread class is created, after start, the thread runs and executes the contents of the run function.
But there are a few lines of confusion in this code:
- Setdaemon (true): The meaning of this function is not simply literal (Daemon: Guardian), generally, when a thread has not finished running, the main program is stuck, but when set to Daemon is true, when the main program ends, the thread ends automatically.
- Join (): This function is to wait for the thread to end and then go down. is equivalent to the same while (true) {} that we often write in the main sequence.
To create a thread, in addition to this inherited runnable interface, you can directly inherit the thread class and then call the object's start function directly. However, this is not convenient, because a class can inherit only one class, but can inherit multiple interfaces, so the interface is more flexible.
For these two features of the thread, we can write a simple example to experiment with:
- 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 <=; 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 ();
- //If daemon is not set, the thread will output 50 before it ends
- Thd.setdaemon (true);
- Thd.start ();
- System.out.println ("Isdaemon = " + Thd.isdaemon ());
- System.in.read (); //Accept Input program at this pause, once there is input, main thread ends, daemon thread ends automatically
- //thd.join ();
- }
- }
The example involves creating a threading method, a join function, and a setdaemno function that can be released separately to debug and observe.
Hadoop Source Learning Notes (3)--first glance datanode and learning threads