迅雷2014校招笔试题之多线程编程(3)

 

 

首页Linux编程

背景:

阅读新闻

迅雷2014校招笔试题之多线程编程

[日期:2013-09-29]   来源:Linux社区  作者:zzp_szu   [字体:]  

2 使用ArrayBlockingQueue:

程序如下:

public class ConsumerProducerActivity extends Activity {
 protected static Handler uiHandler;
 private TextView sizeTextView;
 private TextView producer_consumer;
 private ExecutorService executorService;
 private static String str;
 private static Buffer buffer = new Buffer();
 private static ArrayBlockingQueue<Integer> integer_buffer = new ArrayBlockingQueue<Integer>(10);
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_consumer_producer);

uiHandler = new Handler(new UiHandler());
  init(); 
 }

private void init(){
  sizeTextView = (TextView)findViewById(R.id.size);
  producer_consumer = (TextView)findViewById(R.id.produce_comsumer);
 
  executorService = Executors.newFixedThreadPool(2);
  //executorService.execute(new ProducerTask());
  //executorService.execute(new ComsumerTask());
  executorService.execute(new Producer());
  executorService.execute(new Consumer());
  executorService.shutdown();
 }
public static class Producer implements Runnable{
  @Override
  public void run(){
   try {
    int i = 50;
    while(i-- >0){
     if(integer_buffer.size() == 10){
      Message msg = new Message();
      msg.what = Definition.FULL;
      uiHandler.sendMessage(msg);
     }
     integer_buffer.put(1000);
     Message msg = new Message();
     msg.what = Definition.NOTEMPTY;
     uiHandler.sendMessage(msg);
     Thread.sleep((int)(Math.random()*10000));
    }
   } catch (InterruptedException e) {
    // TODO: handle exception
    e.printStackTrace();
   }
  }
 }
 
 public static class Consumer implements Runnable{
  @Override
  public void run(){
   try {
    int i = 50;
    while(i-- >0){
     if(integer_buffer.size() == 0){
      Message msg = new Message();
      msg.what = Definition.Empty;
      uiHandler.sendMessage(msg);
     }
     integer_buffer.take();
     Message msg = new Message();
     msg.what = Definition.NOTFULL;
     uiHandler.sendMessage(msg);
     Thread.sleep((int)(Math.random()*10000));
    }
   } catch (InterruptedException e) {
    // TODO: handle exception
    e.printStackTrace();
   }
  }
 }
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.consumer_producer, menu);
  return true;
 }
 
 public class UiHandler implements Callback{

@Override
  public boolean handleMessage(Message msg) {
   // TODO Auto-generated method stub
   sizeTextView.setText("大小为:" + integer_buffer.size());
   switch (msg.what) {
   case Definition.Empty:
    str += Definition.EMPTY_STRING;
    str += "\n";
    break;
   case Definition.FULL:
    str += Definition.FULL_STRING;
    str += "\n";
    break;
   case Definition.NOTEMPTY:
    str += Definition.NOTEMPTY_STRING;
    str += "\n";
    break;
   case Definition.NOTFULL:
    str += Definition.NOTFULL_STRING;
    str += "\n";
    break;
   default:
    break;
   }

producer_consumer.setText(str);
   return false;
  }
 
 }
}

这个比较容易理解,因为ArrayBlockingQueue具有能自行实现阻塞队列

 

一道百度2014校招笔试题

Golang通过Thrift框架完美实现跨语言调用

相关资讯       招聘  迅雷2014试题 

   

本文评论   查看全部评论 (0)


评论声明

尊重网上道德,遵守中华人民共和国的各项有关法律法规

承担一切因您的行为而直接或间接导致的民事或刑事法律责任

本站管理人员有权保留或删除其管辖留言中的任意内容

本站有权在网站内转载或引用您的评论

参与本评论即表明您已经阅读并接受上述条款

 

 

 

最新资讯

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/33037b39459f71be03749d27d176fd8a.html