标签:
数码视讯宣讲会现场笔试题:
1、list长度为100,用两个线程交换取出5个数
1 package ren.laughing.test.shumashixun; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * 两个线程,交换取出5个元素,直到list为空 8 * 9 * @author Laughing_Lz 10 * @time 2016年9月29日 11 */ 12 class SynList { 13 public List list = new ArrayList(); 14 15 public synchronized void get5Counts1() { 16 if (list.size() == 0) { 17 System.out.println("list已为空,无法取出!"); 18 } else { 19 System.out.println("执行线程1"); 20 for (int i = 0; i < 5; i++) { 21 System.out.println(list.get(0)); 22 list.remove(0); 23 } 24 System.out.println("已取出5个"); 25 try { 26 wait();// wait写在前面 27 if (list.size() == 0) { 28 System.out.println("list已为空,无法取出!"); 29 } 30 } catch (InterruptedException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 notify(); 35 } 36 } 37 38 public synchronized void get5Counts2() { 39 if (list.size() == 0) { 40 System.out.println("list已为空,无法取出!"); 41 } else { 42 System.out.println("执行线程2"); 43 for (int i = 0; i < 5; i++) { 44 System.out.println(list.get(0)); 45 list.remove(0); 46 } 47 System.out.println("已取出5个"); 48 notify();// notify写在前面 49 try { 50 wait(); 51 if (list.size() == 0) { 52 System.out.println("list已为空,无法取出!"); 53 } 54 } catch (InterruptedException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 } 59 } 60 61 public void set100Counts() { 62 for (int i = 0; i < 100; i++) { 63 list.add(i); 64 } 65 } 66 } 67 68 class SynQueue1 implements Runnable { 69 private SynList synList; 70 71 public SynQueue1(SynList synList) { 72 super(); 73 this.synList = synList; 74 } 75 76 @Override 77 public void run() { 78 while (synList.list.size() >= 5) { 79 synList.get5Counts1(); 80 } 81 } 82 } 83 84 class SynQueue2 implements Runnable { 85 private SynList synList; 86 87 public SynQueue2(SynList synList) { 88 super(); 89 this.synList = synList; 90 } 91 92 @Override 93 public void run() { 94 while (synList.list.size() >= 5) { 95 synList.get5Counts2(); 96 } 97 } 98 } 99 100 public class SynQueue { 101 public static void main(String[] args) { 102 SynList synList = new SynList(); 103 synList.set100Counts(); 104 new Thread(new SynQueue1(synList)).start(); 105 new Thread(new SynQueue2(synList)).start(); 106 ; 107 } 108 }
2、给定日期,获得当月最后一天和上两个月的第一天
1 package ren.laughing.test.shumashixun; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 8 /** 9 * 关于获取某月份的第一天和最后一天 10 * 11 * @author Laughing_Lz 12 * @time 2016年9月29日 13 */ 14 public class AboutDate { 15 /** 16 * 以传入日期起,获取i个月后的第一天 17 * 18 * @param strDate 19 * @param i 20 * @return 21 * @throws ParseException 22 */ 23 public String getFirstDayOfMonth(String strDate, int i) throws ParseException { 24 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 25 Date date = sdf.parse(strDate); 26 Calendar cal = Calendar.getInstance(); 27 cal.setTime(date); 28 cal.add(Calendar.MONTH, i);// 添加i个月份 29 cal.set(Calendar.DAY_OF_MONTH, 1);// 设置cal为当前日期的第一天 30 String firstDayOfMonth = sdf.format(cal.getTime()); 31 return firstDayOfMonth; 32 } 33 34 /** 35 * 以传入日期起,获取i个月后的最后一天 36 * 37 * @param strDate 38 * @param i 39 * @return 40 * @throws ParseException 41 */ 42 public String getLastDayOfMonth(String strDate, int i) throws ParseException { 43 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 44 Date date = sdf.parse(strDate); 45 Calendar cal = Calendar.getInstance(); 46 cal.setTime(date); 47 cal.add(Calendar.MONTH, i + 1);// cal月份加1 48 cal.set(Calendar.DAY_OF_MONTH, 1);// 设置cal为当前月份的第一天 49 cal.add(Calendar.DATE, -1);// cal日期加(-1)天 50 String lastDayOfMonth = sdf.format(cal.getTime()); 51 return lastDayOfMonth; 52 } 53 54 public static void main(String[] args) throws ParseException { 55 String strDate = "2016-09-28"; 56 String result1 = new AboutDate().getFirstDayOfMonth(strDate, 0); 57 System.out.println(result1); 58 String result2 = new AboutDate().getLastDayOfMonth(strDate, -2); 59 System.out.println(result2); 60 } 61 }
3、统计某文件夹下包含的jsp,xml,java文件总行数
1 package ren.laughing.test.shumashixun; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 /** 12 * 统计某文件夹下包含的jsp,xml,java文件总行数 13 * 14 * @author Laughing_Lz 15 * @time 2016年9月29日 16 */ 17 public class AboutFile { 18 public static int count4Java = 0; 19 public static int count4Xml = 0; 20 public static int count4Jsp = 0; 21 22 /** 23 * 遍历获得某文件夹下所有文件 24 * 25 * @param str 26 * @param fileList 27 * @return 28 */ 29 public List<String> GetFiles(String str, List<String> fileList) { 30 File file = new File(str); 31 File[] files = file.listFiles();// 列出所有子文件和路径 32 // String[] files = file.list();//列出所有子文件和路径名 33 for (int i = 0; i < files.length; i++) { 34 if (files[i].isDirectory()) {// 是目录,递归 35 // str = str + "\\" + files[i].getName();//拼接子文件夹路径 36 fileList = GetFiles(files[i].getAbsolutePath(), fileList);// 传入子文件夹绝对路径名,继续遍历 37 } else { 38 fileList.add(files[i].getAbsolutePath());// 是文件,将绝对路径名放入list 39 } 40 } 41 return fileList; 42 } 43 44 /** 45 * 统计各类文件的总行数 46 * 47 * @param fileList 48 */ 49 public void getCounts(List<String> fileList) { 50 for (String file : fileList) { 51 try { 52 BufferedReader br = new BufferedReader(new FileReader(new File(file))); 53 String oneLine = br.readLine(); 54 while (oneLine != null) { 55 if (file.endsWith(".java")) { 56 count4Java++; 57 } else if (file.endsWith(".xml")) { 58 count4Xml++; 59 } else if (file.endsWith(".jsp")) { 60 count4Jsp++; 61 } 62 oneLine = br.readLine(); 63 } 64 } catch (FileNotFoundException e) { 65 e.printStackTrace(); 66 } catch (IOException e) { 67 e.printStackTrace(); 68 } 69 70 } 71 System.out.println("java文件总行数:" + count4Java + "\n" + "jsp文件总行数:" + count4Jsp + "\n" + "xml文件总行数:" + count4Xml); 72 } 73 74 public static void main(String[] args) { 75 List<String> fileList = new ArrayList<String>(); 76 String str = "G:\\test"; 77 AboutFile af = new AboutFile(); 78 af.GetFiles(str, fileList); 79 af.getCounts(fileList); 80 } 81 }
标签:
原文地址:http://www.cnblogs.com/Laughing-Lz/p/5920940.html