隨筆 - 8, 文章 - 0, 評論 - 6, 引用 - 0

          導(dǎo)航

          <2007年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          2007年5月30日

          實現(xiàn)一個棧,使其push,pop,min(取得棧中的最小元素)均為O(1)

          實現(xiàn)一個棧,使其push,pop,min(取得棧中的最小元素)均為O(1)

          我的解
          interface IntStack
          {
              
          int pop();
              
          void push(int i);
              
          int get();
          }


          class MinStack
          {
              
          //store all the element
              private IntStack elemStack = new IntStack();
              
              
          //store current and historical smallest element
              private IntStack minStack = new IntStack();
              
              
          public void push(int i)
              
          {
                  elemStack.push(i);
                  
                  
          int currentMin = minStack.get();
                  
          if(i <= currentMin) minStack.push(i);
              }

              
              
          public int pop()
              
          {
                  
          int result = elemStack.pop();
                  
          if(result == minStack.get()) minStack.pop();
                  
          return result;
              }

              
              
          public int getMinElem()
              
          {
                  
          return minStack.get();
              }

          }

          posted @ 2007-07-18 20:57 Job Hu 閱讀(906) | 評論 (0)編輯 收藏

          二叉排序樹變?yōu)殡p向鏈表

          把一個二叉排序樹(也許不叫這個)變?yōu)檫f增的雙向鏈表,不能夠生成額外的結(jié)點.
          eg 6
                 / \
                4   8
               / \ / \
              3  5 7  9

          3=4=5=6=7=8=9

          我的解:

          class Node
          {
              
          public Node left;
              
          public Node right;
              
              
          private static Node getLinkListTail(Node head)
              
          {
                  Node result 
          = head;
                  
          if(result==nullreturn null;
                  
          while(result.right!=null)
                  
          {
                      result 
          = result.right;
                  }

                  
          return result;
              }

              
              
          public static Node flatten(Node root)
              
          {
                  
          if(root==nullreturn null;
                  
                  Node result 
          = root;
                  
                  
          // A leaf node
                  if(root.left==null&&root.right==nullreturn root;
                  
                  
          //divide-and-conquer
                  Node leftSubTreeLinkListHead = flatten(root.left);
                  Node rightSubTreeLinkListHead 
          = flatten(root.right);
                  
                  
          //merge
                  Node leftSubTreeLinkListTail = getLinkListTail(leftSubTreeLinkListHead);
                  root.left 
          = leftSubTreeLinkListTail;
                  root.right 
          = rightSubTreeLinkListHead;
                  
          if(leftSubTreeLinkListHead!=null
                  
          {
                      result 
          = leftSubTreeLinkListHead;
                      leftSubTreeLinkListTail.right 
          = root;
                  }

                  
          if(rightSubTreeLinkListHead!=null) rightSubTreeLinkListHead.left = root;
                  
                  
          return result;
              }

          }


          posted @ 2007-07-18 20:37 Job Hu 閱讀(650) | 評論 (0)編輯 收藏

          Byte in Java


          public class ByteTest
          {
              
          public static void main(String[] args)
              
          {
                  
          byte b;
                  
          byte c;
                  
          //b = 255; //Cannot convert from int to byte
                  
          //b = 0xFF; //Cannot convert from int to byte
                  b = 127;
                  c 
          = 0x7F;
                  
          if(b == c) System.out.println("b == c");
                  
          if(127 == 0x7F) System.out.println("127 == 0x7F");
                  
                  b 
          = -128;
                  
          //c = 0x80; //Cannot convert from int to byte
                  c = (byte)0x80;
                  
          if(b == c) System.out.println("b == c");
                  
          if(-128 == 0x80) System.out.println("-128 == 0x80");
                  
          if(128 == 0x80) System.out.println("128 == 0x80"); 
                  
                  c 
          = (byte)0x80;
                  
          if(128 == c) System.out.println("128 == c");
                  
          if(-128 == c) System.out.println("-128 == c");
                  
          if(128 == (c&0xFF)) System.out.println("128 == (c&0xFF)");
              }

          }

          輸出:
          b == c
          127 == 0x7F
          b == c
          128 == 0x80
          -128 == c
          128 == (c&0xFF)

          posted @ 2007-07-17 23:22 Job Hu 閱讀(296) | 評論 (0)編輯 收藏

          Java Language Keywords

          Here's a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.
          abstract continue for new switch
          assert*** default goto* package synchronized
          boolean do if private this
          break double implements protected throw
          byte else import public throws
          case enum**** instanceof return transient
          catch extends int short try
          char final interface static void
          class finally long strictfp** volatile
          const* float native super while

          posted @ 2007-07-09 22:38 Job Hu 閱讀(204) | 評論 (1)編輯 收藏

          Tomcat筆記(三)

          來看看ContainerBasestart方法:
           1public synchronized void start() throws LifecycleException {
           2
           3        //如果Container已經(jīng)處于start狀態(tài),直接返回
           4        if (started) {
           5            if(log.isInfoEnabled())
           6                log.info(sm.getString("containerBase.alreadyStarted", logName()));
           7            return;
           8        }

           9        
          10        // Notify our interested LifecycleListeners
          11        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
          12
          13        started = true;
          14
          15        // Start our subordinate components, if any
          16        if ((loader != null&& (loader instanceof Lifecycle))
          17            ((Lifecycle) loader).start();
          18        logger = null;
          19        getLogger();
          20        if ((logger != null&& (logger instanceof Lifecycle))
          21            ((Lifecycle) logger).start();
          22    //用來管理session
          23        if ((manager != null&& (manager instanceof Lifecycle))
          24            ((Lifecycle) manager).start();
          25    //看名字就知道是干什么的,不過研究集群的優(yōu)先級很低
          26        if ((cluster != null&& (cluster instanceof Lifecycle))
          27            ((Lifecycle) cluster).start();
          28    //用來進行訪問控制,或者權(quán)限控制的
          29        if ((realm != null&& (realm instanceof Lifecycle))
          30            ((Lifecycle) realm).start();
          31    //和JNDI相關(guān)
          32        if ((resources != null&& (resources instanceof Lifecycle))
          33            ((Lifecycle) resources).start();
          34
          35        //啟動所有子container
          36        Container children[] = findChildren();
          37        for (int i = 0; i < children.length; i++{
          38            if (children[i] instanceof Lifecycle)
          39                ((Lifecycle) children[i]).start();
          40        }

          41
          42        // 啟動Container內(nèi)部持有的pipeline對象,Container對Pipeline接口的實現(xiàn)就是通過調(diào)用這個內(nèi)部持有的Pipeline對象
          43        if (pipeline instanceof Lifecycle)
          44            ((Lifecycle) pipeline).start();
          45
          46        // Notify our interested LifecycleListeners
          47        lifecycle.fireLifecycleEvent(START_EVENT, null);
          48
          49        // 注釋說這個函數(shù)用來check session是否過期,但看的不是太懂
          50        threadStart();
          51
          52        // Notify our interested LifecycleListeners
          53        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
          54
          55}

          56
           

          所有和clusterrealm相關(guān)都放在最后來研究了,不管怎么樣先把tomcat如何處理request的整個過程串起來對現(xiàn)在的我來說是最重要的。另外還有Tomcat中的很多部件都用到了JMX API,即SNMPJava實現(xiàn)來進行性能檢測和管理,這個也會放在最后研究。

          ContainerBase就看這么多了,下面來看看StandardEngine這個類。除去和clusterrealm、JMX相關(guān)的方法后,StanderdEngine剩下的方法就很很少了。

          StandardEngine有一個Service類型的成員。Java doc中指出Service就是由很多共享同一個ContainerConnector組成。一個Service對應(yīng)于一個Container,來自這個Service的任何一個Connectorrequest都會由其對應(yīng)的Container進行處理。看到現(xiàn)在的感覺就是ConnectorContainer提供request對象,并接受Container返回的response對象。在Tomcat中有很多類別都被用來體現(xiàn)現(xiàn)request或者response,例如org.apache.catalina.connector .Request就是Coyote request的一個wrapper類,Coyote這個framework幫助封裝了底層的網(wǎng)絡(luò)復(fù)雜性,向上提供一個統(tǒng)一的接口。我想tomcat既能夠成為一個standalonehttpjsp/Servlet服務(wù)器,也能夠同apache http server集成,很可能就是依賴于Coyote提供的統(tǒng)一接口。

          在構(gòu)造函數(shù)中會將StandardEngine這個Pipeline的最后一個Valve,即Basic設(shè)置為StandardEngineValve。來看看StandardEnginValueinvoke方法
           1public final void invoke(Request request, Response response)
           2        throws IOException, ServletException {
           3
           4        // Select the Host to be used for this Request
           5        Host host = request.getHost();
           6        if (host == null{
           7            response.sendError
           8                (HttpServletResponse.SC_BAD_REQUEST,
           9                 sm.getString("standardEngine.noHost"
          10                              request.getServerName()));
          11            return;
          12        }

          13
          14        // Ask this Host to process this request
          15        host.getPipeline().getFirst().invoke(request, response);
          16
          17    }

          18
           

          可以看出在處理到StandardEngine這個Pipeline的最后一個Valve時,會根據(jù)當前request所指定的Host,將當前的requestresponse傳遞給該Host這個Pipeline的第一個Valve進行處理。

          我想Tomcat中的Engine、Host、ContextWrapper處理request生成response的過程大概是這樣的:

          Engine在收到request后在其Pipeline中的每一個Valverequest進行處理,也可能會生成response的某些部分,在最后一個Valve中將requestresponse傳給下一級ContainerHost的第一個ValveHost重復(fù)同樣過程,繼續(xù)傳遞給ContextContext再傳遞給Wrapper。由于Wrapper代表的是Servlet對象,因此在Wrapper處所有的處理都結(jié)束了,response對象生成完畢。當然了,如果在某一級中無法找到request要求的下一級對象,則整個處理過程也會立即結(jié)束。

          posted @ 2007-05-30 22:58 Job Hu 閱讀(363) | 評論 (1)編輯 收藏

          主站蜘蛛池模板: 博白县| 台中县| 铁岭县| 海宁市| 台湾省| 陆川县| 乃东县| 卢氏县| 改则县| 山西省| 怀远县| 伊吾县| 赤峰市| 开化县| 姜堰市| 恩平市| 金堂县| 如东县| 航空| 永清县| 望江县| 牡丹江市| 甘肃省| 宁蒗| 永顺县| 汤阴县| 曲阳县| 云梦县| 额济纳旗| 府谷县| 瑞金市| 玉溪市| 庐江县| 榆中县| 娱乐| 河南省| 常德市| 鄯善县| 台南市| 大埔区| 沿河|