• 迅雷笔试题 (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());
    
        }
    }
  • 相关阅读:
    我喜欢的电影
    QObject
    python-类
    pycharm活动模板
    pyqt5模块介绍
    第九章第四节 流体压强与流速的关系
    开源的推荐系统
    VNote: 一个舒适的Markdown笔记软件
    jira项目管理平台搭建
    Win10环境下,告别MarkdownPad,用Notepad++搭建编写md文档的环境
  • 原文地址:https://www.cnblogs.com/pingh/p/3573678.html
Copyright © 2020-2023  润新知