zoukankan      html  css  js  c++  java
  • 20155224 2016-2017-2 《Java程序设计》第6周学习总结

    20155224 2016-2017-2 《Java程序设计》第6周学习总结

    教材学习内容总结

    • Thread线程:

      • 定义某线程后,要有 xxx.stard();
      • Thread.sleep()会让线程进入Blocked状态,此时若调用线程的interru方法,会抛出interruptedException异常对象。
    • synchronized:

      • 在方法前加上此关键字,可以确保方法内的流程完整执行。
    • 资源在多线程下的交叉调用可能造成死锁。

    • Lock:

      • 主要操作类为ReentrantLock
      • 方法:

    lock()与unlock()

    trylock()

    • ReadWriteLock:
      • readLock()与WriteLock()可返回Lock操作对象。

    代码调试中的问题和解决过程

    • 问题1:343页代码
      • 出现错误位置程序中已标出
    package cc.openhome;
    
    class Resource{
        private String name;
        private int res;
        Resource(String name,int res){
            this.name=name;
            this.res=res;
        }
        String getName(){
            return name;
        }
        synchronized int doSome(){
            return ++res;
        }
        synchronized void cooperate(Resource resou){
            resou.doSome();//Cannot resolve methed 'doSome()'
            System.out.printf("%s 整合 %s 的资源%n",this.name,resou.getName());//Cannot resolve methed 'getName()'
        }
    }
    public class C11P343 {
        public static void main(String[] args) {
            Resource res1=new Resource("R1",10);
            Resource res2=new Resource("R2",20);
    
            Thread thr1=new Thread(()->{
                for(int i=0;i<10;i++){
                    res1.cooperate(res2);
                }
            }
            );
            Thread thr2=new Thread(()->{
                for(int i=0;i<10;i++){
                    res2.cooperate(res1);
                }
            }
            );
    
            thr1.start();
            thr2.start();
        }
    }
    
    • 问题1解决方案:XXXXXX

    • 问题2:355页代码

      • 虽然程序已经声明import java.util.concurrent.locks.*;但显示 unused import statement 所有用到locks相关的行全部出错
    package cc.openhome;
    import java.util.concurrent.locks.*;
    
    class Resource{
        private ReetrantLock lock=new ReetrantLock();
        private String name;
        Resource(String name){
            this.name=name;
        }
        void cooperate(Resource res){
            while(true){
                try{
                    if(lockMeAnd(res)){
                        System.out.printf("%s 整合 %s 的资源%n",this.name,res.name);
                        break;
                    }
                }finally {
                    unLockMeAnd(res);
                }
            }
        }
        private boolean lockMeAnd(Resource res){
            return this.lock.tryLock() && res.lock.tryLock();
        }
        private void unLockMeAnd(Resource res){
            if(this.lock.isHeldByCurrentThread()){
                this.lock,unlock();
            }
            if(res.lock.isHeldByCurrentThread()){
                res.lock.unlock();
            }
        }
    }
    public class C11P355 {
        public static void main(String[] args) {
            Resource res1=new Resource("resource1");
            Resource res2=new Resource("resource2");
            Thread thr1=new Thread(()->{
                for(int i=0;i<10;i++){
                    res1.cooperate(res2);
                }
            }
            );
            Thread thr2=new Thread(()->{
                for(int i=0;i<10;i++){
                    res2.cooperate(res1);
                }
            }
            );
            thr1.start();
            thr2.start();
        }
    }
    
    • 问题2解决方案:XXXXXX

    • 问题3:361页代码

    package cc.openhome;
    
    import java.io.InterruptedIOException;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class C11P361 {
        private int pro=-1;
        private Lock lock=new ReentrantLock();
        private Condition cond=lock.newCondition();
    
        public void SetProduct(int product)throws InterruptedIOException{
            lock.lock();
            try{
                waitIfFull();
                this.pro=product;
                System.out.printf("生产者设定(%d)%n",this.pro);
                condition.signal();
            }finally {
                lock.unlock();
            }
        }
        private void waitIfFull()throws InterruptedIOException{
            while(this.pro!=-1){
                condition.await();
            }
        }
        public int getPro()throws InterruptedIOException{
            lock.lock();
            try{
                waitIfEmpty();
                int p=this.pro;
                this.pro=-1;
                System.out.printf("消费者取走(%d)%n",p);
                condition.signal();
                return p;
            }finally {
                lock.unlock();
            }
        }
        private void waitIfEmpty()throws InterruptedIOException{
            while(this.pro==-1){
                condition.await();
            }
        }
    }
    
    • 问题3解决方案:
      • 经过检查我发现,我在先前变量命名为cond(condition)的简写,下面的程序忘记改正,于是出错。
      • 改正变量名称后,一些方法出现如下错误:

      unhandled exception:java.lang.InterruptedException

      • 发现是声明的时候throws了InterruptedIOException错误,把InterruptedIOException改为 InterruptedException 就可以了

    代码托管

    • 代码提交过程截图:

      • 运行 git log --pretty=format:"%h - %an, %cd : %s" 并截图
    • 代码量截图:

      • 运行 find src -name "*.java" | xargs cat | grep -v ^$ | wc -l 并截图

    上周考试错题总结

    • 18.现有:
     1. class Flow {
    
     2.   public static void main(String [] args) t
    
     3.    try {
    
     4 .              System. out .print ("before") ;
    
     5 .                  doRiskyThing ( )  ;
    
     6.          System.out.print ("after ") ;
    
     7.    } catch (Exception fe) {
    
     8.          System.out.print ("catch") ;
    
     9.     }
    
     10 .          System. out .println ( " done") ;
    
     11.   }
    
     12.  public static void doRiskyThing() throws Exception{
    
     13.   
    
    
              }}
    
    

    可能会产生哪两项结果 ?(选两项)

    A .
    before catch

    B .
    before after done

    C .
    before catch done

    D .
    before after catch

    • 答案:B、C

      • 解析:doRiskyThing中有throw C,否则B
    • 15.现有:

        1.  class Propeller2  {
    
        2.   pulolic static void main (String[]args)//add code here?
    
        3.    {  new Propeller2().topGo();  }
    
        4.
    
        5.void topGo()  //add code here?
    
        6.    {   middleGo();  }
    
        7.
    
        8.void middleGo()  //add code here?
    
        9.    {   go();  System.out.println ("late middle");  }
    
        10.
    
        11. void go()  //add code here?
    
        12.    {throw new Exception();  }
    
        13.  }
    

    为使代码通过编译,需要在哪一行加入声明throws Exception?

    A .
    只在第11行

    B .
    在第8行和第11行

    C .
    在第5行、第8行和第11行

    D .
    在第2行、第5行、第8行和第11行

    • 答案:D

      • 解析:内容中有throw的方法一定要throws错误类型
      1. 现有:
       1.   class  Parser extends  Utilis  {
    
       2.     public static void main (String  []  args)  {
    
       3 .          try  {  System. out.print (new  Parser ( ) .getlnt ("42")) ;
    
       4.     }  catch (NumberFormatException n) {
    
       5 .              System.out .println ( "NFExc" ) ;   }
    
       6.    }
    
       7.     int getlnt (String arg)  throws NumberFormatException{
    
       8.       return Integer.parselnt (arg) ;
    
       9.    }
    
       10. }
    
       11. class Utils {
    
       12.    int getlnt (String arg)  {  return 42;  }
    
       13. }
    

    结果为 :

    A .
    NFExc

    B .
    42

    C .
    42NFExc

    D .
    编译失败

    • 答案:B
      • 解析:未出现catch()中的错误

    结对及互评

    评分标准

    1. 正确使用Markdown语法(加1分):

      • 不使用Markdown不加分
      • 有语法错误的不加分(链接打不开,表格不对,列表不正确...)
      • 排版混乱的不加分
    2. 模板中的要素齐全(加1分)

      • 缺少“教材学习中的问题和解决过程”的不加分
      • 缺少“代码调试中的问题和解决过程”的不加分
      • 代码托管不能打开的不加分
      • 缺少“结对及互评”的不能打开的不加分
      • 缺少“上周考试错题总结”的不能加分
      • 缺少“进度条”的不能加分
      • 缺少“参考资料”的不能加分
    3. 教材学习中的问题和解决过程, 一个问题加1分

    4. 代码调试中的问题和解决过程, 一个问题加1分

    5. 本周有效代码超过300分行的(加2分)

      • 一周提交次数少于20次的不加分
    6. 其他加分:

      • 周五前发博客的加1分
      • 感想,体会不假大空的加1分
      • 排版精美的加一分
      • 进度条中记录学习时间与改进情况的加1分
      • 有动手写新代码的加1分
      • 课后选择题有验证的加1分
      • 代码Commit Message规范的加1分
      • 错题学习深入的加1分
      • 点评认真,能指出博客和代码中的问题的加1分
      • 结对学习情况真实可信的加1分
    7. 扣分:

      • 有抄袭的扣至0分
      • 代码作弊的扣至0分
      • 迟交作业的扣至0分

    点评模板:

    • 博客中值得学习的或问题:

      • xxx
      • xxx
      • ...
    • 代码中值得学习的或问题:

      • xxx
      • xxx
      • ...
    • 基于评分标准,我给本博客打分:XX分。得分情况如下:xxx

    • 参考示例

    点评过的同学博客和代码

    • 本周结对学习情况

      • 20155219付颖卓

      • 结对照片

      • 结对学习内容

        • 上周错题的理解与掌握。
        • 第十章与第十一章教材学习中遇到的问题进行讨论。
        • 第十章与第十一章代码调试中遇到的问题进行讨论。
    • 上周博客互评情况

    学习进度条

    代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
    第一周 20/20 1/1 4/4
    第二周 73/93 1/2 4/8
    第三周 231/324 1/3 6/14
    第四周 842/1166 1/4 15/29
    第五周 1134/2300 1/5 21/50
    第六周 1088/3388 1/6 23/73

    尝试一下记录「计划学习时间」和「实际学习时间」,到期末看看能不能改进自己的计划能力。这个工作学习中很重要,也很有用。
    耗时估计的公式
    :Y=X+X/N ,Y=X-X/N,训练次数多了,X、Y就接近了。

    参考:软件工程软件的估计为什么这么难软件工程 估计方法

    • 计划学习时间:XX小时

    • 实际学习时间:XX小时

    • 改进情况:

    (有空多看看现代软件工程 课件
    软件工程师能力自我评价表
    )

    参考资料

  • 相关阅读:
    qt的.pro配置总结
    【GOJ 1489】Monster Hunter
    CPU 杂谈
    【CF 1061C|GOJ 3505】Multiplicity
    【CF 1039D|GOJ 3502】You Are Given a Tree
    我跳过的坑
    【CF 1101D|GOJ 3501】GCD Counting
    【HDU 5269|GOJ 739】xor的最低位
    beta阶段组间的140字互评
    【第七周】【新蜂站会】3
  • 原文地址:https://www.cnblogs.com/nxy970408/p/6660108.html
Copyright © 2011-2022 走看看