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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

看看题才,值不值做
回复 使用道具 举报
第一次,试试吧。
回复 使用道具 举报
来 看看吗 ,来把
回复 使用道具 举报

题目一:try{}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
思路:定义一个求乘法运算的功能用来演示上面的问题。
class Demo
{
        public static void main(String[] args)
        {
                System.out.println(show(3,4));
        }

        public static int show(int a,int b)
        {
                try
                {
                          return a*b;
                }   
                finally
                {
                         System.out.println("finally");
                }  
        }
}

输出的结果为:
finally
12
所以,在try{}语句中有一个return语句,紧跟在这个try后的finally {}里的code会被执行,
并且在return前执行。

题目二:将某一盘符下只要是文件夹里有.java结尾的文件就输出它的绝对路径,注意是多级文件夹哦。
思路:因为多级文件夹,所以用递归比较简便。
1,对指定的目录进行递归。
2,获取递归过程中所有的java文件的路径并打印。
import java.io.*;
class JavaFileList
{
        public static void main(String[] args)
        {
                File dir = new File("d:\\java0217");
                getDir(dir);
        }

        public static void getDir(File dir)
       {
               File[] files = dir.listFiles();
               for(File file : files)
              {
                       if(file.isDirectory())
                                 getDir(file);
                      else
                      {
                                if(file.getName().endsWith(".java"))
                                System.out.println(file.getAbsolutePath());
                     }
              }
        }
}

题目三:编写程序分别使用Synchronized和Lock实现一个买票程序的线程安全问题,两套代码分开写哦!
步骤:
1.定义类实现Runnable接口。
2.覆盖Runnable接口中的run方法,将线程要运行的代码存放在该run方法中。
3.通过Thread类建立线程对象。
4.将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
5.调用Thread类的start方法开启线程并调用Runnable接口子类的run方法。

class Ticket implements Runnable
{
         private int tick = 1000;
         Object obj = new Object();
         public void run()
         {  
                 while(true)
                 {
                             synchronized(obj)
                             {
                                       if(tick>0)
                                       {
                                                 try{Thread.sleep(10);}catch(Exception e){}
                                                 System.out.println(Thread.currentThread().getName()+"...sale:"+tick--);
                                       }
                             }
                 }
        }
}

class  TicketDemo
{
            public static void main(String[] args)
            {
                      Ticket t = new Ticket();
                      Thread t1 = new Thread(t);
                      Thread t2 = new Thread(t);
                      Thread t3 = new Thread(t);
                      Thread t4 = new Thread(t);
                      t1.start();
                      t2.start();
                      t3.start();
                      t4.start();
  

         }
}

用Lock实现一个买票程序的线程安全问题
import java.util.concurrent.locks.*;
class Ticket implements Runnable
{
            private int tick = 1000;
            private Lock lock = new ReentrantLock();

            public void run()
            {
                     lock.lock();  
                     try
                     {
                             while(true)
                             {   
                                        if(tick>0)
                                        {
                                                     Thread.sleep(10);
                                                     System.out.println(Thread.currentThread().getName()+"...sale:"+tick--);
                                        }
   
                              }
                   }
                   catch (Exception e)
                   {
                              System.out.println(e.toString());
                   }
                   finally
                   {
                               lock.unlock();
                   }
        }
}

class  TicketDemo2
{
           public static void main(String[] args)
           {
                      Ticket t = new Ticket();
                      Thread t1 = new Thread(t);
                      Thread t2 = new Thread(t);
                      Thread t3 = new Thread(t);
                      Thread t4 = new Thread(t);
                      t1.start();
                      t2.start();
                      t3.start();
                       t4.start();
           }
}



回复 使用道具 举报
其实才刚刚开始....黑马视频辅导班马上就要毕业了,备战就业班考试!!
回复 使用道具 举报
领题!!
回复 使用道具 举报
领个题目瞅瞅
回复 使用道具 举报
才学完多线程,领题看看
回复 使用道具 举报
回帖领题!!!!!!!!!!!!!!!!!!
回复 使用道具 举报
请原谅我姗姗来迟,我也要分,嘻嘻:victory:
回复 使用道具 举报
               赞一个。
回复 使用道具 举报
回帖领题
回复 使用道具 举报
  过来看看 ,貌似不难的样子
回复 使用道具 举报
抓紧时间学习,大家加油!
回复 使用道具 举报
回复开题
回复 使用道具 举报
题在哪里
回复 使用道具 举报
不懂你说啥意思,但是我知道finally里的code会执行,程序会给return保留一下,等到finally里的code运行了再回来运行return,除非在finally之前关闭jvm才不会运行finally
回复 使用道具 举报
逝....曾经 发表于 2015-5-24 17:37
不懂你说啥意思,但是我知道finally里的code会执行,程序会给return保留一下,等到finally里的code运行了再 ...

手快啦,可发出去啦,不好意思呀
回复 使用道具 举报
顶版主!!
回复 使用道具 举报
adsfasasdfasdfas
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马