A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lwj123   /  2015-5-23 09:29  /  15409 人查看  /  318 人回复  /   3 人收藏 转载请遵从CC协议 禁止商业使用本文

题目不能停!
回复 使用道具 举报
回帖领题     
回复 使用道具 举报
我来领题来了
回复 使用道具 举报
先来看看
回复 使用道具 举报
finally{}内的code会被执行,应该是在return之前吧!
public static void main()
{
        test();
}
public static boolean test()
{
     try{
            return false;
         }
     finally
       {
           system.out.println("true");
       }
}
输出结果为:
true
false
我的键盘出毛病了,大小写都切换不出来了。简单的写一个小程序,希望能拿个技术分
回复 使用道具 举报
前来领题
回复 使用道具 举报
废话不多说,先看看题
回复 使用道具 举报
领个题看看
回复 使用道具 举报
抱着试试看的态度
回复 使用道具 举报
过来领题目
回复 使用道具 举报
回帖领题
回复 使用道具 举报
领题啦   
回复 使用道具 举报
领走  领走
回复 使用道具 举报
为什么我点了连接也看不到题目呢?
回复 使用道具 举报
看到了就来玩玩
回复 使用道具 举报
对于新人,有没有简单一点的题呢?
回复 使用道具 举报
回复领题
回复 使用道具 举报
回复领题。。
回复 使用道具 举报
本帖最后由 半世心修 于 2015-5-23 16:26 编辑

题目一:try{}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?使用程序验证。在异常机制中,捕获错误的时候finally是一定会被执行的,尽管try中有一个return。如果jvm运行到了try后意外退出了那就可能出现finally不执行的情况。
        public static void calculate(int x){
                System.out.println(5/x);
        }
        public static void main(String args[]){
                try {
                        calculate(2);
                        return;
                } catch (ArithmeticException e) {
                        System.out.println("false");
                }finally{
                        System.out.println("finally is go on");
                }
        }
//输出2 finally is go on
题目二:将某一盘符下只要是文件夹里有.java结尾的文件就输出它的绝对路径,注意是多级文件夹哦。
首先得有个文件夹嘛,然后通过File类的listFiles方法获取文件夹下所有的文件,最后判断这些文件是否以.java为后缀,如果是,就输出路径。
public static void realPath(String path){
                File f = new File(path);
                for(File name:f.listFiles()){
                        System.out.println(name);
                        if(name.toString().endsWith(".java")){
                                System.out.println(name.toString());
                        }
                }
        }
        public static void main(String args[]){
                realPath("C:/Users/Administrator/Desktop/test");
        }
输出:C:\Users\Administrator\Desktop\test\Test.java
题目三:编写程序分别使用Synchronized和Lock实现一个买票程序的线程安全问题,两套代码分开写哦!

额,第一个,实现Runnable接口并重写run方法,这个不难,LOCK给忘了啊,去了解下先。。
public class Test implements Runnable{
        public static int x = 100;
        public void run() {
                while (x> 0)
                        this.shop(Thread.currentThread().getName());
        }
        public static synchronized void shop(String name){
                if(x>0){
                        System.out.println(name+"-----"+x);
                        x--;
                }
        }
        public static void main(String args[]){
                Thread t1 = new Thread(new Test(),"1");
                Thread t2 = new Thread(new Test(),"2");
                t1.start();
                t2.start();
        }
LOCK
这个看过一个例子就知道怎么写了,一样是线程同步的,之前忘了,看来得回去看看多线程了,代码来了:
public class Test implements Runnable{
    public static int x = 100;
    public void run() {
            while (x> 0)
                                try {
                                        this.shop(Thread.currentThread().getName());
                                } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
    }
    public static void shop(String name){
            Lock l = new ReentrantLock();
            if(x>0){
                    try {
                            l.lock();
                             System.out.println(name+"-----"+x);
                     x--;
                                } catch (Exception e) {
                                        // TODO: handle exception
                                }finally{
                    l.unlock();
                                }
                    }
    }
    public static void main(String args[]){
            Thread t1 = new Thread(new Test(),"1");
            Thread t2 = new Thread(new Test(),"2");
            t1.start();
            t2.start();
    }


回复 使用道具 举报
看看,领题
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马