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

 

 

首页Linux编程

背景:

阅读新闻

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

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

然后在acitivty中启动Producer和Customer两个线程,为了方便,只循环了50次。

package gibbon.thread.threadtestinAndroid;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.R.integer;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class ConsumerProducerActivity extends Activity {
 protected static Handler uiHandler;
 //protected static Handler comsumerHandler;
 private TextView sizeTextView;
 private TextView producer_consumer;
 private ExecutorService executorService;
 private static String str;
 private static Buffer buffer = new Buffer();
 
 @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.shutdown();
 }
 
 public static class ProducerTask implements Runnable{
  @Override
  public void run(){
   int count = 50;
   while(count-- > 0){
    buffer.write(1000);
    try {
     Thread.sleep((int)(Math.random()*10000));
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
 
 public static class ComsumerTask implements Runnable{
  @Override
  public void run(){
   int count = 50;
   int value = 0;
   while(count-- > 0){
    buffer.read();
    try {
     Thread.sleep((int)(Math.random()*10000));
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     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("大小为:" + buffer.queue.size());
   synchronized (msg) {
    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;
  }
 
 }

}

另外附带一些自定义变量:

package gibbon.thread.threadtestinandroid;

import android.R.integer;

public class Definition {
 protected static final int NOTFULL = 1;
 protected static final int NOTEMPTY = 2;
 protected static final int BUFFERSZIE = 3;
 protected static final int FULL = 4;
 protected static final int Empty = 5;
 protected static final String NOTFULL_STRING = "Wait for not noFull condition";
 protected static final String NOTEMPTY_STRING = "\tWait for notEmpty condition";
 protected static final String FULL_STRING = "\t\tBufferSize FuLL";
 protected static final String EMPTY_STRING = "\t\t\tBufferSize Empty";
}

运行结果如下:

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

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

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

 

一道百度2014校招笔试题

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

相关资讯       招聘  迅雷2014试题 

   

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


评论声明

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

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

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

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

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

 

 

 

最新资讯

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

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