zoukankan      html  css  js  c++  java
  • 迅雷笔试题 (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());
    
        }
    }
  • 相关阅读:
    洛谷1894 [USACO4.2]完美的牛栏The Perfect Stall
    洛谷2417 课程
    洛谷2860 [USACO06JAN]冗余路径Redundant Paths
    洛谷1983 车站分级
    BZOJ1178或洛谷3626 [APIO2009]会议中心
    BZOJ1179或洛谷3672 [APIO2009]抢掠计划
    CF Round #516 (Div. 2, by Moscow Team Olympiad)
    洛谷1262 间谍网络
    NOI导刊 2018河南郑州游记
    BZOJ1001或洛谷4001 [BJOI2006]狼抓兔子
  • 原文地址:https://www.cnblogs.com/pingh/p/3573678.html
Copyright © 2011-2022 走看看