今天面试公司没有回答好的问题总结

1、两个线程,一个输出hello,另一个线程输出world ,需要输入的是  线程1 :hello  线程2:world  线程1:hello 线程2:world 这样线程交替输出

当时没搞定,因为我以为线程组有相应的方法的,但不知道哪个方法,根本没往别地方想,思维局限了,回来看看api没有,才想别个办法写的,套路一比较原始

public class ThreadHelloWorld {

    public static void main(String[] args) {
        class State {
            private boolean sayHello = true;
            private int count = 0;

            public boolean isSayHello() {
                return sayHello;
            }

            public void setSayHello(boolean sayHello) {
                this.sayHello = sayHello;
            }

            public int getCount() {
                return count;
            }

            public void setCount(int count) {
                this.count = count;
            }

        }

        State state = new State();
        Thread tdHello = new Thread(new Runnable() {
            public void run() {
                while (state.getCount() < 1001) {
                    synchronized (state) {
                        if (state.isSayHello()) {
                            System.out.print("hello ");
                            state.setSayHello(false);
                            state.setCount(state.getCount() + 1);
                        }
                    }
                }
            }
        });

        Thread tdWorld = new Thread(new Runnable() {
            public void run() {
                while (state.getCount() < 1001) {
                    synchronized (state) {
                        if (!state.isSayHello()) {
                            System.out.println("world " + state.getCount());
                            state.setSayHello(true);
                            state.setCount(state.getCount() + 1);
                        }
                    }
                }
            }
        });

        tdHello.start();
        tdWorld.start();
    }

}

写完想跟一个朋友pk下,被干败了,他用了最新的语法。。而且还是cas的锁,速度和代码精简程度上更胜一筹

public static AtomicInteger count = new AtomicInteger(1);

public static void main(String[] args) throws Exception {
    new Thread(() -> {
        while (true) {
            if (count.intValue() % 2 == 1) {
                System.out.print("hello");
                count.addAndGet(1);
            }
        }
    }).start();

    new Thread(() -> {
        while (true) {
            if (count.intValue() % 2 == 0) {
                System.out.println(" word");
                count.addAndGet(1);
            }
        }
    }).start();
}

2、Mybatis 配置文件中 resultMap 与resultType 的区别

先欠个债。。。回头认真遇到了再认真写

3、一个线程,调用http请求

可以使用HttpClient、HttpURLConnection、OKHttp,发送http请求

4、关于设计模式、代理模式在什么地方见过使用代理模式的

 该问题指的是非spring的AOP、rpc等非常用的,目前在JDK中还想不起来,也欠个债,回头遇到了补充进来

原文地址:https://www.cnblogs.com/tiaowen/p/9069698.html