本文主要是介绍黑马程序员---空中网面试题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
空中网4k/5k月薪挑选大四实习生的线程题
两年前,我们一个大四的学员去应聘空中网的实习生职位,空中网只给他出了三道线程题,拿回家做两天后再去给经理讲解,如果前两题做好了给4k月薪,第三道题也做出来的话就给5k的月薪。这样的实习薪水和招聘要求,不需要有工作经验的限制,纯粹是技术功底的比拼和考核,而不像许多其他公司非要招两年工作经验的人,逼得那些刚毕业和未毕业的大学生不得不去撒谎,不得不去做假简历,甚至假毕业证!所以,空中网的这份工作对未毕业的大学生来说,还是很有吸引力的。
*第一题:
现有的程序代码模拟产生了16个日志对象,并且需要运行16秒才能打印完这些日志,请在程序中增加4个线程去调用parseLog()方法来分头打印这16个日志对象,程序只需要运行4秒即可打印完这些日志对象。原始代码如下:
package read;public class Test {public static void main(String[] args){//打印一行现在的时间,多少秒:System.out.println("begin:"+(System.currentTimeMillis()/1000));for(int i=0;i<16;i++){ //这行代码不能改动final String log = ""+(i+1);//这行代码不能改动{Test.parseLog(log);}}}//parseLog方法内部的代码不能改动public static void parseLog(String log){//打印log序号+现在的时间(秒)System.out.println(log+":"+(System.currentTimeMillis()/1000));try {//休息一秒Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} }
}
运行结果:
begin:1400554274
1:1400554274
2:1400554275
3:1400554276
4:1400554277
5:1400554278
6:1400554279
7:1400554280
8:1400554281
9:1400554282
10:1400554283
11:1400554284
12:1400554285
13:1400554286
14:1400554287
15:1400554288
16:1400554289
修改之后:
package read;import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class Test {public static void main(String[] args){//打印一行现在的时间,多少秒:System.out.println("begin:"+(System.currentTimeMillis()/1000));final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(16);for(int i=0;i<16;i++){ //这行代码不能改动final String log = ""+(i+1);//这行代码不能改动{//Test.parseLog(log);try {queue.put(log);} catch (InterruptedException e) {e.printStackTrace();}}}for(int i=0;i<4;i++){new Thread(new Runnable(){public void run() {try {for(int j=0;j<4;j++){Test.parseLog(queue.take());}} catch (InterruptedException e) {e.printStackTrace();}}}).start();}}//parseLog方法内部的代码不能改动public static void parseLog(String log){//打印log序号+现在的时间(秒)System.out.println(log+":"+(System.currentTimeMillis()/1000));try {//休息一秒Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} }
}
运行结果:
begin:1400592551
1:1400592551
2:1400592551
3:1400592551
4:1400592551
5:1400592552
6:1400592552
7:1400592552
8:1400592552
9:1400592553
10:1400592553
11:1400592553
12:1400592553
13:1400592554
15:1400592554
14:1400592554
16:1400592554
package read;import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class Test {public static void main(String[] args){//打印一行现在的时间,多少秒:System.out.println("begin:"+(System.currentTimeMillis()/1000));final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(1);for(int i=0;i<4;i++){new Thread(new Runnable(){public void run() {try {while(true){Test.parseLog(queue.take());}} catch (InterruptedException e) {e.printStackTrace();}}}).start();}for(int i=0;i<16;i++){ //这行代码不能改动final String log = ""+(i+1);//这行代码不能改动{//Test.parseLog(log);try {queue.put(log);} catch (InterruptedException e) {e.printStackTrace();}}}}//parseLog方法内部的代码不能改动public static void parseLog(String log){//打印log序号+现在的时间(秒)System.out.println(log+":"+(System.currentTimeMillis()/1000));try {//休息一秒Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} }
}
运行结果: begin:1400594575
1:1400594575
2:1400594575
3:1400594575
4:1400594575
5:1400594576
6:1400594576
7:1400594576
8:1400594576
9:1400594577
10:1400594577
11:1400594577
12:1400594577
13:1400594578
14:1400594578
15:1400594578
16:1400594578
*第二题:
package queue;public class Test {public static void main(String[] args) {System.out.println("begin:"+(System.currentTimeMillis()/1000));for(int i=0;i<10;i++){ //这行不能改动String input = i+""; //这行不能改动String output = TestDo.doSome(input);System.out.println(Thread.currentThread().getName()+ ":" + output);}}
}//不能改动此TestDo类
class TestDo {public static String doSome(String input){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}String output = input + ":"+ (System.currentTimeMillis() / 1000);return output;}
}
修改后的代码:
package queue;import java.util.concurrent.Semaphore;
import java.util.concurrent.SynchronousQueue;public class Test {public static void main(String[] args) {System.out.println("begin:"+(System.currentTimeMillis()/1000));//定义一盏灯。相当于Lock。final Semaphore semaphore = new Semaphore(1);//一种阻塞队列,其中每个插入操作必须等待另一个线程的对应移除操作。final SynchronousQueue<String> queue = new SynchronousQueue<String>();for(int i=0;i<10;i++){new Thread(new Runnable(){public void run() {try {semaphore.acquire();String input = queue.take();String output = TestDo.doSome(input);System.out.println(Thread.currentThread().getName()+ ":" + output);} catch (InterruptedException e) {e.printStackTrace();}finally{semaphore.release();}}}).start();}for(int i=0;i<10;i++){ //这行不能改动String input = i+""; //这行不能改动try {queue.put(input);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
}//不能改动此TestDo类
class TestDo {public static String doSome(String input){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}String output = input + ":"+ (System.currentTimeMillis() / 1000);return output;}
}
运行结果:
begin:1400596420
Thread-0:0:1400596421
Thread-1:1:1400596422
Thread-2:2:1400596423
Thread-3:3:1400596424
Thread-4:4:1400596425
Thread-5:5:1400596426
Thread-6:6:1400596427
Thread-7:7:1400596428
Thread-8:8:1400596429
Thread-9:9:1400596430
*第三题:
4:4:1258199615
1:1:1258199615
3:3:1258199615
1:2:1258199615
请修改代码,如果有几个线程调用TestDo.doSome(key, value)方法时,传递进去的key相等(equals比较为true),则这几个线程应互斥排队输出结果,即当有两个线程的key都是"1"时,它们中的一个要比另外其他线程晚1秒输出结果,如下所示:
4:4:1258199615
1:1:1258199615
3:3:1258199615
1:2:1258199616
总之,当每个线程中指定的key相等时,这些相等key的线程应每隔一秒依次输出时间值(要用互斥),如果key不同,则并行执行(相互之间不互斥)。原始代码如下:
package syn;//不能改动此Test类 public class Test extends Thread{private TestDo testDo;private String key;private String value;public Test(String key,String key2,String value){this.testDo = TestDo.getInstance();/*常量"1"和"1"是同一个对象,下面这行代码就是要用"1"+""的方式产生新的对象,以实现内容没有改变,仍然相等(都还为"1"),但对象却不再是同一个的效果*/this.key = key+key2; this.value = value;}public static void main(String[] args) throws InterruptedException{Test a = new Test("1","","1");Test b = new Test("1","","2");Test c = new Test("3","","3");Test d = new Test("4","","4");System.out.println("begin:"+(System.currentTimeMillis()/1000));a.start();b.start();c.start();d.start();}public void run(){testDo.doSome(key, value);}}class TestDo {private TestDo() {}private static TestDo _instance = new TestDo(); public static TestDo getInstance() {return _instance;}public void doSome(Object key, String value) {// 以大括号内的是需要局部同步的代码,不能改动!{try {Thread.sleep(1000);System.out.println(key+":"+value + ":"+ (System.currentTimeMillis() / 1000));} catch (InterruptedException e) {e.printStackTrace();}}}}
运行结果:
begin:1400601982
1:1:1400601983
4:4:1400601983
1:2:1400601983
3:3:1400601983
修改之后:
package syn;import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;//不能改动此Test类
public class Test extends Thread{private TestDo testDo;private String key;private String value;public Test(String key,String key2,String value){this.testDo = TestDo.getInstance();/*常量"1"和"1"是同一个对象,下面这行代码就是要用"1"+""的方式产生新的对象,以实现内容没有改变,仍然相等(都还为"1"),但对象却不再是同一个的效果*///它是两个变量相加的结果,在运行时候才能知道结果,所以编译器无法把两个"1"识别成为一个对象。this.key = key+key2; /*如果是这样写,编译器就会把它们看作是一个对象:* String a = "1"+"";* String b = "1"+"";*/this.value = value;}public static void main(String[] args) throws InterruptedException{Test a = new Test("1","","1");Test b = new Test("1","","2");Test c = new Test("3","","3");Test d = new Test("4","","4");System.out.println("begin:"+(System.currentTimeMillis()/1000));a.start();b.start();c.start();d.start();}public void run(){testDo.doSome(key, value);}
}class TestDo {//单例设计模式private TestDo() {}private static TestDo _instance = new TestDo(); public static TestDo getInstance() {return _instance;}//new一个支持多线程并发操作的ArrayList,来存放那些key。CopyOnWriteArrayList<Object> keys = new CopyOnWriteArrayList<Object>();public void doSome(Object key, String value) {Object obj = key;//如果集合里没有这个key,就添加到集合里。如果集合里有这个key,就把这个新的对象替换成集合里哪个旧的。if(!keys.contains(key)){keys.add(key);}else{for(Iterator<Object> it=keys.iterator(); it.hasNext();){Object o = it.next();if(o.equals(obj)){obj = o;}}}synchronized(obj)// 以大括号内的是需要局部同步的代码,不能改动!{try {Thread.sleep(1000);System.out.println(key+":"+value + ":"+ (System.currentTimeMillis() / 1000));} catch (InterruptedException e) {e.printStackTrace();}}}
}
//这样相同的key值就不会在同一秒打印了。
//但是排除另外一种极罕见的可能,就是毫秒换算秒的误差。
运行结果:
begin:1400601770
4:4:1400601771
3:3:1400601771
1:1:1400601771
1:2:1400601772
这篇关于黑马程序员---空中网面试题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!