面试代码

一,两个线程交替打印

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值