• 迅雷笔试题 (JAVA多线程)启动三个线程,分别打印A B C,现在写一个程序 循环打印ABCABCABC


    题目:http://wenku.baidu.com/view/d66187aad1f34693daef3e8a.html

    启动三个线程,分别打印A B C,现在写一个程序 循环打印ABCABCABC....

     本文分别使用wait、nofity和Semaphore来实现:

    wait、nofity版本

    public class TestThread {
     
        public static void main(String[] args) {
            new Thread(new OrderThread(0,'A')).start();
            new Thread(new OrderThread(1,'B')).start();
            new Thread(new OrderThread(2,'C')).start();
        }
    }
     
    class OrderThread implements Runnable {
        //定义一个类静态变量的锁对象
        private static Object o = new Object();
        //类静态变量、用来记录是哪个线程进入运行
        private static int count = 0;
        //每个线程的标识,名称
        private char ID;
        //用来控制是线程运行的标识
        private int id;
        //每个线程运行的次数
        private int num = 0;
     
        public OrderThread(int id,char ID) {
            this.id = id;
            this.ID = ID;
        }
     
        public void run() {
            synchronized (o) {
                while (num < 10) {
                    if(count % 3 == id){
                        System.out.print(ID);
                        ++ count;
                        ++ num;
                        o.notifyAll();
                    }
                    else{
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    Semaphore版本

    public class ThreadSync {
        static class ConditionThread extends Thread {
            private Semaphore preCond;
            private Semaphore postCond;
    
            ConditionThread(Semaphore preCond, Semaphore postCond, String name) {
                this.preCond = preCond;
                this.postCond = postCond;
                this.setName(name);
            }
    
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        preCond.acquire();
                        System.out.print(Thread.currentThread().getName());
                        postCond.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            Semaphore semaphoreA = new Semaphore(0);
            Semaphore semaphoreB = new Semaphore(0);
            Semaphore semaphoreC = new Semaphore(1);
    
            Thread threadA = new ConditionThread(semaphoreC, semaphoreA, "A");
            Thread threadB = new ConditionThread(semaphoreA, semaphoreB, "B");
            Thread threadC = new ConditionThread(semaphoreB, semaphoreC, "C");
    
            threadA.start();
            threadB.start();
            threadC.start();
    
            // threadA.join();
            // threadB.join();
            // threadC.join();
        }
    }

     2,假如有字符串“6sabcsssfsfs33” ,用最有快速的方法去掉字符“ab3”,不能用java内置字符串方法(indeOf,substring,replaceAll等)

    package com.kingdee.al;
    
    public class Test {
        public static void main(String[] args) {
    
            String str = "6sabcsssfsfs33";
            char[] array = { 'a', 'b', '3' };
            char[] newArray = str.toCharArray();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < newArray.length; i++) {
                boolean isMatch = false;
                for (char key : array){
                    if (newArray[i] == key)
                        isMatch = true;
                }
                if(!isMatch){
                    sb.append(newArray[i]);
                }
            }
            System.out.println(sb.toString());
    
        }
    }
  • 相关阅读:
    Leetcode 694. 不同岛屿的数量 中等 回溯 岛屿问题
    集成电路设计流程
    Leetcode 44. 通配符匹配 困难 动态规划 精选 TOP 面试题
    Leetcode 13. 罗马数字转整数 简单 字符串
    Leetcode 36. 有效的数独 中等 数组遍历 精选 TOP 面试题
    windows 不是真正的多用户OS,linux才是
    java AWT弹球游戏
    java AWT 图片查看器
    java AWT 简易绘图
    java Swing 进度条
  • 原文地址:https://www.cnblogs.com/pingh/p/3573678.html
Copyright © 2020-2023  润新知