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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lovecx24 中级黑马   /  2013-11-29 23:42  /  2107 人查看  /  4 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 lovecx24 于 2013-11-30 00:05 编辑

分享两道阿里巴巴2014招聘笔试题第一题:问下面哪个方法计算速度更快?
  1. public class FFFtest {
  2.     public static void main(String[] args) {
  3.         int[] a=new int[1000];
  4.         int[] b=new int[10000000];
  5.         long start = System.currentTimeMillis();
  6.         //method 1
  7.         for(int i=0;i<1000;i++){
  8.             for(int j=0;j<10000000;j++){
  9.                 a[i]++;
  10.             }
  11.         }
  12.         long end = System.currentTimeMillis();
  13.         System.out.println(end-start);
  14.          
  15.         start=System.currentTimeMillis();
  16.         //method 2
  17.         for(int i=0 ;i<10000000;i++){
  18.             for(int j=0;j<1000;j++){
  19.                 b[i]++;
  20.             }
  21.         }
  22.         end = System.currentTimeMillis();
  23.         System.out.println(end-start);
  24.          
  25.          
  26.     }
  27. }
复制代码
第二题:求出一下每个线程的输出结果(不用关心线程顺序,只需写出输出的结果集即可)
  1. public class TestThread{
  2. public static void main(String[] args){
  3. //test1
  4. Thread t1 = new Thread();{
  5. @Override
  6. public void run(){
  7. try{
  8. int i=0;
  9. while(i++<100000000){
  10. //nothing
  11. }
  12. System.out.println("A1");
  13. }catch(IOException e){
  14. System.out.println("B1");
  15. }
  16. }
  17. };
  18. t1.start();
  19. t1.interrupt();
  20. //test2
  21. Thread t2 = new Thread(){
  22. public  void run(){
  23. try{
  24.     Thread.sleep(50000);
  25. System.out.println("A2");
  26.   }catch(IOException e){
  27.   System.out.println("B2");
  28. }
  29. }
  30. };
  31. t2.start();
  32. t2.start();
  33. //test3
  34. Thread t3 = new Thread(){
  35. public static void run(){
  36. try{
  37.     Thread.sleep(50000);
  38. System.out.println("A3");
  39.   }catch(IOException e){
  40.   System.out.println("B3");
  41. }
  42. }
  43. };
  44. t3.start();
  45. t3.interrupt();
  46. //t4
  47. Thread t4 = new Thread(){
  48. public void run(){
  49. try{
  50.     Thread.sleep(50000);
  51. System.out.println("A4");
  52.   }catch(IOException e){
  53.   System.out.println("B4");
  54. }
  55. }
  56. };
  57. t4.start();
  58. t4.start();
  59. //test5
  60. try{
  61. t4.start();
  62. System.out.println("A5");
  63. } catch(IOException e){
  64. System.out.println("B5");
  65. }
  66. }
  67. }



复制代码




评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
狼王 + 1 + 3 赞一个!谢谢分享。。。

查看全部评分

4 个回复

倒序浏览
第二题,我看的时候,你还没排版,乱的啊!!!
B5  B2   B4   B3   A1
都快绕晕了
开始程序执行,t1中断,t2 sleep(5000)中断,t3 sleep(50000)中断,t4(50000)中断,捕捉到t4.start异常,输出B5; t1循环未结束,继续异常中断,t2输出B2,t3,继续sleep 中断,t4因为输出B5时是扑捉t4.start的异常,所以sleep结束,输出B4;t1循环还未结束,继续异常中断,t3,sleep结束,输出B3,t1循环结束,没有捕捉到异常,输出A1

评分

参与人数 1技术分 +1 收起 理由
狼王 + 1 真不错,继续努力哈。

查看全部评分

回复 使用道具 举报
第一题:个人觉得,是Method1的快些;
两种方法,循环次数一致,但是Method2需要开辟10000000个空间来存储数据
Method1只需要开辟1000个空间

评分

参与人数 1黑马币 +6 收起 理由
狼王 + 6 加了一次,给你金币吧。。。。.

查看全部评分

回复 使用道具 举报
顶,阿里一直是我想进的公司,虽然以我现在的水平还不知道
回复 使用道具 举报
第一题是method2快些,我运行了两次,都是method2快些,哈哈


其实个人觉得是第二个,因为开辟空间实在记时之前就开辟空间了,所以开辟空间不会影响后面的记时,而是method1和method2两个的循环次数相同,但是他们在内循环中相加的次数却不同,method1每次都加10000000,而method2每次都加1000
哈哈,这是个人意见啊,不要打击我哦

Image.png (1.74 KB, 下载次数: 35)

Image.png
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马