面试代码
来源:互联网 发布:香港网络电视台 编辑:程序博客网 时间:2024/06/04 08:44
一,两个线程交替打印
public class TwoThreadPrint { public static class Printer { private Boolean flag = false; public synchronized void printA() { try { while (!flag) { wait(); } System.out.println(Thread.currentThread().getName() + " ---- A"); flag = !flag; notify(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void printB() { try { while (flag) { wait(); } System.out.println(Thread.currentThread().getName() + " ---- B"); flag = !flag; notify(); } catch (InterruptedException e) { e.printStackTrace(); } } } public static class ThreadA extends Thread { private Printer printer; public ThreadA(Printer printer) { this.printer = printer; } @Override public void run() { for (int i = 0; i < 10; i++) { this.printer.printA(); } } } public static class ThreadB extends Thread { private Printer printer; public ThreadB(Printer printer) { this.printer = printer; } @Override public void run() { for (int i = 0; i < 10; i++) { this.printer.printB(); } } } public static void main(String[] args) throws Exception { Printer printer = new Printer(); Thread threadA = new ThreadA(printer); Thread threadB = new ThreadB(printer); threadA.start(); threadB.start(); } }
二, 给定一个能够生成均匀1~5随机枚举数的函数,请设计一个能够生成均匀1~7随机枚举数的函数。参考这里
public class Rand57 { private static Random random = new Random(); public static int rand5() { return random.nextInt(5); } private static int[][] initArray = new int[5][5]; public static int rand7() { int col = Rand57.rand5(); int row = Rand57.rand5(); if (col == 4 && row > 0) { return Rand57.rand7(); } else { return Rand57.initArray[col][row] % 7; } } static { int val = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { Rand57.initArray[i][j] = (val % 7); val++; } } } public static void main(String[] args) throws Exception { System.out.println(Rand57.rand7()); Map<String, Integer> resultMap = new HashMap<String, Integer>(); for (int i = 0; i < 10000000; i++) { String val = String.valueOf(Rand57.rand7()); if (null == resultMap.get(val)) { resultMap.put(val, 1); } else { resultMap.put(val, resultMap.get(val).intValue() + 1); } } for (String key : resultMap.keySet()) { System.out.println(key + "----" + resultMap.get(key)); } } }
三,对指定的N个数字排序
public class SortRandomNumber { public static void main(String[] args) throws Exception { File file = new File("random_number.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); long start = System.currentTimeMillis(); byte[] location = new byte[10000000]; for (int i = 0; i < location.length; i++) { location[i] = 0; } File outFile = new File("sort_number.txt"); outFile.createNewFile(); BufferedWriter bw = new BufferedWriter(new FileWriter(outFile)); String line = br.readLine(); while (StringUtils.isNotBlank(line)) { location[Integer.valueOf(line)] = 1; line = br.readLine(); } boolean first = true; for (int i = 0; i < location.length; i++) { if (location[i] == 1) { if (!first) { bw.newLine(); } bw.write(String.valueOf(i)); first = false; } } long end = System.currentTimeMillis(); System.out.println("total time: " + (end -start)); br.close(); bw.flush(); bw.close(); }}
0 0
- 面试代码
- 面试代码
- 面试代码
- 面试->代码
- 面试集锦代码部分
- Java 面试代码题型
- 【leetcode】面试leetcode代码
- js面试代码题
- 面试手写代码题目
- 前端面试(代码)
- php面试(代码)
- 常见面试代码总结
- 面试代码题记录
- 面试宝典之代码编写
- 【面试】一些排序算法代码
- 快速排序 模板代码 面试
- 程序员面试,写代码要点
- 面试必会代码总结
- 找工作
- 回射客户端服务器select 修改版[1]( echo client && server improved by select function)
- js自执行函数的几种不同写法的比较
- 你应该知道的35个Jquey小技巧
- 51nod 1433 0和5
- 面试代码
- OBB文件和StorageManager类及.nomedia
- Lucene / Solr 开发经验
- xml文件配置报错问题
- 一个优秀的Java程序员必须了解的GC机制
- leetcode-Path Sum
- Android之NDK开发
- Java容器学习笔记(1)
- 泛型List<T>方法属性