简体   繁体   English

Android-使用多个线程执行不同的功能

[英]Android - executing different functions with multiple threads

I am pretty new to Android programming but got some experience in other languages. 我对Android编程很陌生,但是在其他语言方面也有一些经验。 I want to create an APP which is principle working like this. 我想创建一个原理如此的APP。

在此处输入图片说明

  1. The APP is a process which is asking my Web-/Database-Server every 10 seconds if there is an event to execute. APP是一个进程,每隔10秒询问我的Web /数据库服务器是否有事件要执行。

  2. The Web-/Database-Server answers with an event id or even a function name. Web /数据库服务器用事件ID甚至函数名称回答。

  3. The APP opens a new thread which executes the event with the id or even better directly the function name. APP将打开一个新线程,该线程将使用id甚至直接使用函数名称执行事件。

My Questions are: 我的问题是:

  1. Is this perfomant? 这是个人吗? Or can this chrash pretty easily? 还是可以轻松应对这种崩溃?
  2. Is it limited to 2 threads within the process or can I even open every time a new thread for the function which I want to execute? 是在进程中限制为2个线程,还是每次为要执行的功能创建新线程时都可以打开? Maybe because of the other function is still running? 也许因为其他功能仍在运行?
  3. How can I execute a function with the return value? 如何使用返回值执行函数? For example 例如

    InputStream in = response.getEntity().getContent(); //Get the data in the entity

    public in(void) { // execute a function which got´s the same name as the variable "in" }

The result should be: The one thread is asking every 10 seconds my Web-/Database-Server if there is an event. 结果应该是:一个线程每10秒询问我的Web- /数据库服务器是否有事件。 The event gets executed within a thread and this is working parallel (at the same time without crashing or even getting stucked). 该事件在线程内执行,并且正在并行工作(同时没有崩溃或卡住)。

Sample Code of these Threads would be appreciated. 这些线程的示例代码将不胜感激。

My code till yet: 我的代码至今:

public class service extends Service { private static final String TAG = "MyService"; 公共类服务扩展了Service {private static final String TAG =“ MyService”;

 @Override public IBinder onBind(Intent intent) { return null; } public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); } @Override public int onStartCommand(Intent intent, int flags, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); Thread thread = new Thread(new Runnable() { @Override public void run() { new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println("Send GO!"); Device dev = new Device(); dev.getDeviceId(getBaseContext()); dev.getEmail(getBaseContext()); dev.sendDeviceItems(); } }, 0, 10000);//put here time 1000 milliseconds=1 second } }); thread.start(); return Service.START_STICKY; } } 
  1. Is this perfomant? 这是个人吗? Or can this chrash pretty easily? 还是可以轻松应对这种崩溃?
    • Yes it will be performance hit. 是的,这会影响性能。 Asking for api every 10sec will drain your battery also. 每10秒询问一次api也将耗尽您的电池。
    • With proper exception handling it won't crash pretty easily. 有了适当的异常处理,它就不会轻易崩溃。

Is it limited to 2 threads within the process or can I even open every time a new thread for the function which I want to execute? 是在进程中限制为2个线程,还是每次为要执行的功能创建新线程时都可以打开? Maybe because of the other function is still running? 也许因为其他功能仍在运行?

  • You can run as many threads you want but it will be difficult to maintain code in that 您可以运行任意多个线程,但是很难维护其中的代码
    case . 案件 。 You can use VOLLY or OKHTTP for network related things they will handle you thread part also. 您可以将VOLLY或OKHTTP用于与网络相关的事情,它们也会处理您的线程部分。

How can I execute a function with the return value? 如何使用返回值执行函数? For example 例如

InputStream in = response.getEntity().getContent(); InputStream in = response.getEntity()。getContent(); //Get the data in the entity //获取实体中的数据

public in(void) { // execute a function which got´s the same name as the variable "in" } public in(void){//执行一个与变量“ in”同名的函数}

  • For calling method at run time with Method name you can just use of REFLECTION in java. 为了在运行时使用方法名称调用方法,您只能在Java中使用REFLECTION。 This will fulfil your need. 这将满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM