Google面试题(java)—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
来源:互联网 发布:转移矩阵和马尔科夫链 编辑:程序博客网 时间:2024/05/16 00:41
转自:http://blog.csdn.net/b275518834/article/details/8750142
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();}}}
C++ 不过不完整
1 #include <iostream> 2 #include <stdlib.h> 3 #include <pthread.h> 4 using namespace std; 5 6 pthread_mutex_t myloack=PTHREAD_MUTEX_INITIALIZER; 7 pthread_cond_t mycond=PTHREAD_COND_INITIALIZER; 8 int n=0; 9 void *ThreadFunc(void *arg)10 {11 int num=(int )arg;12 for (int i = 0; i < 10; ++i)13 {14 pthread_mutex_lock(&myloack);15 while (n!=num)//1号线程第一次执行时直接跳过16 pthread_cond_wait(&mycond,&myloack);//因为多个线程共用一个条件变量,所以在田间变量被signal后,需要用while来确定是否本线程获得执行权17 18 if (num==0)19 cout<<"1";20 else if(num==1)21 cout<<"2";22 else if(num==2)23 cout<<"3";24 else 25 cout<<"4"<<endl;26 n=(n+1)%4;27 pthread_mutex_unlock(&myloack);28 pthread_cond_broadcast(&mycond);29 }30 return (void *)0;31 }32 33 int main(int argc, char const *argv[])34 {35 36 pthread_t id[4];37 for (int i = 0; i < 4; ++i)38 {39 int err=pthread_create(&id[i],NULL,ThreadFunc,(void *)i);40 if (err!=0)41 {42 cout<<"create err:"<<endl;43 exit(-1);44 }45 46 }47 48 for (int i = 0; i < 4; ++i)49 {50 int ret=pthread_join(id[i],NULL);51 if (ret!=0)52 {53 cout<<"join err:"<<endl;54 exit(-1);55 }56 }57 return 0;58 }
0 0
- 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
- HDU 5634 Rikka with Phi
- 搭建hadoop2.6集群
- 博客的第一天
- 从软件工程的角度写机器学习2——流行的机器学习应用模式与算法
- NGUI另一种按钮事件触发的方法
- Google面试题(java)—有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
- 【慕课笔记】第二章 认识JAVA中的字符串 第3节 JAVA中String类的常用方法(一)
- 使用VS2013编译Lua5.3.2
- 毕业设计第五天
- 如何修改Eclipse的 workspace目录
- 【HDU5452】Minimum Cut
- 在Service中播放音乐
- Nginx转发请求
- 算法学习笔记-排序