java基础——多线程(面试题)

4 篇文章 0 订阅
2 篇文章 0 订阅

[java]  view plain copy
  1. public class LogTest {  
  2.     /** 
  3.      * 需求:程序代码模拟产生16个日志对象,并且需要16秒才能打印完成日志,请程序添加四个线程,去调用parseLog的方法分头打印,只运行4秒打印完。 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         <span style="color:#ff0000">//新建一个队列  
  8.         final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(1);//1和16都可以  
  9.         //建立一个缓冲线程池  
  10.         ExecutorService threadPool = Executors.newCachedThreadPool();  
  11.         //创建4个Runnable即能产生4个线程  
  12.         for (int i = 0; i < 4; i++) {  
  13.             threadPool.execute(new Runnable() {  
  14.                 @Override  
  15.                 public void run() {  
  16.                     while(true){  
  17.                         try {  
  18.                             //queue.take()获取队列的数据  
  19.                             String log = queue.take();  
  20.                             //输出日志  
  21.                             LogTest.parselog(log);  
  22.                         } catch (InterruptedException e) {  
  23.                             e.printStackTrace();  
  24.                         }  
  25.                     }  
  26.                 }  
  27.             });  
  28.         }</span>  
  29.   
  30.         System.out.println("begin  " + (System.currentTimeMillis() / 1000));  
  31.         for (int i = 1; i <= 16; i++) {  
  32.             final String log = "" + i;  
  33.             //LogTest.parselog(log);  
  34.             try {  
  35.                 <span style="color:#ff0000">//队列放入数据  
  36.                 queue.put(log);</span>  
  37.             } catch (InterruptedException e) {  
  38.                 e.printStackTrace();  
  39.             }  
  40.         }  
  41.   
  42.     }  
  43.     /** 
  44.      * 每隔一秒打印一次日志 
  45.      * @param log 
  46.      */  
  47.     public static void parselog(String log) {  
  48.         System.out.println(log + ": " + System.currentTimeMillis() / 1000);  
  49.         try {  
  50.             Thread.sleep(1000);  
  51.         } catch (InterruptedException e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.     }  
  55. }  


第二题:现成程序中的Test类中的代码在不断地产生数据,然后交给TestDo.doSome()方法去处理,就好像生产者在不断地产生数据,消费者在不断消费数据。请将程序改造成有10个线程来消费生成者产生的数据,这些消费者都调用TestDo.doSome()方法去进行处理,故每个消费者都需要一秒才能处理完,程序应保证这些消费者线程依次有序地消费数据,只有上一个消费者消费完后,下一个消费者才能消费数据,下一个消费者是谁都可以,但要保证这些消费者线程拿到的数据是有顺序的。

[java]  view plain copy
  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.SynchronousQueue;  
  4.       
  5.     public class Test {  
  6.       
  7.         public static void main(String[] args) {  
  8.             <span style="color:#ff0000">//用灯控制一秒钟一个  
  9. <span style="white-space:pre">          </span>final Semaphore semaphore = new Semaphore(1);  
  10. <span style="white-space:pre">          </span>//同步队列  
  11. <span style="white-space:pre">          </span>final SynchronousQueue<String> queue =new  SynchronousQueue<String>();  
  12. <span style="white-space:pre">          </span>ExecutorService threadPool = Executors.newCachedThreadPool();  
  13. <span style="white-space:pre">          </span>//十个线程  
  14. <span style="white-space:pre">          </span>for (int i = 0; i < 10; i++) {  
  15. <span style="white-space:pre">              </span>threadPool.execute(new Runnable() {  
  16. <span style="white-space:pre">                  </span>@Override  
  17. <span style="white-space:pre">                  </span>public void run() {  
  18. <span style="white-space:pre">                      </span>try {  
  19. <span style="white-space:pre">                          </span>semaphore.acquire();//抢到等的使用权,用Lock也可以  
  20. <span style="white-space:pre">                          </span>//从队列里面获取数据,放入TestDo.doSome(input)  
  21. <span style="white-space:pre">                          </span>String input = queue.take();  
  22. <span style="white-space:pre">                          </span>String output = TestDo.doSome(input);  
  23. <span style="white-space:pre">                          </span>System.out.println(Thread.currentThread().getName()+ ":" + output);  
  24. <span style="white-space:pre">                          </span>semaphore.release();//用完之后释放掉  
  25. <span style="white-space:pre">                      </span>} catch (InterruptedException e) {  
  26. <span style="white-space:pre">                          </span>e.printStackTrace();  
  27. <span style="white-space:pre">                      </span>}  
  28. <span style="white-space:pre">                  </span>}  
  29. <span style="white-space:pre">              </span>});  
  30. <span style="white-space:pre">          </span>}</span>  
  31.             System.out.println("begin:"+(System.currentTimeMillis()/1000));  
  32.             for(int i=0;i<10;i++){  //这行不能改动  
  33.                 String input = i+"";  //这行不能改动  
  34.                   
  35.                 <span style="color:#ff0000">//把数据放入队里里面  
  36.                 try {  
  37.                     queue.put(input);  
  38.                 } catch (InterruptedException e) {  
  39.                     e.printStackTrace();  
  40.                 }</span>  
  41.             }  
  42.         }  
  43.     }  
  44.       
  45.     //不能改动此TestDo类  
  46.     class TestDo {  
  47.         public static String doSome(String input){  
  48.               
  49.             try {  
  50.                 Thread.sleep(1000);  
  51.             } catch (InterruptedException e) {  
  52.                 e.printStackTrace();  
  53.             }  
  54.             String output = input + ":"+ (System.currentTimeMillis() / 1000);  
  55.             return output;  
  56.         }  
  57.     }  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值