线程工作不正常 - Android电子 [英] Thread not working properly - Android

查看:112
本文介绍了线程工作不正常 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的线程工作不正常。这是执行所有code在 RepeatingThread()方法,但不是在的run()方法,每3秒。我在做什么错了?

下面是code中的:

 公共RepeatingThread室温;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    Log.e(LOG_TAG,开始重复主题);
    // RT =新主题(新RepeatingThread());
    RT =新RepeatingThread();
    rt.start();
    Log.e(LOG_TAG,启动重复主题);
}公共类RepeatingThread实现Runnable {    私人最终处理程序mHandler =新的处理程序();
    私人INT LEN = 0;
    私人字节[] =输入新的字节[LEN]
    公共RepeatingThread(){
        //尝试{
        Log.e(LOG_TAG,inputJSON字符串之前);
        // inputJSON = dataInputStream.readUTF();
        // URL URL =新的URL(TCP://23.23.175.213:1337);
        // inputJSON = dataInputStream.readUTF();
        // inputstrrd =新的InputStreamReader(socket.getInputStream());
        字符串Hello =Hello World的;
        //串inputJSON = getStringFromBuffer(新的InputStreamReader(socket.getInputStream()));        //兑换
        Log.e(LOG_TAG,inputJSON字符串后:+ inputJSON);
        / *}
         赶上(UnknownHostException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
         }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
         } * /        //首先看这里
        // inputJSON是从服务器接收回 - 以inputJSON
        //字符串和使用正前pressions这里删除了所有其他字符
        //除了有效载荷JSON字符串。
        // refreshViewModels(inputJSON);
    }    @覆盖
    公共无效的run(){
        尝试{
            Log.e(LOG_TAG,IN REPEATINGTHREAD-INPUTJSON);
            //outputstrwr.write(outputJSONserv); //注释如果需要将数据发送到GET BACK JSON
            // inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
            inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }        Log.e(LOG_TAG,IN REPEATINGTHREAD-INPUTJSON2:+ inputJSON);
        refreshViewModels(inputJSON);
        mHandler.postDelayed(这一点,3000);
    }
}


解决方案

您也许应该使用定时对于这种短期反复任务:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    TimerTask的任务=新RepeatingTask();
    定时器定时器=新的Timer();
    timer.scheduleAtFixedRate(任务,0,3000);
}公共类RepeatingTask扩展的TimerTask { 私人INT LEN = 0; 私人字节[] =输入新的字节[LEN]
 公共RepeatingTask(){
     //尝试{
        Log.e(LOG_TAG,inputJSON字符串之前);
        // inputJSON = dataInputStream.readUTF();
        // URL URL =新的URL(TCP://23.23.175.213:1337);
        // inputJSON = dataInputStream.readUTF();
        // inputstrrd =新的InputStreamReader(socket.getInputStream());
        字符串Hello =Hello World的;
        //串inputJSON = getStringFromBuffer(新的InputStreamReader(socket.getInputStream()));        //兑换
        Log.e(LOG_TAG,inputJSON字符串后:+ inputJSON);
     / *}
     赶上(UnknownHostException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
     }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
     } * /     //首先看这里
     // inputJSON是从服务器接收回 - 以inputJSON
     //字符串和使用正前pressions这里删除了所有其他字符
     //除了有效载荷JSON字符串。
     // refreshViewModels(inputJSON);
 } @覆盖
 公共无效的run(){
      尝试{
          Log.e(LOG_TAG,IN REPEATINGTHREAD-INPUTJSON);
          //outputstrwr.write(outputJSONserv); //注释如果需要将数据发送到GET BACK JSON
          // inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
          inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
      }赶上(IOException异常五){
          // TODO自动生成catch块
          e.printStackTrace();
      }      Log.e(LOG_TAG,IN REPEATINGTHREAD-INPUTJSON2:+ inputJSON);
      refreshViewModels(inputJSON);
 }

}

在您的code,你混合线程模式(开始用start()方法)和处理程序,这是一个有点混乱。

My thread is not working properly. It is executing all the code in RepeatingThread() method but not in the run() method every 3 seconds. What am I doing wrong?

Here is the the code:

public RepeatingThread rt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.e(LOG_TAG, "Start Repeat Thread");
    //rt = new Thread(new RepeatingThread());
    rt = new RepeatingThread();
    rt.start();
    Log.e(LOG_TAG, "Started Repeat Thread");
}

public class RepeatingThread implements Runnable {

    private final Handler mHandler = new Handler();
    private int len = 0; 


    private byte[] input = new byte[len];


    public RepeatingThread() {
        //try {
        Log.e(LOG_TAG, "Before inputJSON String");
        //inputJSON = dataInputStream.readUTF();
        //URL url = new URL("tcp://23.23.175.213:1337");
        //inputJSON = dataInputStream.readUTF();
        //inputstrrd = new InputStreamReader(socket.getInputStream());
        String hello = "hello world";
        //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));

        //Convert 
        Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 
        /*}
         catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }*/

        //LOOK HERE FIRST  
        //inputJSON is what is received back from the server - Take the inputJSON 
        //String and use regular expressions HERE to remove all the other characters in the 
        //string except the payload JSON.
        //refreshViewModels(inputJSON);
    }

    @Override
    public void run() { 
        try {
            Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
            //outputstrwr.write(outputJSONserv);  //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
            //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
            inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
        refreshViewModels(inputJSON);
        mHandler.postDelayed(this, 3000);       
    }
}

解决方案

You should probably use a Timer for this kind of short recurrent tasks:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TimerTask task = new RepeatingTask();
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(task, 0, 3000);
}

public class RepeatingTask extends TimerTask {

 private int len = 0; 

 private byte[] input = new byte[len];


 public RepeatingTask() {
     //try {
        Log.e(LOG_TAG, "Before inputJSON String");
        //inputJSON = dataInputStream.readUTF();
        //URL url = new URL("tcp://23.23.175.213:1337");
        //inputJSON = dataInputStream.readUTF();
        //inputstrrd = new InputStreamReader(socket.getInputStream());
        String hello = "hello world";
        //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));

        //Convert 
        Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 
     /*}
     catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

     //LOOK HERE FIRST  
     //inputJSON is what is received back from the server - Take the inputJSON 
     //String and use regular expressions HERE to remove all the other characters in the 
     //string except the payload JSON.
     //refreshViewModels(inputJSON);
 }

 @Override
 public void run() { 
      try {
          Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
          //outputstrwr.write(outputJSONserv);  //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
          //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
          inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }

      Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
      refreshViewModels(inputJSON);
 }

}

In your code, you mix the Thread pattern (started with the start() method) and handlers, which is a bit confusing.

这篇关于线程工作不正常 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆