Google面试题(java)—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
来源:互联网 发布:mac系统锐捷不能连 编辑:程序博客网 时间:2024/05/10 06:53
Google面试题—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
程序运行起来,4个文件的写入结果如下: A:12341234... B:23412341... C:34123412... D:41234123...
同学给我出了道面试题,自己首先想到的是用信号灯,感觉自己应该理解错了。不可能这么简单。
java1.5以前的手法写了一个,感觉不对,后来发觉应该是斜向输出。这道题大概主要是考察对多线程的流程控制。
import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class CallThread {private static class MyRunable implements Runnable {int index = 0;int max = 4 * 2;HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();{for (int i = 0; i < 4; i++) {map.put(i, new ArrayList<Integer>());}}boolean isRunning = true;public synchronized void run() {while (true) {int name = Integer.valueOf(Thread.currentThread().getName());while (index % 4 != name) {try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}if (!isRunning) {return;}}try {Thread.sleep(1000);} catch (InterruptedException e) {}for (int i = 0, arrIndex = index; i < 4; i++ ) {try {if (arrIndex-- < 0) {break;}List<Integer> list = map.get(i);if (list.size() < max) {list.add(name + 1);}} catch (Exception e) {}}System.err.println("A:" + map.get(0));System.err.println("B:" + map.get(1));System.err.println("C:" + map.get(2));System.err.println("D:" + map.get(3));System.err.println("-----------------------");if (map.get(map.size()-1).size() == max) {isRunning = false;this.notifyAll();return;} else {index++;}this.notifyAll();}}}public static void main(String[] args) throws InterruptedException {Runnable runable = new MyRunable();for (int i = 0; i < 4; i++) {Thread t = new Thread(runable);t.setName(String.valueOf(i));t.start();}}}
程序打印结果A:[1]B:[]C:[]D:[]-----------------------A:[1, 2]B:[2]C:[]D:[]-----------------------A:[1, 2, 3]B:[2, 3]C:[3]D:[]-----------------------A:[1, 2, 3, 4]B:[2, 3, 4]C:[3, 4]D:[4]-----------------------A:[1, 2, 3, 4, 1]B:[2, 3, 4, 1]C:[3, 4, 1]D:[4, 1]-----------------------A:[1, 2, 3, 4, 1, 2]B:[2, 3, 4, 1, 2]C:[3, 4, 1, 2]D:[4, 1, 2]-----------------------A:[1, 2, 3, 4, 1, 2, 3]B:[2, 3, 4, 1, 2, 3]C:[3, 4, 1, 2, 3]D:[4, 1, 2, 3]-----------------------A:[1, 2, 3, 4, 1, 2, 3, 4]B:[2, 3, 4, 1, 2, 3, 4]C:[3, 4, 1, 2, 3, 4]D:[4, 1, 2, 3, 4]-----------------------A:[1, 2, 3, 4, 1, 2, 3, 4]B:[2, 3, 4, 1, 2, 3, 4, 1]C:[3, 4, 1, 2, 3, 4, 1]D:[4, 1, 2, 3, 4, 1]-----------------------A:[1, 2, 3, 4, 1, 2, 3, 4]B:[2, 3, 4, 1, 2, 3, 4, 1]C:[3, 4, 1, 2, 3, 4, 1, 2]D:[4, 1, 2, 3, 4, 1, 2]-----------------------A:[1, 2, 3, 4, 1, 2, 3, 4]B:[2, 3, 4, 1, 2, 3, 4, 1]C:[3, 4, 1, 2, 3, 4, 1, 2]D:[4, 1, 2, 3, 4, 1, 2, 3]-----------------------
jdk1.5写法
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class CallThread2 {static int index = 0;static int max = 4 * 2;static HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();static {for (int i = 0; i < 4; i++) {map.put(i, new ArrayList<Integer>());}}static boolean isRunning = true;public static class MyRunnable implements Runnable {int name;Lock lock;Condition condition;private MyRunnable(int name, Lock lock, Condition condition) {super();this.name = name;this.lock = lock;this.condition = condition;}public boolean isMyName(int index) {while (index != name) {myWait();if(!isRunning)return false;return true;}return false;}public void myWait() {lock.lock();try {condition.await();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}lock.unlock();}public void myNotifyAll() {lock.lock();condition.signalAll();lock.unlock();}@Overridepublic void run() {// TODO Auto-generated method stubwhile (isRunning) {while (isMyName(index % 4)) {}if(!isRunning)return;for (int i = 0, arrIndex = index; i < 4; i++) {try {if (arrIndex-- < 0) {break;}List<Integer> list = map.get(i);if (list.size() < max) {list.add(name + 1);}} catch (Exception e) {}}System.err.println("A:" + map.get(0));System.err.println("B:" + map.get(1));System.err.println("C:" + map.get(2));System.err.println("D:" + map.get(3));System.err.println("-----------------------");if (map.get(map.size() - 1).size() == max) {isRunning = false;myNotifyAll();return;} else {index++;}myNotifyAll();}}}public static void main(String[] args) {// TODO Auto-generated method stubfinal Lock lock = new ReentrantLock();final Condition condition = lock.newCondition();ExecutorService threadPool = Executors.newCachedThreadPool();for (int i = 0; i < 4; i++) {threadPool.execute(new MyRunnable(i, lock, condition));} threadPool.shutdown();}}
感觉自己应该理解错了。不可能这么简单。
- Google面试题(java)—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
- Google面试题(java)—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
- Google面试题—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
- Google面试题—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD【转载】
- (Google面试题)有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。
- (Google面试题)有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空
- 多线程---有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
- 有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推........
- 有四个线程1、2、3、4,线程1的功能就是输出1,线程2的功能就是输出2,以此类推......... 现在有四个文件A B C D,初始都为空。现要让四个文件呈如下格式:A:1 2 3 4 1 2..
- 四个线程(ABCD)输入四个文件(1,2,3,4)
- 四个线程循环输出ABCD
- 面试题:有1、2、3、4四个数字,能组成多少个互不相同且一个数字中无重复数字的三位数,并把它们都输出。
- java多线程--给点1,2,A,B四个线程,交叉输出数字和字母
- 有1、2、3、4四个数字,输出所有能组成的互不相同且无重复的三位数。
- windows多线程编程-----1.四个线程同时输出//一个文件( 没有参数+有参数 )
- java使用三个线程,按顺序线程1输出1、线程2输出2、线程3输出3
- JAVA经典面试题:四线程写四个文件
- 金山面试题--四个线程a,b,c,d. 线程a,b对变量i加一. 线程c,d对变量i减去一.四个线程顺序执行, 每个线程每次只执行一次.i的初始值为0, 打印结果0 1 2 1 0 1 2 1 0 1
- Know Your Limits
- 0, \0, NULL的区别
- Know Your Next Commit
- WebRTC
- Javascript执行效率小结
- Google面试题(java)—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
- ubuntu 常用快捷键使用(本人亲测)
- The specified child already has a parent. You must call removeView() on the child's parent first
- Large Interconnected Data Belongs to a Database
- android常见异常处理
- 时间
- 本地连接显示成功,但是不能上网
- JMS学习笔记(三)
- EAX、ECX、EDX、EBX寄存器的作用