码迷,mamicode.com
首页 > 其他好文 > 详细

我的有水平的代码

时间:2016-04-11 18:17:23      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

我们常用的Iterator方法实践
private class ListItr extends Itr implements ListIterator<E> {
    ListItr(int index) {
        cursor = index;
    }

    public boolean hasPrevious() {
        return cursor != 0;
    }

        public E previous() {
            checkForComodification();
            try {
                int i = cursor - 1;
                E previous = get(i);
                lastRet = cursor = i;
                return previous;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

    public int nextIndex() {
        return cursor;
    }

    public int previousIndex() {
        return cursor-1;
    }

    public void set(E e) {
        if (lastRet == -1)
        throw new IllegalStateException();
            checkForComodification();

        try {
        AbstractList.this.set(lastRet, e);
        expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
        }
    }

    public void add(E e) {
            checkForComodification();

        try {
        AbstractList.this.add(cursor++, e);
        lastRet = -1;
        expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
        }
    }
    }

 

HashMap中的resize方法和putAll方法
 void resize(int newCapacity) {
        Entry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }

        Entry[] newTable = new Entry[newCapacity];
        transfer(newTable);
        table = newTable;
        threshold = (int)(newCapacity * loadFactor);
    }
  public void putAll(Map<? extends K, ? extends V> m) {
        int numKeysToBeAdded = m.size();
        if (numKeysToBeAdded == 0)
            return;
        if (numKeysToBeAdded > threshold) {
            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
            if (targetCapacity > MAXIMUM_CAPACITY)
                targetCapacity = MAXIMUM_CAPACITY;
            int newCapacity = table.length;
            while (newCapacity < targetCapacity)
                newCapacity <<= 1;
            if (newCapacity > table.length)
                resize(newCapacity);
        }

        for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<? extends K, ? extends V> e = i.next();
            put(e.getKey(), e.getValue());
        }
    }

我自己写的 服务器监控中的应用日志,继承了Runnable接口

 public void run() {
        String curStopedReasonMsg;
        try {
            if (this.applicationModel.getOnPreRunCallBack() != null) {
                this.applicationModel.getOnPreRunCallBack().callBack(this);
            }
            int firstFlag = this.applicationModel.getFirstFlag();
            String command = CmdUtil.getCommand(this.applicationModel);
            log.info("exe command====>:" + command);
            File filePath = StringUtils.isNotBlank(this.applicationModel.getProgramPath())
                    ? new File(this.applicationModel.getProgramPath() + File.separator) : null;
            this.process = Runtime.getRuntime().exec(command, null, filePath);
            if (this.applicationModel.getOnRunCallBack() != null) {
                this.applicationModel.getOnRunCallBack().callBack(this);
            }
            InputStream stdin = this.process.getInputStream();
            InputStream stderr = this.process.getErrorStream();
            Thread tIn = new Thread(new AppConsole(stdin, this.applicationModel));
            Thread tErr = new Thread(new AppConsole(stderr, this.applicationModel));
            tIn.start();
            tErr.start();
            if (this.applicationModel.getOnRunMoniCallBack() != null) {
                this.applicationModel.getOnRunMoniCallBack().callBack(this);
            }
            int result = this.process.waitFor();
            tIn.join();
            tErr.join();
            if (result == 0) {
                log.info("服务器[" + this.applicationModel.getAppName() + "]停止成功! ");
            } else {
                log.info("服务器[" + this.applicationModel.getAppName() + "]停止失败! ");
            }
            String stopTime = DateUtil.getNow();
            if (AppStatusEnum.RUNNING.equals(this.applicationModel.getAppStatusEnum())) {
                this.stopedMsg = (AppConstants.STOPED_EXCEPTION_MSG + "[" + stopTime + "]");
            } else {
                this.stopedMsg = (this.applicationModel.getStopedReasonMsg() + "[" + stopTime + "]");
            }
        } catch (Exception e) {
            log.error("", e);

            this.stopedMsg = (AppConstants.RUNNING_EXCEPTION_MSG + "[" + e.getMessage() + "]");
            try {
                if (this.applicationModel.getOnStopCallBack() != null) {
                    this.applicationModel.getOnStopCallBack().callBack(this);

                    curStopedReasonMsg = this.applicationModel.getStopedReasonMsg();
                    if ((StringUtils.isNotBlank(curStopedReasonMsg))
                            && (curStopedReasonMsg.indexOf(AppConstants.STOPED_RESTART_MSG) != -1)) {
                        Thread.sleep(3000L);
                        AppManager.getInstance().startApp(this.applicationModel);
                    }
                }
            } catch (Exception ex) {
                log.error("", ex);
                throw new RuntimeException(ex);
            }
        } finally {
            try {
                if (this.applicationModel.getOnStopCallBack() != null) {
                    this.applicationModel.getOnStopCallBack().callBack(this);

                    curStopedReasonMsg = this.applicationModel.getStopedReasonMsg();
                    if ((StringUtils.isNotBlank(curStopedReasonMsg))
                            && (curStopedReasonMsg.indexOf(AppConstants.STOPED_RESTART_MSG) != -1)) {
                        Thread.sleep(3000L);
                        AppManager.getInstance().startApp(this.applicationModel);
                    }
                }
            } catch (Exception e) {
                log.error("", e);
                throw new RuntimeException(e);
            }
        }
    }

 

我的有水平的代码

标签:

原文地址:http://www.cnblogs.com/winAlaugh/p/5379213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!